本文介绍了使用反射获取基类的字段信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我的UI程序集中存在一个类,它继承自这样的引用程序集中的类 。 类EmployeesDC { 私人字符串mstrEmployeeNumber; 等 } 类EmployeesBL:EmployeesDC { } 然后在UI组装 班员工:员工BL { } 有人可以给我代码,这样我就可以在EmployeesDC上使用反射获取私人领域的信息 。 我用google搜索但我还不清楚?? 谢谢 rotseyHi,I have a class that exist in my UI assembly that inherits from a classin a referenced assembly like this.class EmployeesDC{private string mstrEmployeeNumber;etc}class EmployeesBL : EmployeesDC{}Then in UI Assemblyclass Employees : EmployeesBL{}Can someone give me the code so I can get info on the private fieldsin EmployeesDC using reflection.I have googled but I am still not clear how??thanksrotsey推荐答案 你好rotsey, 它不是太复杂了,但我可以问你之后是什么类型的信息?b 如果你是在该领域的内容之后,那么有没有理由 为什么你不能宣称它是受保护的? 如果你在其他事情之后,让我们知道,我们会指出你正确的方向 方向。 - Tom Spink 爱丁堡大学Hi rotsey,It''s not too complicated, but may I ask what type of information you''reafter? If you''re after the contents of the field, then is there a reasonwhy you can''t declare it as protected?If you''re after something else, let us know and we''ll point you in the rightdirection.--Tom SpinkUniversity of Edinburgh 你好rotsey , 这不是太复杂,但我可以问你之后是什么类型的信息?b $ b如果你是在该领域的内容之后,那么有没有理由 为什么你不能宣称它是受保护的? 如果你在其他事情之后,请告诉我们,我们会在 右边 方向指出你。 - Tom Spink 爱丁堡大学Hi rotsey,It''s not too complicated, but may I ask what type of information you''reafter? If you''re after the contents of the field, then is there a reasonwhy you can''t declare it as protected?If you''re after something else, let us know and we''ll point you in therightdirection.--Tom SpinkUniversity of Edinburgh 你好rotsey , 这不是太复杂,但我可以问你之后是什么类型的信息?如果你是在该领域的内容之后,那么有一个理由为什么你不能宣称它是受保护的? 如果你在追求别的东西,让我们我们知道,我们会指出你的 方向。 - Tom Spink 爱丁堡大学 Hi rotsey,It''s not too complicated, but may I ask what type of information you''reafter? If you''re after the contents of the field, then is there a reasonwhy you can''t declare it as protected?If you''re after something else, let us know and we''ll point you in therightdirection.--Tom SpinkUniversity of Edinburgh 嗨Rotsey, /// 使用System; 使用System.Reflection; 公共舱A { 私人字符串pfa_A; 私人字符串pfb_A; 私人字符串pfc_A; } 公共舱B:A { private string pfa_B; private string pfb_B; private string pfc_B; } public C级:B { public void Foo() { 类型t = this.GetType( ); BindingFlags bf = BindingFlags.Default; bf | = BindingFlags.NonPublic; bf | = BindingFlags.FlattenHierarchy ; bf | = BindingFlags.Instance; foreach(t.GetFields(bf)中的FieldInfo字段) { Console.WriteLine(字段); } } } 公共课E { public static void Main() { C c = new C(); c.Foo(); } } /// 这里发生的事情最有趣的是C的Foo()方法。 继承层次结构是C -B -A,当你在C中调用Foo()时,它将打印出它找到的字段列表。 修改bf(BindingFlags参数)将有助于优化/扩展 搜索。 字段变量(在foreach语句的每次迭代中) )(最可能是)包含你需要知道的关于这个领域的一切。 - Tom Spink 爱丁堡大学Hi Rotsey,Cool. Take a look at the following example:///using System;using System.Reflection;public class A{private string pfa_A;private string pfb_A;private string pfc_A;}public class B : A{private string pfa_B;private string pfb_B;private string pfc_B;}public class C : B{public void Foo (){Type t = this.GetType();BindingFlags bf = BindingFlags.Default;bf |= BindingFlags.NonPublic;bf |= BindingFlags.FlattenHierarchy;bf |= BindingFlags.Instance;foreach (FieldInfo field in t.GetFields(bf)){Console.WriteLine(field);}}}public class E{public static void Main (){C c = new C();c.Foo();}}///What''s going on here is most interesting in the Foo() method of C. Theinheritance hierarchy is C -B -A, and when you call Foo() in C, it willprint out a list of fields it finds.Modifying bf (the BindingFlags parameter) will help to refine/expand thesearch.The field variable (on each iteration of the foreach statement) will (mostlikely) contain everything you need to know about the field.--Tom SpinkUniversity of Edinburgh 这篇关于使用反射获取基类的字段信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-26 16:39