我不确定我想要实现的究竟是如何论证的,因此 - 写了一些代码:
public class Foo{
public Bar Bar{get;set;}
}
public class Bar{
public string Fizz{get;set;}
}
public class Facts{
[Fact]
public void fact(){
Assert.Equal(expectedExp(),barToFoo(barExp()));
}
private Expression<Func<Foo,bool>> expectedExp(){
return f=>f.Bar.Fizz=="fizz";
}
private Expression<Func<Bar,bool>> barExp(){
return b=>b.Fizz=="fizz";
}
private Expression<Func<Foo,bool>> barToFoo
(Expression<Func<Bar,bool>> barExp){
return Voodoo(barExp); //<-------------------------------------------???
}
}
这甚至可能吗?
最佳答案
public class Foo
{
public Bar Bar { get; set; }
}
public class Bar
{
public string Fizz { get; set; }
}
private Expression<Func<Bar, bool>> BarExp()
{
return b => b.Fizz == "fizz";
}
private Expression<Func<Foo, bool>> BarToFoo(Expression<Func<Bar, bool>> barExp)
{
Expression<Func<Foo, Bar>> barMemberAccessExpression = foo => foo.Bar;
var fooParam = Expression.Parameter(typeof(Foo), "foo");
var invokeExpression = Expression.Invoke(
barExp,
Expression.Invoke(barMemberAccessExpression, fooParam)
);
var result = Expression.Lambda<Func<Foo, bool>>(invokeExpression, fooParam);
return result;
//Expression<Func<Foo, bool>> result =
// foo => barExp.Compile().Invoke(foo.Bar);
//return result;
}
[Test]
public void TestBarToFoo()
{
var fooInstance = new Foo { Bar = new Bar() { Fizz = "fizz" } };
var barExpr = this.BarExp();
Console.WriteLine(
barExpr.Compile().Invoke(fooInstance.Bar)); // = True
Console.WriteLine(
this.BarToFoo(barExpr).Compile().Invoke(fooInstance)); // = True
}
关于c# - Linq 表达式的转换,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4646374/