不引用dll如何调用dll链接库(动态调用,不知道有没有这个dll)

类似于人机在Plugin中调用的dll,方法如下:

static void Main( string[] args )
  {
  	// 读取dll
   Assembly assembly = Assembly.LoadFrom( "ClassLibrary.dll" );
   
	//获取要实例化类的类型
   Type type = assembly.GetType( "ClassLibrary.Class1" );

	//实例化类
   object obj = Activator.CreateInstance( type );

	//定位到dll中的方法
   MethodInfo methodInfo = type.GetMethod( "Function" );
	//执行方法
   methodInfo.Invoke( obj, null );

   Console.Read();
  }

上述的

//获取要实例化类的类型
   Type type = assembly.GetType( "ClassLibrary.Class1" );

ClassLibrary.Class1代表dll中的类的类型;

	//定位到dll中的方法
   MethodInfo methodInfo = type.GetMethod( "Function" );

"Function"代表dll类中的方法名称。
具体的dll中代码如下:

public class Class1
{
	public Class1()
	{
		Console.WriteLine( "cons" );
	}

	public void Function()
	{
		Console.WriteLine( "fun" );
	}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章