不引用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" );
	}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章