C# 在线程中运行 InvokeMember
所以我想做的是在线程中运行 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
传奇的
foreach
捕获问题 - 当它开始调用它时(在不同的线程上),type
变量引用了不同的Type
...改为复制:
区别在于循环变量(示例中的
type
,我的示例中的tmp
)的作用域外部循环(根据规范),因此出于捕获目的,它是整个循环共有的单个变量。但是,在我的示例中,type
的作用域位于循环内部,因此(出于捕获目的)每次迭代都被视为不同的变量。the legendary
foreach
capture issue - by the time it gets around to invoking it (on a different thread), thetype
variable refers to a differentType
...Make a copy instead:
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.