Activator.CreateInstance(string, string) 抛出 TypeLoadException
我有以下代码:
public static void Invoke(string assemblyName, string scheduledTaskExecutorName)
{
ObjectHandle objectHandle = Activator.CreateInstance(assemblyName, scheduledTaskExecutorName);
IScheduledTaskExecutor scheduledTaskExecutor = (IScheduledTaskExecutor)objectHandle.Unwrap();
scheduledTaskExecutor.ExecuteScheduledTask();
}
我有一个名为 DummyScheduledTaskExecutor
的类,如下所示:
public class DummyScheduledTaskExecutor : IScheduledTaskExecutor
{
public void ExecuteScheduledTask()
{
DummyTextFile.Text = "Success!";
}
}
它驻留在一个程序集中,其程序集名称(如程序集属性中定义)为 Tests.WebApplication.Application。单位
。
我对 Invoke(string, string)
的调用如下所示:
ScheduledTaskInvoker.Invoke("Tests.WebApplication.Application.Unit", "DummyScheduledTaskExecutor");
尝试运行此代码只会引发 TypeLoadException。我是否错误地表达了程序集或类型名称,或者发生了其他情况?
I have the following code:
public static void Invoke(string assemblyName, string scheduledTaskExecutorName)
{
ObjectHandle objectHandle = Activator.CreateInstance(assemblyName, scheduledTaskExecutorName);
IScheduledTaskExecutor scheduledTaskExecutor = (IScheduledTaskExecutor)objectHandle.Unwrap();
scheduledTaskExecutor.ExecuteScheduledTask();
}
I have a class called DummyScheduledTaskExecutor
which looks like this:
public class DummyScheduledTaskExecutor : IScheduledTaskExecutor
{
public void ExecuteScheduledTask()
{
DummyTextFile.Text = "Success!";
}
}
It resides in an assembly whose assembly name (as defined in the assembly's properties) is Tests.WebApplication.Application.Unit
.
My call to Invoke(string, string)
looks like this:
ScheduledTaskInvoker.Invoke("Tests.WebApplication.Application.Unit", "DummyScheduledTaskExecutor");
Trying to run this code just throws a TypeLoadException. Have I expressed the assembly or type name incorrectly, or is something else going on?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
ScheduledTaskExecutorName 需要包含命名空间。
尝试在第二个参数中包含整个名称空间。
我的例子:
scheduledTaskExecutorName need to include the namespace.
Try including the whole namespace in your second parameter.
My example:
您尝试过程序集绑定日志吗?
http://msdn.microsoft.com/en-us/library/e74a18c4.aspx
did you try the assembly binding log?
http://msdn.microsoft.com/en-us/library/e74a18c4.aspx
CreateInstance 将假定程序集已加载。我的猜测是事实并非如此。您需要改用CreateInstanceFrom。
编辑:如果您知道程序集已加载,那么很可能是 CreateInstance 的参数有问题。使用完全限定的类型名称而不是像现在这样的简单类名称。
CreateInstance will assume the assembly is already loaded. My guess is it actually isn't. You need to use CreateInstanceFrom instead.
EDIT: Well if you know the assembly is loaded, then it is most likely a problem with your parameters to CreateInstance. Use the fully qualified type name instead of the simple class name like you are now.