類似於人機在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" );
}
}