c# – 派生类型的GetFields
发布时间:2020-12-15 05:37:30 所属栏目:百科 来源:网络整理
导读:我试图反映派生类型中的字段,但它返回的字段 基础类型. public class basetype{ string basevar;}public class derivedtype : basetype{ string derivedvar;} 在某些功能: derivedtype derived = new derivedtype();FieldInfo[] fields = derived.GetType()
我试图反映派生类型中的字段,但它返回的字段
基础类型. public class basetype { string basevar; } public class derivedtype : basetype { string derivedvar; } 在某些功能: derivedtype derived = new derivedtype(); FieldInfo[] fields = derived.GetType().GetFields(); 这将返回basevar,但不返回derivedvar.我已经尝试了所有不同的绑定,它似乎没有什么区别. 此外,我在App_Code中的ASP.NET中执行此操作,其中basevar在App_Code中定义,derivedvar是在App_Controls中定义的用户控件,其中类型不在范围内. 解决方法
因此,这将不返回任何内容,因为默认绑定仅适用于公共字段.
同样,衍生类型不是从基类派生的 附: FieldInfo[] fields = derived.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance); 它返回derivedvar.我刚刚检查过LINQPad. 如果我将derivedtype更改为从basetype派生,那么我可以使用以下两个字段获取: FieldInfo[] fields = derived.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance).Concat(derived.GetType().BaseType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance)).ToArray(); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |