如何根据枚举的类型和其中一个字段的名称创建枚举的表达式?
How do you create an Expression of an enum from its type and the name of one of its fields?
很难找到我正在尝试做的事情,this post was the closest I could find. This post 不会工作,因为我不知道枚举的整数值,我只知道它的名字。给定以下代码:
public enum Foo
{
Row = 0,
Column = 20, // This is why the second post won't work, I only know the name "Column"
None = 30
}
public static class ExpressionGetter
{
public static Expression GetExpression(Type type, string name)
{
// Not sure what I should do here. I want an expression object for Foo.Row
}
}
void Main()
{
var expression = ExpressGetter.GetExpression(typeof(Foo), "Row");
}
稍后在我的应用程序中,我正在构建表达式树以生成 LINQ 查询,并且我知道 enum
的类型和 enum
的名称,现在我想创建一个 Expression.Constant
或者如果有其他方法可以做到这一点,我想知道怎么做。
最后我想要一个如下所示的表达式:
Foo.Row
我试过:
Expression.Property(null, enumType, name)
但是不行。结果
ArgumentException: Property 'Row' is not defined for type 'Foo'
Parameter name: propertyName
这是有道理的,因为它是结构而不是对象。
所以我不确定如何在给定枚举类型 Foo
和名称为字符串的情况下构建表达式 Foo.Row
。
大致是这样的:
public enum EnumerationTest
{
A, B, C
}
public class ClassTest
{
public EnumerationTest Test { get; set; }
}
public static Expression PropertyExpression()
{
// Think of this like a lambda (p) => p.Test == Enumeration.A
var parameter = Expression.Parameter(typeof(ClassTest), "p");
var property = Expression.PropertyOrField(parameter, "Test");
var value = (EnumerationTest)Enum.Parse(typeof(EnumerationTest), "A");
var constant = Expression.Constant(value, typeof(EnumerationTest));
return Expression.Equal(property, constant);
}
在做表达式树时,您通常会使用大量的反射和字符串解析。至少这是我在我的经验中发现的
枚举值是枚举类型的静态字段。如果您只有枚举值的名称作为字符串,则第二个版本就是您要查找的内容。但是您也可以对第一个版本执行 Enum.Parse() 。
Expression.Constant(Foo.Row, typeof(Foo));
// Or any other string that's valid
var name = "Row";
MemberExpression.Field(null, typeof(Foo), name);
很难找到我正在尝试做的事情,this post was the closest I could find. This post 不会工作,因为我不知道枚举的整数值,我只知道它的名字。给定以下代码:
public enum Foo
{
Row = 0,
Column = 20, // This is why the second post won't work, I only know the name "Column"
None = 30
}
public static class ExpressionGetter
{
public static Expression GetExpression(Type type, string name)
{
// Not sure what I should do here. I want an expression object for Foo.Row
}
}
void Main()
{
var expression = ExpressGetter.GetExpression(typeof(Foo), "Row");
}
稍后在我的应用程序中,我正在构建表达式树以生成 LINQ 查询,并且我知道 enum
的类型和 enum
的名称,现在我想创建一个 Expression.Constant
或者如果有其他方法可以做到这一点,我想知道怎么做。
最后我想要一个如下所示的表达式:
Foo.Row
我试过:
Expression.Property(null, enumType, name)
但是不行。结果
ArgumentException: Property 'Row' is not defined for type 'Foo' Parameter name: propertyName
这是有道理的,因为它是结构而不是对象。
所以我不确定如何在给定枚举类型 Foo
和名称为字符串的情况下构建表达式 Foo.Row
。
大致是这样的:
public enum EnumerationTest
{
A, B, C
}
public class ClassTest
{
public EnumerationTest Test { get; set; }
}
public static Expression PropertyExpression()
{
// Think of this like a lambda (p) => p.Test == Enumeration.A
var parameter = Expression.Parameter(typeof(ClassTest), "p");
var property = Expression.PropertyOrField(parameter, "Test");
var value = (EnumerationTest)Enum.Parse(typeof(EnumerationTest), "A");
var constant = Expression.Constant(value, typeof(EnumerationTest));
return Expression.Equal(property, constant);
}
在做表达式树时,您通常会使用大量的反射和字符串解析。至少这是我在我的经验中发现的
枚举值是枚举类型的静态字段。如果您只有枚举值的名称作为字符串,则第二个版本就是您要查找的内容。但是您也可以对第一个版本执行 Enum.Parse() 。
Expression.Constant(Foo.Row, typeof(Foo));
// Or any other string that's valid
var name = "Row";
MemberExpression.Field(null, typeof(Foo), name);