天天看点

nginx 调用dll_C# 不进行引用调用第三方DLL

我们使用VS 开发工具 封装的c# 类库 在使用的过程中需要需要在解决方案下的项目中进行引用,而如果是C#脚本不能进行引用的话我们就需要进行其他的方法进行调用第三放的dll了。

我们可以使用 DllImport 调用第三方的dll 但是 C#封装的DLL是非标准的DLL(托管类),不可以用 DllImport 调用,DllImport是用来调用标准类(非托管类)的,这类DLL一般是用C++写的

所以DllImport 调用第三方的dll我们只能使用 c++封装的标准类,而我们自己使用c#的封装的无法调用

DllImport 调用标准类方法

[DllImport("user32.dll", EntryPoint="MessageBoxA")]

static extern int MsgBox(int hWnd, string msg, string caption, int type);

EntryPoint 可以设置使用其他方法名

我们还需要配合申明 静态方法才能使用

我们封装的c#类库 不可以使用这种方法去调用,由此我们可以使用反射,使用反射方法去调用c#封装的非标准的dll 代码如下:

Assembly asmA = Assembly.LoadFrom("c#封装的dll");

string strClass = string.Empty;

if (null != asmA)

{

strClass = "调用方法所在类";

try

{

Type typeTest = asmA.GetType(strClass);

object obj = Activator.CreateInstance(typeTest);

System.Reflection.MethodInfo miMethod = typeTest.GetMethod("方法名");

string[] param = { 参数使用数组传递 };

var str=(string)miMethod.Invoke(obj, param);

Console.WriteLine(str);

}

catch (Exception e)

{

throw e;

}

}