我正在尝试执行一个名称存储在字符串中的方法... 例如
dim a as integer = warehouse.getTotalCount("item1") 'a is now equal to 100
但我想做的就是这样......
dim item1 as string = "item1"
dim fctn = "warehouse.getTotalCount("+item1+")"
dim a as integer = run(fctn) 'and here a should be equal to 100
我一直试图弄清楚这一段时间......
这些函数可能来自几个不同的类,我想运行完全限定的名称。 如果不使用案例选择,我该怎么做?
编辑:
我遇到了麻烦Type.GetType("warehouse")
或
Type.GetType("fctn")
答案 0 :(得分:2)
这是从MethodBase.Invoke获取的,应该让您大致了解如何操作,它在C#中可以轻松转换:
Type magicType = Type.GetType("MagicClass");
ConstructorInfo magicConstructor = magicType.GetConstructor(Type.EmptyTypes);
object magicClassObject = magicConstructor.Invoke(new object[]{});
// Get the ItsMagic method and invoke with a parameter value of 100
MethodInfo magicMethod = magicType.GetMethod("ItsMagic");
object magicValue = magicMethod.Invoke(magicClassObject, new object[]{100});
尝试这样的事情:
Dim myMethod = wareHouse.GetType().GetMethod("getTotalCount")
Dim res = myMethod.Invoke(wareHouse, New Object() {100})