Activator.CreateInstance(string, string) 抛出 TypeLoadException

发布于 2024-12-17 04:42:44 字数 960 浏览 3 评论 0原文

我有以下代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

草莓酥 2024-12-24 04:42:44

ScheduledTaskExecutorName 需要包含命名空间。

尝试在第二个参数中包含整个名称空间。

我的例子:

namespace WindowsFormsApplication6
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            object obj = Activator.CreateInstance(null, "WindowsFormsApplication6.TestClass");
        }
    }
}

namespace WindowsFormsApplication6
{
    public class TestClass
    {

    }
}

scheduledTaskExecutorName need to include the namespace.

Try including the whole namespace in your second parameter.

My example:

namespace WindowsFormsApplication6
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            object obj = Activator.CreateInstance(null, "WindowsFormsApplication6.TestClass");
        }
    }
}

namespace WindowsFormsApplication6
{
    public class TestClass
    {

    }
}
耶耶耶 2024-12-24 04:42:44

您尝试过程序集绑定日志吗?

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

喵星人汪星人 2024-12-24 04:42:44

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文