反射 returns 只读属性的支持字段?
Reflection returns backing fields of read-only properties?
如果像这样调用的话,反射 return 似乎是属性的支持字段:
type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
有没有办法 return 用户在 class 中声明的所有字段没有任何支持字段/编译器生成的字段等?
编辑: 依赖 [CompilerGenerated]
属性是否安全?
所有此类字段都标有 CompilerGeneratedAttribute,因此您可以这样过滤:
var fields = type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
.Where(f => f.GetCustomAttribute<CompilerGeneratedAttribute>() == null).ToArray();
如果像这样调用的话,反射 return 似乎是属性的支持字段:
type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
有没有办法 return 用户在 class 中声明的所有字段没有任何支持字段/编译器生成的字段等?
编辑: 依赖 [CompilerGenerated]
属性是否安全?
所有此类字段都标有 CompilerGeneratedAttribute,因此您可以这样过滤:
var fields = type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
.Where(f => f.GetCustomAttribute<CompilerGeneratedAttribute>() == null).ToArray();