我想加载一个程序集(dll-test.dll
),并运行该方法GetLabel
:
namespace Dlltest.Test
{
public class Main
{
public string GetLabel()
{
string test = "TestString";
return test;
}
}
}
我有以下代码但是我无法运行GetLabel
:
Assembly assembly = Assembly.LoadFile(@"C:\dll-test.dll");
Type type = assembly.GetType();
var obj = Activator.CreateInstance(type);
var result = type.InvokeMember("GetLabel",
BindingFlags.Default | BindingFlags.InvokeMethod,
null,
obj,
null);
MessageBox.Show(result.ToString);
它应该显示一个带有TestString
.
编辑
我最终使它与:
Assembly assembly = Assembly.LoadFile(@"C:\dll-test.dll");
var type = assembly.GetTypes();
var obj = Activator.CreateInstance(type[0]);
var result = type[0].InvokeMember("GetLabel",
BindingFlags.Default | BindingFlags.InvokeMethod,
null,
obj,
null);
MessageBox.Show(result.ToString());