英文:
How to pass StartsWith() as a parameter to Expression.Call()?
问题
I'm trying to call StartsWith() function as an expression and pass a constant to it.
var textConstant = Expression.Constant(text);
var startsWith = Expression.Call(StartsWith, null, textConstant); // something like this
I'm new to C# and I couldn't figure out how to pass these into Expression.Call().
英文:
Im trying to call StartsWith() function as an expression and pass a constant to it.
var textConstant =Expression.Constant(text);
var startsWith = Expression.Call(StartsWith ,textConstant); //something like this
Im new to c# and I couldn't figure out how to pass these into Expression.Call().
答案1
得分: -1
string text = "Some string";
string startsText = "Some";
Expression callExpr = Expression.Call(
Expression.Constant(text),
typeof(String).GetMethod("StartsWith", new Type[] { typeof(String) }),
Expression.Constant(startsText));
// 打印表达式
Console.WriteLine(callExpr);
// 执行表达式
var lambda = Expression.Lambda<Func
Console.WriteLine(lambda());
英文:
Here is sample:
string text = "Some string";
string startsText = "Some";
Expression callExpr = Expression.Call(
Expression.Constant(text),
typeof(String).GetMethod("StartsWith",
new Type[] { typeof(String) }),
Expression.Constant(startsText));
// print the expression
Console.WriteLine(callExpr);
// execute the expression
var lambda = Expression.Lambda<Func<bool>>(callExpr).Compile();
Console.WriteLine(lambda());
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论