0

我正在尝试创建一个程序,该程序将通过使用用户提供的子名称动态调用方法来执行命令。在帮助下,我已经设法使用字符串字典和 Func() 或 Action() lambda 表达式来链接到各种子程序,但我想使它能够根据方法名称而不需要单独的字典。

在 C# 中建议了类似这样的结构:

Type type = calcobject.GetType();
MethodInfo method = type.GetMethod(RoutineName);
if (method != null)
{
    method.Invoke(this, ...parameters...);
}

它使用了一类子类和函数。我已经设法让代码在 VB 中工作......

Dim Params() As String = {3, 4}
Dim Type As Type = GetType(
Dim Method As MethodInfo = Type.GetMethod("Add")
Dim Result As Integer
If Method IsNot Nothing Then
    Result = Method.Invoke(Method, Params)
End If
Console.WriteLine(Result)

...但我想使用一种不依赖于类的方法,它可以委托给主模块中任何地方的方法。到目前为止我尝试过的一切都没有奏效 - 但我真的不知道我在做什么。有没有办法可以修改上面的代码以允许它引用不在类中的方法?

4

0 回答 0