赋值表达式,用于设置动态创建的枚举值
本文关键字:创建 枚举 动态 设置 表达式 用于 赋值 | 更新日期: 2023-09-27 18:14:41
我有以下枚举:
[Flags]
public enum MyEnum
{
None = 0,
Value1 = 1,
Value2 = 2,
}
此枚举是使用TypeBuilder
动态创建的,因此在编译时不会将其作为强类型枚举引用。
// The variable enumBuilder is fully created and ready for use.
var code = Expression.Variable(enumBuilder, "code");
我现在需要将MyEnum.Value2
赋值给变量code
。
// The following line is supposed to do this: code = MyEnum.Value2;
var assign = Expression.Assign(code, ?????);
如何创建计算为MyEnum.Value2
的表达式的右侧?
Type enumType = enumBuilder.CreateType();
var assign = Expression.Assign(code, Expression.Constant(2, enumType));