System.Reflection.MethodInfo.Invoke 和多线程

发布于 2024-09-19 01:50:10 字数 545 浏览 4 评论 0原文

您好,我如何使用带有线程的参数调用 System.Reflection.MethodInfo.Invoke() 。

例如..

假设我有一个方法,允许您传入一个代表一个的字符串 类名并动态调用相应的类方法,现在我想要 使用线程调用此 Methodinfo.invoke ,我不知道如何执行此操作,因为我正在使用参数调用调用。 meb 给出的代码片段。感谢您的帮助

Type classType = objAssembly.GetType("MyClassName");
object obj = Activator.CreateInstance(classType)
bject[] _objval = new object[3]; 
object[] parameters = new object[] { _objval };
MethodInfo mi = classType.GetMethod("MyMethod");
mi.Invoke(obj, parameters);  // <---**How do i call this with threads.. ????**

Hi how do i call System.Reflection.MethodInfo.Invoke() with paramters with threads.

For instance..

Say I have a method that allows you to pass in a string that represents a
class name and calls corresponding class method dynamically , now i want to
call this Methodinfo.invoke with threads ,I have no idea how to do this since i am calling invoke with paramter . Code snippet given meblow . Thank you for your help

Type classType = objAssembly.GetType("MyClassName");
object obj = Activator.CreateInstance(classType)
bject[] _objval = new object[3]; 
object[] parameters = new object[] { _objval };
MethodInfo mi = classType.GetMethod("MyMethod");
mi.Invoke(obj, parameters);  // <---**How do i call this with threads.. ????**

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

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

发布评论

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

评论(3

不寐倦长更 2024-09-26 01:50:10

由于您想要使用 System.Threading.Thread 创建新线程,而不是在现有 UI 线程或线程池线程上进行调用,因此首先要注意的是使用 System.Threading .Thread 您可以使用 ThreadStartParameterizedThreadStart 委托。

您确实需要为线程的 main 方法提供参数,但 ParameterizedThreadStart 只允许使用对象,这会强制您将其强制转换为所需的类型。因此,我们将使用闭包来获取以类型安全的方式传递的所有参数。

public void InvokeOnNewThread(this MethodInfo mi, object target, params object[] parameters)
{
     ThreadStart threadMain = delegate () { mi.Invoke(target, parameters); };
     new System.Threading.Thread(threadMain).Start();
}

用法示例:

mi.InvokeOnNewThread(obj, parameters);

如果您正在使用 .NET 2.0,则从参数列表中取出关键字 this 并调用:

InvokeOnNewThread(mi, obj, parameters);

这将丢弃任何返回值,但您问题中的无线程示例也是如此。如果您需要返回值,请发表评论。

Since you're wanting to create a new thread with System.Threading.Thread rather than make the call on an existing UI thread or threadpool thread, first thing to notice is that with System.Threading.Thread you can use either a ThreadStart or ParameterizedThreadStart delegate.

You do want parameters to your thread's main method, but ParameterizedThreadStart only allows an object, which forces you to cast it to the required type. So we'll just use a closure to get all the arguments passed across in a type-safe way.

public void InvokeOnNewThread(this MethodInfo mi, object target, params object[] parameters)
{
     ThreadStart threadMain = delegate () { mi.Invoke(target, parameters); };
     new System.Threading.Thread(threadMain).Start();
}

Example usage:

mi.InvokeOnNewThread(obj, parameters);

If you're working with .NET 2.0, then take out the keyword this from the parameter list and call like:

InvokeOnNewThread(mi, obj, parameters);

This will discard any return value, but so did the unthreaded example in your question. If you need the return value leave a comment.

夜唯美灬不弃 2024-09-26 01:50:10

您可以使用匿名方法启动线程:

Thread myThread = new Thread(delegate() {
    object obj = Activator.CreateInstance(typeof(MyClassName));

    object[] _objval = new object[3]; 
    object[] parameters = new object[] { _objval };
    MethodInfo mi = classType.GetMethod("MyMethod");
    mi.Invoke(obj, parameters); 
});
myThread.Start();

delegate() { ... } 内的代码是在新线程上执行的匿名方法。

You can start a thread with an anonymous method:

Thread myThread = new Thread(delegate() {
    object obj = Activator.CreateInstance(typeof(MyClassName));

    object[] _objval = new object[3]; 
    object[] parameters = new object[] { _objval };
    MethodInfo mi = classType.GetMethod("MyMethod");
    mi.Invoke(obj, parameters); 
});
myThread.Start();

The code inside the delegate() { ... } is an anonymous method that is executed on the new thread.

叫嚣ゝ 2024-09-26 01:50:10

只是一个建议,为什么不使用 .Net 4.0 Framework,它有更简单的线程实现。只需使用 Parallel.For、Parallel.ForEach() 或 Parallel.Invoke()。这里有一些进一步的解释 -> http:// /anyrest.wordpress.com/2010/09/09/parallel-programming-easier-than-ever-using-net-framework-4/

Just a suggestion, why not use .Net 4.0 Framework it has an easier threading implementation. Just use Parallel.For, Parallel.ForEach() or Parallel.Invoke(). Some further explanation here -> http://anyrest.wordpress.com/2010/09/09/parallel-programming-easier-than-ever-using-net-framework-4/

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