C# 在线程中运行 InvokeMember

发布于 2024-11-18 20:00:06 字数 1831 浏览 2 评论 0原文

所以我想做的是在线程中运行 InvokeMember。在这里获取了如何执行此操作的信息: C# :在单独的线程中使用 [Type].InvokeMember() 调用方法

所以我的代码现在看起来像这样,并且可以工作:

            Assembly OCA = Assembly.LoadFrom("./CardMax2.Elkart.OrderClutchAgent.dll");
            Type[] types = OCA.GetTypes();
            foreach (var type in types)
            {
                //MethodInfo[] methods = type.GetMethods();
                if (type.Name == "OrderClutchAgent")
                {
                    var obj = Activator.CreateInstance(type);
                    type.InvokeMember("RunAgent",BindingFlags.Default | BindingFlags.InvokeMethod,null,obj,null);
                }

            }

现在,当我尝试在线程中运行它时,代码看起来像这个:

            Assembly OCA = Assembly.LoadFrom("./CardMax2.Elkart.OrderClutchAgent.dll");
            Type[] types = OCA.GetTypes();
            foreach (var type in types)
            {
                //MethodInfo[] methods = type.GetMethods();
                if (type.Name == "OrderClutchAgent")
                {
                    var obj = Activator.CreateInstance(type);
                    Thread t = new Thread(delegate()
                                              {
                                                  type.InvokeMember("RunAgent", BindingFlags.Default | BindingFlags.InvokeMethod, null, obj, null);
                                              });
                    t.Start();
                }

            }

但是通过这段代码,我遇到了奇怪的异常:

Method 'Org.BouncyCastle.Asn1.X509.TbsCertificateList+RevokedCertificatesEnumeration+RevokedCertificatesEnumerator.RunAgent' not found.

我是否做了完全错误的事情,或者也许有人可以指出为什么我会得到这个异常..

so what i am tryng to do is to run the InvokeMember in a thread. Got the info here how to do it: C# : Invoke a method with [Type].InvokeMember() in a separate Thread

So my code looks like this right now and this works:

            Assembly OCA = Assembly.LoadFrom("./CardMax2.Elkart.OrderClutchAgent.dll");
            Type[] types = OCA.GetTypes();
            foreach (var type in types)
            {
                //MethodInfo[] methods = type.GetMethods();
                if (type.Name == "OrderClutchAgent")
                {
                    var obj = Activator.CreateInstance(type);
                    type.InvokeMember("RunAgent",BindingFlags.Default | BindingFlags.InvokeMethod,null,obj,null);
                }

            }

Now when i try to run it in a thread, the code looks like this:

            Assembly OCA = Assembly.LoadFrom("./CardMax2.Elkart.OrderClutchAgent.dll");
            Type[] types = OCA.GetTypes();
            foreach (var type in types)
            {
                //MethodInfo[] methods = type.GetMethods();
                if (type.Name == "OrderClutchAgent")
                {
                    var obj = Activator.CreateInstance(type);
                    Thread t = new Thread(delegate()
                                              {
                                                  type.InvokeMember("RunAgent", BindingFlags.Default | BindingFlags.InvokeMethod, null, obj, null);
                                              });
                    t.Start();
                }

            }

But with this code i am getting strange exception:

Method 'Org.BouncyCastle.Asn1.X509.TbsCertificateList+RevokedCertificatesEnumeration+RevokedCertificatesEnumerator.RunAgent' not found.

Am i doing something totally wrong or maybe someone can point out why i am getting this exception..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

江南烟雨〆相思醉 2024-11-25 20:00:06

传奇的 foreach 捕获问题 - 当它开始调用它时(在不同的线程上),type 变量引用了不同的 Type...

改为复制:

foreach (var tmp in types)
{
    var type = tmp;
    // your code
}

区别在于循环变量(示例中的 type,我的示例中的 tmp)的作用域外部循环(根据规范),因此出于捕获目的,它是整个循环共有的单个变量。但是,在我的示例中,type 的作用域位于循环内部,因此(出于捕获目的)每次迭代都被视为不同的变量。

the legendary foreach capture issue - by the time it gets around to invoking it (on a different thread), the type variable refers to a different Type...

Make a copy instead:

foreach (var tmp in types)
{
    var type = tmp;
    // your code
}

The difference is that the loop variable (type in your example, tmp in mine) is scoped outside the loop (according to the spec), hence for capture purposes it is a single variable common to the entire loop. However, type in my example is scoped inside the loop, so (for capture purposes) is treated as a different variable per iteration.

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