本文介绍了如何使用反射获取私有类成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我正在尝试使用反射让一些私人班级成员,但我有麻烦:例如 使用System; 使用System.Reflection; 公共类客户 { 私人字符串_fname; 公共字符串FirstName { get {return _fname;} 设置{_fname = value;} } private void GetMyMembers() { 类型t = this.GetType(); MemberInfo [] mInfo = t.GetMembers(); } foreach(mInfo中的MemberInfo m) { members + = m.Name +" " ;; Console.WriteLine(m.Name); } } 我的输出显示 get_FirstName set_FirstName 我想要的是_fname。 有什么想法吗? 谢谢, Opa 解决方案 这将为你完成这项工作: 类型t = this.GetType(); MemberInfo [] members = t.GetMembers(BindingFlags.NonPublic | BindingFlags.Instance); foreach(会员中的MemberInfo会员) { Console.WriteLine(member.Name); } Eyal。 这应该为你做的工作 类型t = this.GetType(); MemberInfo [] members = t.GetMembers(BindingFlags.NonPublic | BindingFlags.Instance); foreach(会员中的MemberInfo会员) { Console.WriteLine(mem ber.Name); } 干杯, Eyal。 您好, 这应该为您完成工作 类型t = this.GetType(); MemberInfo [] members = t.GetMembers(BindingFlags.NonPublic | BindingFlags.Instance); foreach(会员中的MemberInfo会员) { Console.WriteLine(member.Name); } 干杯, Eyal。 Hi,I''m trying to get some private class members using reflection, but am havingtrouble: Exampleusing System;using System.Reflection;public class Customer{private string _fname;public string FirstName{get{return _fname;}set{_fname = value;}}private void GetMyMembers (){Type t = this.GetType();MemberInfo[] mInfo = t.GetMembers();}foreach(MemberInfo m in mInfo){members += m.Name + " ";Console.WriteLine(m.Name);}}my output showsget_FirstNameset_FirstNamewhat I want is _fname.Any ideas?Thanks,Opa 解决方案 Hi,This will do the job for you:Type t = this.GetType();MemberInfo[] members = t.GetMembers(BindingFlags.NonPublic |BindingFlags.Instance);foreach(MemberInfo member in members){Console.WriteLine(member.Name);}Eyal. Hi,This should do the job for youType t = this.GetType();MemberInfo[] members = t.GetMembers(BindingFlags.NonPublic |BindingFlags.Instance);foreach(MemberInfo member in members){Console.WriteLine(member.Name);}Cheers,Eyal. Hi,This should do the job for youType t = this.GetType();MemberInfo[] members = t.GetMembers(BindingFlags.NonPublic |BindingFlags.Instance);foreach(MemberInfo member in members){Console.WriteLine(member.Name);}Cheers,Eyal. 这篇关于如何使用反射获取私有类成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-12 19:55