为什么不';t Type.GetFields()返回基类中的后备字段
本文关键字:基类 返回 字段 GetFields Type 为什么不 | 更新日期: 2023-09-27 18:23:54
在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。
作为后备字段的字段对反射没有影响。支持字段的唯一相关属性是它们是私有的。
即使使用FlattenHierarchy
,反射函数也不会返回基类的私有成员。您需要在类层次结构上手动循环,并在每个层次结构上请求私有字段。
我认为FlattenHierarchy
的编写目的是显示所查看类中代码可见的所有成员。因此,在更派生的类中,基础成员可以被同名成员隐藏/隐藏,而私有成员根本不可见。
以下是使用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();
}
}
感谢@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<>相反
编辑:还要注意,这不会在继承链中进一步搜索类型。在我的情况下,我知道在调用该方法时,我是派生类型最多的。