System.Reflection.MethodInfo.Invoke 和多线程
您好,我如何使用带有线程的参数调用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于您想要使用 System.Threading.Thread 创建新线程,而不是在现有 UI 线程或线程池线程上进行调用,因此首先要注意的是使用 System.Threading .Thread 您可以使用
ThreadStart
或ParameterizedThreadStart
委托。您确实需要为线程的 main 方法提供参数,但 ParameterizedThreadStart 只允许使用对象,这会强制您将其强制转换为所需的类型。因此,我们将使用闭包来获取以类型安全的方式传递的所有参数。
用法示例:
如果您正在使用 .NET 2.0,则从参数列表中取出关键字
this
并调用:这将丢弃任何返回值,但您问题中的无线程示例也是如此。如果您需要返回值,请发表评论。
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 withSystem.Threading.Thread
you can use either aThreadStart
orParameterizedThreadStart
delegate.You do want parameters to your thread's main method, but
ParameterizedThreadStart
only allows anobject
, 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.Example usage:
If you're working with .NET 2.0, then take out the keyword
this
from the parameter list and call like:This will discard any return value, but so did the unthreaded example in your question. If you need the return value leave a comment.
您可以使用匿名方法启动线程:
delegate() { ... }
内的代码是在新线程上执行的匿名方法。You can start a thread with an anonymous method:
The code inside the
delegate() { ... }
is an anonymous method that is executed on the new thread.只是一个建议,为什么不使用 .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/