为什么Type.GetFields()不返回基类中的后备字段?

时间:2012-02-08 21:37:34

标签: c# reflection properties backing-field

在C#中,如果使用Type.GetFields()表示派生类的类型,它将返回a)派生类中所有显式声明的字段,b)派生类中自动属性的所有后备字段和c )基类中所有显式声明的字段。

为什么缺少基类中自动属性的d)支持字段?

示例:

public class Base {
    public int Foo { get; set; }
}
public class Derived : Base {
    public int Bar { get; set; }
}
class Program {
    static void Main(string[] args) {
        FieldInfo[] fieldInfos = typeof(Derived).GetFields(
            BindingFlags.Public | BindingFlags.NonPublic |
            BindingFlags.Instance | BindingFlags.FlattenHierarchy
        );
        foreach(FieldInfo fieldInfo in fieldInfos) {
            Console.WriteLine(fieldInfo.Name);
        }
    }
}

这将仅显示Bar的支持字段,而不是Foo。

3 个答案:

答案 0 :(得分:8)

作为支持字段的字段对反射没有影响。支持字段的唯一相关属性是它们是私有的。

即使您使用FlattenHierarchy,反射函数也不会返回基类的私有成员。您需要在类层次结构上手动循环,并在每个层次上请求私有字段。

我认为编写FlattenHierarchy的目的是为了显示所看到的类中代码可见的所有成员。因此,基础成员可以被更多派生类中具有相同名称的成员隐藏/隐藏,并且私有成员根本不可见。

答案 1 :(得分:5)

以下是使用HashSet的修订版:

public static FieldInfo[] GetFieldInfosIncludingBaseClasses(Type type, BindingFlags bindingFlags)
{
    FieldInfo[] fieldInfos = type.GetFields(bindingFlags);

    // If this class doesn't have a base, don't waste any time
    if (type.BaseType == typeof(object))
    {
        return fieldInfos;
    }
    else
    {   // Otherwise, collect all types up to the furthest base class
        var currentType = type;
        var fieldComparer = new FieldInfoComparer();
        var fieldInfoList = new HashSet<FieldInfo>(fieldInfos, fieldComparer);
        while (currentType != typeof(object))
        {
            fieldInfos = currentType.GetFields(bindingFlags);
            fieldInfoList.UnionWith(fieldInfos);
            currentType = currentType.BaseType;
        }
        return fieldInfoList.ToArray();
    }
}

private class FieldInfoComparer : IEqualityComparer<FieldInfo>
{
    public bool Equals(FieldInfo x, FieldInfo y)
    {
        return x.DeclaringType == y.DeclaringType && x.Name == y.Name;
    }

    public int GetHashCode(FieldInfo obj)
    {
        return obj.Name.GetHashCode() ^ obj.DeclaringType.GetHashCode();
    }
}

答案 2 :(得分:1)

感谢@CodeInChaos快速完整的答案!

如果有其他人偶然发现这一点,这里有一个快速的解决方法,跟随字段到最远的基类。

/// <summary>
///   Returns all the fields of a type, working around the fact that reflection
///   does not return private fields in any other part of the hierarchy than
///   the exact class GetFields() is called on.
/// </summary>
/// <param name="type">Type whose fields will be returned</param>
/// <param name="bindingFlags">Binding flags to use when querying the fields</param>
/// <returns>All of the type's fields, including its base types</returns>
public static FieldInfo[] GetFieldInfosIncludingBaseClasses(
    Type type, BindingFlags bindingFlags
) {
    FieldInfo[] fieldInfos = type.GetFields(bindingFlags);

    // If this class doesn't have a base, don't waste any time
    if(type.BaseType == typeof(object)) {
        return fieldInfos;
    } else { // Otherwise, collect all types up to the furthest base class
        var fieldInfoList = new List<FieldInfo>(fieldInfos);
        while(type.BaseType != typeof(object)) {
            type = type.BaseType;
            fieldInfos = type.GetFields(bindingFlags);

            // Look for fields we do not have listed yet and merge them into the main list
            for(int index = 0; index < fieldInfos.Length; ++index) {
                bool found = false;

                for(int searchIndex = 0; searchIndex < fieldInfoList.Count; ++searchIndex) {
                    bool match =
                        (fieldInfoList[searchIndex].DeclaringType == fieldInfos[index].DeclaringType) &&
                        (fieldInfoList[searchIndex].Name == fieldInfos[index].Name);

                    if(match) {
                        found = true;
                        break;
                    }
                }

                if(!found) {
                    fieldInfoList.Add(fieldInfos[index]);
                }
            }
        }

        return fieldInfoList.ToArray();
    }
}

请注意,我手动比较嵌套for循环中的字段。如果您有深层嵌套的类或怪异的大类,请随意使用HashSet&lt;&gt;代替。

编辑:还要注意,这不会在继承链中进一步搜索类型。在我的情况下,我知道在调用方法时我处于派生类型最多的位置。