我正在尝试通过使用从 dll 创建一个实例(类测试)Activator.CreateInstance。
类 Test 有一个构造函数,它需要一个争论类 CommonService。但它会抛出System.MissingMethodException错误。
有代码:
// Define
public class Test: BaseTest
{
public Test(CommonService commonService) : base(commonService, new TestConfigurations()
{
enableTimer = false
})
{
}
}
// Usage
var commonService = new CommonService();
var test = new Test(commonService); // test can be created successfully.
var dll = Assembly.LoadFile(@"xxx\Test.dll");
foreach (Type type in DLL.GetExportedTypes())
{
if (type.BaseType?.Name?.Equals("BaseTest") == true)
{
dynamic c = Activator.CreateInstance(type, new object[] { // Throw System.MissingMethodException error
commonService
});
}
}
这是完整的代码链接:https ://codeshare.io/2EzrEv
关于如何解决这个问题有什么建议吗?