在 system.reflection 的调用方法中将函数作为参数传递
我有一个包含函数层次结构的变量,如下所示:
string str= "fun1(fun2(),fun3(fun4(fun5(34,33,'aa'),'value',fun6()))"
// 这个层次结构作为来自数据库的字符串而来
我已导入 System.reflection 并使用 invoke 方法来调用它,但只有当我只有一个函数 时它才有效乐趣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
请告诉我该怎么办?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是该行
不代表表达式,或者您所说的“函数层次结构”。相反,它会将赋值的右侧求值执行为字符串值。
您可能正在寻找的是这样的:
这里,“f”是一个委托,您可以向其中分配一个 lambda 表达式(匿名方法),稍后可以通过调用委托
f
来执行该表达式。The problem is the line
does not represent an expression, or what you call a 'function hierarchy'. Instead, it executes the right-hand side of the assignment evaluates into a string value.
What you are probably looking for is this:
Here, ´f´ is a delegate into which you assign a lambda expression (anonymous method) that can later be executed by invoking the delegate
f
.