public void TestGetValue_Complex() { var test = new Test1() { Name = "a", A = new Test1.TestA() { Address = "b" } }; //获取表达式的值 Expression<Func<Test1, bool>> expression = t => t.Name == test.Name; Assert.AreEqual( "a", Lambda.GetValue( expression ), "==test.Name" ); Expression<Func<Test1, bool>> expression2 = t => t.Name == test.A.Address; Assert.AreEqual( "b", Lambda.GetValue( expression2 ), "==test.A.Address" ); //获取方法的值 Expression<Func<Test1, bool>> expression3 = t => t.Name.Contains( test.Name ); Assert.AreEqual( "a", Lambda.GetValue( expression3 ), "Contains test.Name" ); Expression<Func<Test1, bool>> expression4 = t => t.Name.Contains( test.A.Address ); Assert.AreEqual( "b", Lambda.GetValue( expression4 ), "==test.A.Address" ); }
public void TestGetValue_Enum() { var test1 = new Test1(); test1.NullableEnumValue = LogType.Error; //属性为枚举,值为枚举 Expression<Func<Test1, bool>> expression = test => test.EnumValue == LogType.Debug; Assert.AreEqual( LogType.Debug.Value(), Util.Enum.GetValue< LogType>( Lambda.GetValue( expression )) ); //属性为枚举,值为可空枚举 expression = test => test.EnumValue == test1.NullableEnumValue; Assert.AreEqual( LogType.Error, Lambda.GetValue( expression ) ); //属性为可空枚举,值为枚举 expression = test => test.NullableEnumValue == LogType.Debug; Assert.AreEqual( LogType.Debug, Lambda.GetValue( expression ) ); //属性为可空枚举,值为可空枚举 expression = test => test.NullableEnumValue == test1.NullableEnumValue; Assert.AreEqual( LogType.Error, Lambda.GetValue( expression ) ); //属性为可空枚举,值为null test1.NullableEnumValue = null; expression = test => test.NullableEnumValue == test1.NullableEnumValue; Assert.AreEqual( null, Lambda.GetValue( expression ) ); }
public void TestGetValue_Instance() { var test = new Test1() { Name = "a", A = new Test1.TestA() { Address = "b", B = new Test1.TestA.TestB() { Name = "c" } } }; //一级属性 Expression<Func<string>> expression = () => test.Name; Assert.AreEqual( "a", Lambda.GetValue( expression ) ); //二级属性 Expression<Func<string>> expression2 = () => test.A.Address; Assert.AreEqual( "b", Lambda.GetValue( expression2 ) ); //三级属性 Expression<Func<string>> expression3 = () => test.A.B.Name; Assert.AreEqual( "c", Lambda.GetValue( expression3 ) ); }