0

因此,我认为我有一个解决方案,可以在拥有具体类时获取 PropertyInfo,并为由具体类实现的接口获取 PropertyInfo。这是代码:

public static PropertyInfo GetImplementingProperty(Type concreteType, PropertyInfo interfaceProperty)
        {

            // do some region parameter check, skipped
            var interfaceType = interfaceProperty.DeclaringType;
            //use the set method if we have a write only property
            var getCorrectMethod = interfaceProperty.GetGetMethod() == null
                ? (Func<PropertyInfo, MethodInfo>) (p => p.GetSetMethod(true))
                : p => p.GetGetMethod(true);
            var propertyMethod = getCorrectMethod(interfaceProperty);
            var mapping = concreteType.GetInterfaceMap(interfaceType);

            MethodInfo targetMethod = null;
            for (var i = 0; i < mapping.InterfaceMethods.Length; i++)
            {
                if (mapping.InterfaceMethods[i] == propertyMethod)
                {
                    targetMethod = mapping.TargetMethods[i];
                    break;
                }
            }

            foreach (var property in concreteType.GetProperties(
                BindingFlags.Instance | BindingFlags.GetProperty |
                BindingFlags.Public | BindingFlags.NonPublic)) // include non-public!
            {
                if (targetMethod == getCorrectMethod(property)) // include non-public!
                {
                    return property;
                }
            }

            throw new InvalidOperationException("The property {0} defined on the interface {1} has not been found on the class {2}. That should never happen."
                .FormatText(interfaceProperty.Name, interfaceProperty.DeclaringType.FullName, concreteType.FullName));
        }

不幸的是,我发现了一个失败的案例,我不确定如何解决这个问题。所以我在一个 dll 中有一个类:

public abstract class BaseClass
{
    public Guid ConfigId { get; set; }
    public virtual Guid ConfigId2 { get; set; }
}

然后在另一个dll中我这样做:

    interface INamed
    {
        Guid ConfigId { get; }
        Guid ConfigId2 { get; }
    }

    private class SuperClass : BaseClass, INamed
    {
    }

现在

        ReflectionHelper.GetImplementingProperty(typeof(SuperClass), typeof(INamed).GetProperty("ConfigId2")); // this works
        ReflectionHelper.GetImplementingProperty(typeof(SuperClass), typeof(INamed).GetProperty("ConfigId")); // this fails

知道如何将 ConfigId 属性与基类属性定义匹配吗?

PS。我在具体的类属性上有属性,这就是我需要获取这些属性的原因。

任何帮助表示赞赏!

4

1 回答 1

0

您需要添加BindingFlags.FlattenHierarchy到您的 GetProperties 调用以获取父类属性。请参阅https://msdn.microsoft.com/en-us/library/kyaxdd3x(v=vs.110).aspx上的文档

于 2015-12-21T14:55:16.423 回答