10

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

これは、Fooではなく、Barのバッキングフィールドのみを表示します。

4

3 に答える 3

11

バッキングフィールドであるフィールドは、反射に影響を与えません。バッキングフィールドの唯一の関連するプロパティは、それらがプライベートであるということです。

リフレクション関数は、を使用しても、基本クラスのプライベートメンバーを返しませんFlattenHierarchy。クラス階層を手動でループし、それぞれにプライベートフィールドを要求する必要があります。

FlattenHierarchyは、あなたが見ているクラスのコードに見えるすべてのメンバーを表示することを意図して書かれていると思います。そのため、ベースメンバーは、より派生したクラスの同じ名前のメンバーによって非表示/シャドウイングされる可能性があり、プライベートメンバーはまったく表示されません。

于 2012-02-08T21:38:58.413 に答える
7

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();
    }
}
于 2012-09-07T11:15:08.853 に答える
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<>を自由に使用してください。

編集:これは継承チェーンのさらに下のタイプを検索しないことにも注意してください。私の場合、メソッドを呼び出すときに最も派生したタイプであることがわかります。

于 2012-02-08T22:12:14.037 に答える