给定Msdn:constant-expression是一个可以在编译时完全评估的表达式。
但是在下面的示例代码中,我有一个无法在编译时评估的contantExpression。
我应该错过什么,但是什么?
public class SomeClass
{
public string Key { get; set; }
}
public static void Sample()
{
var wantedKey = Console.ReadLine();
Expression<Func<SomeClass, bool>> expression = c => c.Key == wantedKey;
var maybeAConstantExpression = ((MemberExpression)((BinaryExpression)expression.Body).Right).Expression;
//Both are true, so we have a constantExpression,righ and Value should be known
Console.WriteLine(maybeAConstantExpression.NodeType == ExpressionType.Constant);
Console.WriteLine(maybeAConstantExpression.GetType() == typeof(ConstantExpression));
var constantExpression = ((ConstantExpression)maybeAConstantExpression);
var constantValue = constantExpression.Value;
//ConsoleApplication1.Program+<>c__DisplayClass0
//Do not looks like a constant..this is a class...
Console.WriteLine(constantValue);
var fakeConstantValue = constantValue.GetType().GetField("wantedKey").GetValue(constantValue);
//Return the value entered whith Console.ReadLine
//This is not really known at compile time...
Console.WriteLine(fakeConstantValue);
}
答案 0 :(得分:5)
const
可以在编译时进行评估。仅ConstantExpression
仅表示固定值。这可能来自编译时,但不是必须的,而且往往不是。
常量表达式(在C#语言意义上)和ConstantExpression
(运行时对象)之间存在差异。
您的案例中的constantValue
代表捕获上下文 - 提升wantedKey
的静默类。基本上,该代码是(通过编译器):
class HorribleNameThatYouCannotSay {
public string wantedKey; // yes, a public field
}
...
static void Sample()
{
var ctx = new HorribleNameThatYouCannotSay();
ctx.wantedKey = Console.ReadLine();
var p = Expression.Parameter(typeof(SomeClass), "c");
Expression<Func<SomeClass, bool>> expression =
Expression.Lambda<Func<SomeClass, bool>>(
Expression.Equal(
Expression.PropertyOrField(p, "Key"),
Expression.PropertyOrField(
Expression.Constant(ctx), "wantedKey")
), p);
);
}
或非常接近的事情
但它可能只是:
string wantedKey = Console.ReadLine();
var p = Expression.Parameter(typeof(SomeClass), "c");
Expression<Func<SomeClass, bool>> expression =
Expression.Lambda<Func<SomeClass, bool>>(
Expression.Equal(
Expression.PropertyOrField(p, "Key"),
Expression.Constant(wantedKey, typeof(string))
), p);
如果您使用Expresion
API手动编写(例如上述内容)