想找寻直接在c#的aspx页面中通过手写代码调用dll中的函数,不过还没找到准确的资料。
只能,循序渐进,一步一步来.
一.
先从c#项目中直接加载dll的方法入手:(通过网上一些资料显示,也测试通过了的可行步骤).
参考:http://blog.csdn.net/yuanxi/archive/2006/06/01/768203.aspx 潘磊的专栏
//
******temp.cs***********
//
namespace TEMPV01 ... {
public class Manage ...{
public int cc = 0;
public int init(int temp) ...{
cc= cc+temp;
return cc; } }}
/**/ /************************/ //
namespace TEMPV01 ... {
public class Manage ...{
public int cc = 0;
public int init(int temp) ...{
cc= cc+temp;
return cc; } }}
/**/ /************************/ //
1.生成dll文件,我用的是c#编写成的代码,temp.cs
打开.net自带的工具 .net工具-〉.net 命令提示
输入 csc /t:library /out:d:/temp.dll d:/temp.cs //cs文件在d盘下,生成的dll文件也放在d盘下。
执行后,成功生成temp.dll文件,把该文件复制到需要调用它的项目的bin 文件夹里.
2.在项目中添加引用,(我的项目名为ctest1),选择浏览,选中temp.dll文件,确定.
3.在项目里的一页面index.aspx 对应的cs文件index.aspx.cs下导入命名空间
using TEMPV01;
并在执行代码中定义类Manage 对象 ma, 再调用其中的方法init(),运行页面,页面显示 '333'.执行成功.
//
******index.aspx.cs********
//
using TEMPV01;
...
namespace ctest1 ... {
//...
public class index : System.Web.UI.Page ...{
//...
private void Page_Load(object sender, System.EventArgs e) ...{
// 在此处放置用户代码以初始化页面
Manage ma = new Manage();
Label1.Text =ma.init(333).ToString(); }}
//****************************//
因为temp.cs中有类有命名空间,所以之前使用其他的方法
[System.Runtime.InteropServices.DllImportAttribute("temp")]
public static extern int init(int temp);
不可行,出现一些错误. 大概可能需要把函数init()改为全局静态才能行得通吧,不过对此还未做过测试.
using TEMPV01;
...
namespace ctest1 ... {
//...
public class index : System.Web.UI.Page ...{
//...
private void Page_Load(object sender, System.EventArgs e) ...{
// 在此处放置用户代码以初始化页面
Manage ma = new Manage();
Label1.Text =ma.init(333).ToString(); }}
//****************************//
因为temp.cs中有类有命名空间,所以之前使用其他的方法
[System.Runtime.InteropServices.DllImportAttribute("temp")]
public static extern int init(int temp);
不可行,出现一些错误. 大概可能需要把函数init()改为全局静态才能行得通吧,不过对此还未做过测试.