VB.NET: How to convert from a string to method name?

时间:2016-10-19 13:36:45

标签: vb.net multithreading

I have my method name as a string(Eventually going to read from a file). For now the method can be accessed by AIModule.Main, then it is going to be run as a different thread. So far I have this:

Dim MyMethod As System.Reflection.MethodInfo = Me.GetType().GetMethod("AIModule.Main")
AIThread = New System.Threading.Thread(AddressOf MyMethod)

But this doesn't work, as the AddressOf operator only wants names. How would I make it such that I get the method name or indeed the method address?

Thank you.

1 个答案:

答案 0 :(得分:0)

预先充分披露这是从Jon Skeet在一个不同的论坛中回答类似问题的帖子中提取的,他提出的解决这个问题的方法是以这样的方式创建一个ThreadStart委托( C#source here):

ThreadStart ts = (ThreadStart) Delegate.CreateDelegate
 (typeof (ThreadStart), methodInfo);

来自你的来源的一个粗略的VB改编可能看起来像(警告 - 未经测试! - 用于说明):

Dim MyThreadStart as ThreadStart = Delegate.CreateDelegate(GetType(ThreadStart), MyMethod) as ThreadStart

看起来你手头有这些基本的东西,可以相应地调整这个解决方案。

Original thread with Jon Skeet's discussion here