类似于人机在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" );
}
}