【发布时间】:2010-03-22 22:35:26
【问题描述】:
在这篇文章之后:link text 我正在尝试创建一个引用属性属性的表达式树。我的代码如下所示:
public interface IFoo
{
void X {get;set;}
}
public interface IBar : IFoo
{
void Y {get;set;}
}
public interface IFooBarContainer
{
IBar Bar {get;set;}
}
public class Filterer
{
//Where T = "IFooBarContainer"
public IQueryable<T> Filter<T>(IEnumerable<T> collection)
{
var argument = Expression.Parameter(typeof (T), "item");
//...
//where propertyName = "IBar.X";
PropertyOfProperty(argument, propertyName);
}
private static MemberExpression PropertyOfProperty(Expression expr, string propertyName)
{
return propertyName.Split('.').Aggregate<string, MemberExpression>(null, (current, property) => Expression.Property(current ?? expr, property));
}
}
我收到异常:
System.ArgumentException:实例 没有为类型定义属性“X” 'IBar'
ReSharper 将上面链接中的代码转换为我示例中的简明语句。两种形式的方法都返回相同的错误。
如果我引用IBar.Y,该方法不会失败。
【问题讨论】:
标签: .net linq expression-trees