/// <summary> /// 获取值 /// </summary> /// <param name="expression">表达式</param> public object GetValue(Expression expression) { if (expression == null) { return(null); } var result = Lambda.GetValue(expression); if (result == null) { return(null); } var type = result.GetType(); if (type.IsEnum) { return(Enum.GetValue(type, result)); } return(result); }
public void TestGetValue_Enum() { var test1 = new Sample { NullableEnumValue = EnumSample.C }; Expression <Func <Sample, bool> > expression = test => test.EnumValue == EnumSample.D; Assert.Equal(EnumSample.D.Value(), Enum.GetValue <EnumSample>(Lambdas.GetValue(expression))); expression = test => test.EnumValue == test1.NullableEnumValue; Assert.Equal(EnumSample.C, Lambdas.GetValue(expression)); expression = test => test.NullableEnumValue == EnumSample.E; Assert.Equal(EnumSample.E, Lambdas.GetValue(expression)); expression = test => test.NullableEnumValue == test1.NullableEnumValue; Assert.Equal(EnumSample.C, Lambdas.GetValue(expression)); test1.NullableEnumValue = null; expression = test => test.NullableEnumValue == test1.NullableEnumValue; Assert.Null(Lambdas.GetValue(expression)); }