开发者

Expression Tree with Property Inheritance causes an argument exception

开发者 https://www.devze.com 2022-12-23 15:37 出处:网络
Following this post: link text I\'m trying to create an expression tree that references the property of a property. My code looks like this:

Following this post: link text I'm trying to create an expression tree that references the property of a property. My code looks like this:

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));
        }
}

I receive the exception:

Syst开发者_运维问答em.ArgumentException: Instance property 'X' is not defined for type 'IBar'

ReSharper turned the code in the link above into the condensed statement in my example. Both forms of the method returned the same error.

If I reference IBar.Y the method does not fail.


The property you're trying to access is not IBar.X, it's IFoo.X. The Expression.Property method expects the actual type that declares the property, not a subtype. If you're not convinced, try that :

var prop = typeof(IBar).GetProperty("X");

It returns null (only because IBar is an interface ; it would work for a class)

I think the easiest way to make it work is to create a helper method to resolve the actual property, by walking up the type hierarchy recursively :

private PropertyInfo GetProperty(Type type, string propertyName)
{
    PropertyInfo prop = type.GetProperty(propertyName);
    if (prop == null)
    {
        var baseTypesAndInterfaces = new List<Type>();
        if (type.BaseType != null) baseTypesAndInterfaces.Add(type.BaseType);
        baseTypesAndInterfaces.AddRange(type.GetInterfaces());
        foreach(Type t in baseTypesAndInterfaces)
        {
            prop = GetProperty(t, propertyName);
            if (prop != null)
                break;
        }
    }
    return prop;
}

You can then rewrite your PropertyOfProperty as follows :

private static MemberExpression PropertyOfProperty(MemberExpression expr, string propertyName)
{
    return propertyName
               .Split('.')
               .Aggregate<string, MemberExpression>(
                   expr,
                   (current, property) =>
                       Expression.Property(
                           current,
                           GetProperty(current.Type, property)));
}
0

精彩评论

暂无评论...
验证码 换一张
取 消