0

I have got a variable which contains function hierarchy like:

string str= "fun1(fun2(),fun3(fun4(fun5(34,33,'aa'),'value',fun6()))" 

// this hierarchy is coming as a string from database

I have imported System.reflection and used invoke method to invoke it, but it's only working if I have a only one function fun1.

With above function hierarchy it's taking complete expression as a one function name.

I am using this below code to invoke my function hierarchy:

public static string InvokeStringMethod(string typeName, string methodName)
{
// Get the Type for the class
Type calledType = Type.GetType(typeName);

// Invoke the method itself. The string returned by the method winds up in s
String s = (String)calledType.InvokeMember(
                methodName,
                BindingFlags.InvokeMethod | BindingFlags.Public | 
                    BindingFlags.Static,
                null,
                null,
                null);

// Return the string that was returned by the called method.
return s;
}  

Reference: http://www.codeproject.com/KB/cs/CallMethodNameInString.aspx

Please tell me what should I do?

4

1 回答 1

1

问题是线路

string str= fun1(fun2(),fun3(fun4(fun5(34,33,'aa'),'value',fun6()));

不代表表达式,或者您所说的“函数层次结构”。相反,它执行赋值的右侧,计算为字符串值。

您可能正在寻找的是:

Func<string> f = () => fun1(fun2(),fun3(fun4(fun5(34,33,'aa'),'value',fun6()));
…
string result = f();

在这里,“f”是一个委托,您可以在其中分配一个 lambda 表达式(匿名方法),稍后可以通过调用该委托来执行该表达式f

于 2010-10-12T11:48:29.953 回答