我正在表达式树中做一些工作。当你在表达式树上调用ToString()时,你会得到一段可爱的诊断文本(这里是一个示例):
((Param_0.Customer.LastName == "Doe")
AndAlso ((Param_0.Customer.FirstName == "John")
Or (Param_0.Customer.FirstName == "Jane")))
所以我写了这段代码,试图用一些日志记录功能来包装表达式:
public Expression WithLog(Expression exp)
{
return Expression.Block(exp, Expression.Call(
typeof (Debug).GetMethod("Print",
new Type [] { typeof(string) }),
new [] { exp } ));
}
我有点期待方法调用能推断ToString()的用法,但我认为这是一个编译时特性。当我执行此操作时,我会得到错误:
"System.Boolean"类型的表达式不能用于方法"Void Print(System.String)"的"System.String"类型的参数
很公平。但当我把它改成这个:
public Expression WithLog(Expression exp)
{
return Expression.Block(exp, Expression.Call(
typeof (Debug).GetMethod("Print",
new Type [] { typeof(string) }),
new [] { exp.ToString() } ));
}
它不可编译。为什么?我需要做些什么来解决这个问题?
根据我的评论,它应该是Expression[]
,但您已经传递了string[]
。您可以这样做,这将立即在exp
:上运行ToString()
public Expression WithLog(Expression exp)
{
return Expression.Block(Expression.Call(
typeof (Debug).GetMethod("Print",
new Type [] { typeof(string) }),
new [] { Expression.Constant(exp.ToString()) } ), exp);
}
哪个收益率:
Print("c => ((c.LastName == "Doe") AndAlso ((c.FirstName == "John") OrElse (c.LastName == "Jane")))")
或者,您可以将Expression.Constant(exp.ToString())
更改为exp
上ToString
的调用,以便在调用表达式时执行ToString
。
public Expression WithLog(Expression exp)
{
return Expression.Block(Expression.Call(
typeof (Debug).GetMethod("Print",
new Type [] { typeof(string) }),
new [] { Expression.Call(Expression.Constant(exp), exp.GetType().GetMethod("ToString")) } ), exp);
}
哪个给出:
Print(c => ((c.LastName == "Doe") AndAlso ((c.FirstName == "John") OrElse (c.LastName == "Jane"))).ToString())