0

次のような関数階層を含む変数があります。

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

//この階層はデータベースから文字列として取得されます

System.reflectionをインポートし、invokeメソッドを使用して呼び出しましたが、関数が1つしかない場合にのみ機能しますfun1

上記の関数階層では、完全な式を1つの関数名として使用しています。

以下のコードを使用して、関数階層を呼び出しています。

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;
}  

参照: http: //www.codeproject.com/KB/cs/CallMethodNameInString.aspx

どうしたらいいか教えてください。

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」は、デリゲートを呼び出すことで後で実行できるラムダ式(匿名メソッド)を割り当てるデリゲートfです。

于 2010-10-12T11:48:29.953 に答える