Expression Tree with Property Inheritance causes an argument exception

馋奶兔 提交于 2019-12-05 17:49:57
Thomas Levesque

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)));
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!