C#中通过反射获取类中非公有成员
2016-05-10 16:28
543 查看
public class NGlbGlobeXComm
{
public static T GetPrivateField<T>(object instance, string fieldname)
{
BindingFlags flag = BindingFlags.Instance | BindingFlags.NonPublic;
Type type = instance.GetType();
FieldInfo field = type.GetField(fieldname, flag);
T to = default(T);
try
{
to = (T)field.GetValue(instance);
}catch(Exception e){}
return to;
}
}
public class NGlbFieldX : NObjectSafety, INGlbFieldX
{
private string mpr_org = null;
public NGlbFieldX()
{
mpr_org = new NGlbField();
}
}
如果需要获取NGlbFieldX中的mpr_org成员 使用如下语句
string fd = NGlbGlobeXComm.GetPrivateField<string>(field, "mpr_org");
即可得到。
参考:
一般而言,非公共成员是受保护的,不能被外部访问的,这些都是基于安全性考虑。可是有时,我们很想取到非公共成员的某个对象。那我们就得用到两个方法:
GetType().GetField();
GetType().GetProperty();
GetField()用来获取字段,GetProperty()用来获取属性。
示例:
如图,我要获取_row和Row的值。
深入:
如图,两者都有相似的参数类型,可以只输入要搜索的Name参数,也可以额外加入负责搜索执行方式的参数BindingAttr。
BindingAttr的参数大体如下(来自MSDN):
指定 BindingFlags.Public 可在搜索中包含公共字段。
指定 BindingFlags.NonPublic 可在搜索中包含非公共字段(即私有字段、内部字段和受保护的字段)。
指定 BindingFlags.FlattenHierarchy 以便沿层次结构向上包括public 和protected 静态成员;不包括继承类中的private 静态成员。
BindingFlags.IgnoreCase,表示忽略name 的大小写。
BindingFlags.DeclaredOnly,表示仅搜索在Type 上声明的字段,而不搜索简单继承的字段。
{
public static T GetPrivateField<T>(object instance, string fieldname)
{
BindingFlags flag = BindingFlags.Instance | BindingFlags.NonPublic;
Type type = instance.GetType();
FieldInfo field = type.GetField(fieldname, flag);
T to = default(T);
try
{
to = (T)field.GetValue(instance);
}catch(Exception e){}
return to;
}
}
public class NGlbFieldX : NObjectSafety, INGlbFieldX
{
private string mpr_org = null;
public NGlbFieldX()
{
mpr_org = new NGlbField();
}
}
如果需要获取NGlbFieldX中的mpr_org成员 使用如下语句
string fd = NGlbGlobeXComm.GetPrivateField<string>(field, "mpr_org");
即可得到。
参考:
一般而言,非公共成员是受保护的,不能被外部访问的,这些都是基于安全性考虑。可是有时,我们很想取到非公共成员的某个对象。那我们就得用到两个方法:
GetType().GetField();
GetType().GetProperty();
GetField()用来获取字段,GetProperty()用来获取属性。
示例:
如图,我要获取_row和Row的值。
protected void GV_Event_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "gvwEventLinkButton") { //_row 前面蓝色小图标,是变量(字段), 他是私有成员 //Row 前面白色小图标,是属性 ,他是保护乘员(白色图标上有个H的区域) //是字段还是属性,根据命名也能看出来 //根据调试时查看,Row类型是GridViewRow GridViewRow row = null; System.Reflection.FieldInfo fRow = e.GetType().GetField("_row", BindingFlags.Instance | BindingFlags.NonPublic); row = fRow.GetValue(e) as GridViewRow; int rowIndex = row.RowIndex; GridViewRow row2 = null; System.Reflection.PropertyInfo pRow = e.GetType().GetProperty("Row", BindingFlags.Instance | BindingFlags.NonPublic); row2 = pRow.GetValue(e, null) as GridViewRow; int rowIndex2 = row2.RowIndex; //如果RowIndex仍然是非公成员,可以同理对row进行反射操作 } }
深入:
如图,两者都有相似的参数类型,可以只输入要搜索的Name参数,也可以额外加入负责搜索执行方式的参数BindingAttr。
BindingAttr的参数大体如下(来自MSDN):
指定 BindingFlags.Public 可在搜索中包含公共字段。
指定 BindingFlags.NonPublic 可在搜索中包含非公共字段(即私有字段、内部字段和受保护的字段)。
指定 BindingFlags.FlattenHierarchy 以便沿层次结构向上包括public 和protected 静态成员;不包括继承类中的private 静态成员。
BindingFlags.IgnoreCase,表示忽略name 的大小写。
BindingFlags.DeclaredOnly,表示仅搜索在Type 上声明的字段,而不搜索简单继承的字段。
相关文章推荐
- windows server2008 64位 word 2007 C#生成word文档 提示:拒绝访问
- Windows系统上的.Net版本和.NETFramework的C#版本
- [C#]线程处理
- C# 之 判断或设置以管理员身份运行程序
- C# 复制PDF页面到另一个PDF文档
- C# 复制PDF页面到另一个PDF文档
- C# 的命名空间
- C#中winform使用相对路径读取文件的方法
- C# + WinForm + EmguCV 学习一:初步显示图像;
- C#学习笔记之Hashtable集合
- C# Stack源码剖析
- C# DateTime.Now.ToString("yyyy-MM-dd");
- 详解.net 中C# 编程中 堆(托管堆CLR) 和 栈
- C#学习笔记之ArrayList集合
- c# 一个记录日志的通用方法
- C#学习笔记之里式转换
- C#中 DataGridView 属性说明(转载)
- C#中消息处理机制(事件与委托)
- C#学习笔记之继承
- C#之委托、事件、lambda