Activator.CreateInstance 使用自定义 class 参数
Activator.CreateInstance with custom class argument
我正在尝试使用 Activator.CreateInstance
.
从 dll 创建一个实例(Class 测试)
Class 测试有一个需要参数的构造函数 Class 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
});
}
}
这里是完整代码link:https://codeshare.io/2EzrEv
有什么解决问题的建议吗?
如图所示的Activator.CreateInstance
:应该可以正常工作。如果不是,那么您正在初始化的 type
可能不是您认为的那样 - 检查 type.FullName
的值。或者,您可能在不同位置使用相同名称 的类型 ,这意味着:您在 中传递的 CommonService
不是 与类型期望的 CommonService
相同。您可以通过反思来检查。以下打印 True
两次:
using System;
using System.Linq;
using System.Reflection;
public class Test : BaseTest
{
public Test(CommonService commonService) : base(commonService, new TestConfigurations()
{
enableTimer = false
})
{
}
}
static class P
{
static void Main()
{
// Usage
var commonService = new CommonService();
var test = new Test(commonService); // test can be created successfully.
var type = typeof(Test); // instead of loop
dynamic c = Activator.CreateInstance(type, new object[] { // Throw System.MissingMethodException error
commonService
});
Console.WriteLine(c is object); // it worked
var ctor = type.GetConstructors().Single();
var ctorArg = ctor.GetParameters().Single();
Console.WriteLine(ctorArg.ParameterType == typeof(CommonService)); // check the expected param type
}
}
public class CommonService { }
public class TestConfigurations
{
internal bool enableTimer;
}
public class BaseTest
{
public BaseTest(CommonService svc, TestConfigurations cfg) { }
}
我正在尝试使用 Activator.CreateInstance
.
Class 测试有一个需要参数的构造函数 Class 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
});
}
}
这里是完整代码link:https://codeshare.io/2EzrEv
有什么解决问题的建议吗?
如图所示的Activator.CreateInstance
:应该可以正常工作。如果不是,那么您正在初始化的 type
可能不是您认为的那样 - 检查 type.FullName
的值。或者,您可能在不同位置使用相同名称 的类型 ,这意味着:您在 中传递的 CommonService
不是 与类型期望的 CommonService
相同。您可以通过反思来检查。以下打印 True
两次:
using System;
using System.Linq;
using System.Reflection;
public class Test : BaseTest
{
public Test(CommonService commonService) : base(commonService, new TestConfigurations()
{
enableTimer = false
})
{
}
}
static class P
{
static void Main()
{
// Usage
var commonService = new CommonService();
var test = new Test(commonService); // test can be created successfully.
var type = typeof(Test); // instead of loop
dynamic c = Activator.CreateInstance(type, new object[] { // Throw System.MissingMethodException error
commonService
});
Console.WriteLine(c is object); // it worked
var ctor = type.GetConstructors().Single();
var ctorArg = ctor.GetParameters().Single();
Console.WriteLine(ctorArg.ParameterType == typeof(CommonService)); // check the expected param type
}
}
public class CommonService { }
public class TestConfigurations
{
internal bool enableTimer;
}
public class BaseTest
{
public BaseTest(CommonService svc, TestConfigurations cfg) { }
}