C#使用反射机制获取类信息[转]
http://www.cnblogs.com/zhoufoxcn/archive/2006/10/31/2515873.html
1.用反射动态创建类实例,并调用其公有成员函数。
//新建一个类库项目,增加一个GetSum方法。
using System;
namespace ClassLibrary1
{
public class Class1
{
public Class1()
{
}
public int GetSum(int x, int y)
{
return x + y;
}
}
}
//再另建一个项目,在项目中引用上面生成的ClassLibrary1.DLL
System.Reflection.Assembly a = System.Reflection.Assembly.LoadFrom("ClassLibrary1.DLL");
System.Type t = a.GetType("ClassLibrary1.Class1");
//动态生成ClassLibrary1.Class类的实例
Object theObj = System.Activator.CreateInstance(t);
//参数信息,GetSum需要两个int参数
System.Type[] paramTypes = new System.Type[2];
paramTypes[0] = System.Type.GetType("System.Int32");
paramTypes[1] = System.Type.GetType("System.Int32");
System.Reflection.MethodInfo mi = t.GetMethod("GetSum", paramTypes);
//参数值
Object[] parameters = new Object[2];
parameters[0] = 3;
parameters[1] = 4;
Object returnValue = mi.Invoke(theObj, parameters);
Console.WriteLine("ClassLibrary1.Class1.GetSum(3, 4) returns: {0}", returnValue.ToString());
2.用反射访问类的私有成员。
如果是C++,我们可以计算对象内成员的位置,然后偏移指针以访问类型的所有非公开成员。但是.NET对象完全受GC管理,地址根本无法得到,并且也无法通过指针调用方法。
当然... 这是一种很不值得推荐的技巧,访问非公有成员很可能破坏对象状态,造成不可预料的后果。但是无论如何,利用.NET的反射机制可以做到。
比如这样一个类:
class MyClass
{
private string PrivateField = "Private Field";
protected string ProtectedField = "Protected Field";
private string _ProtectedProperty = "Protected Property";
protected string ProtectedProperty
{
get{return _ProtectedProperty;}
set{_ProtectedProperty = value;}
}
private string _PrivateProperty = "Private Property";
private string PrivateProperty
{
get{return _PrivateProperty;}
set{_PrivateProperty = value;}
}
protected void ProtectedMethod()
{
Console.WriteLine("Protected Method Invoked");
}
private void PrivateMethod()
{
Console.WriteLine("Private Method Invoked");
}
}
除了默认的构造函数,没有任何成员是公开的,但是我仍然想获取和设置Field和Property的值,以及调用那两个方法。方法是:
MyClass mc = new MyClass();
Type t = typeof(MyClass);
BindingFlags bf = BindingFlags.Instance | BindingFlags.NonPublic;
// Fields
FieldInfo fi_protected = t.GetField("ProtectedField",bf);
FieldInfo fi_private = t.GetField("PrivateField",bf);
Console.WriteLine(fi_protected.GetValue(mc));
Console.WriteLine(fi_private.GetValue(mc));
fi_private.SetValue(mc,"New Private Field");
Console.WriteLine(fi_private.GetValue(mc));
Console.WriteLine();
// Properties
PropertyInfo pi_protected = t.GetProperty("ProtectedProperty", bf);
PropertyInfo pi_private = t.GetProperty("PrivateProperty", bf);
Console.WriteLine(pi_protected.GetValue(mc,null));
Console.WriteLine(pi_private.GetValue(mc,null));
pi_private.SetValue(mc,"New Private Property",null);
Console.WriteLine(pi_private.GetValue(mc,null));
Console.WriteLine();
// Methods
MethodInfo mi_protected = t.GetMethod("ProtectedMethod", bf);
MethodInfo mi_private = t.GetMethod("PrivateMethod", bf);
mi_protected.Invoke(mc,null);
mi_private.Invoke(mc,null);
Console.ReadLine();
输出:
Protected Field
Private Field
New Private Field
Protected Property
Private Property
New Private Property
Protected Method Invoked
Private Method Invoked
事件,一样可以操作, EventInfo :-)
当然... 这是一种很不值得推荐的技巧,访问非公有成员很可能破坏对象状态,造成不可预料的后果。但是无论如何,利用.NET的反射机制可以做到。
比如这样一个类:
class MyClass
{
private string PrivateField = "Private Field";
protected string ProtectedField = "Protected Field";
private string _ProtectedProperty = "Protected Property";
protected string ProtectedProperty
{
get{return _ProtectedProperty;}
set{_ProtectedProperty = value;}
}
private string _PrivateProperty = "Private Property";
private string PrivateProperty
{
get{return _PrivateProperty;}
set{_PrivateProperty = value;}
}
protected void ProtectedMethod()
{
Console.WriteLine("Protected Method Invoked");
}
private void PrivateMethod()
{
Console.WriteLine("Private Method Invoked");
}
}
除了默认的构造函数,没有任何成员是公开的,但是我仍然想获取和设置Field和Property的值,以及调用那两个方法。方法是:
MyClass mc = new MyClass();
Type t = typeof(MyClass);
BindingFlags bf = BindingFlags.Instance | BindingFlags.NonPublic;
// Fields
FieldInfo fi_protected = t.GetField("ProtectedField",bf);
FieldInfo fi_private = t.GetField("PrivateField",bf);
Console.WriteLine(fi_protected.GetValue(mc));
Console.WriteLine(fi_private.GetValue(mc));
fi_private.SetValue(mc,"New Private Field");
Console.WriteLine(fi_private.GetValue(mc));
Console.WriteLine();
// Properties
PropertyInfo pi_protected = t.GetProperty("ProtectedProperty", bf);
PropertyInfo pi_private = t.GetProperty("PrivateProperty", bf);
Console.WriteLine(pi_protected.GetValue(mc,null));
Console.WriteLine(pi_private.GetValue(mc,null));
pi_private.SetValue(mc,"New Private Property",null);
Console.WriteLine(pi_private.GetValue(mc,null));
Console.WriteLine();
// Methods
MethodInfo mi_protected = t.GetMethod("ProtectedMethod", bf);
MethodInfo mi_private = t.GetMethod("PrivateMethod", bf);
mi_protected.Invoke(mc,null);
mi_private.Invoke(mc,null);
Console.ReadLine();
输出:
Protected Field
Private Field
New Private Field
Protected Property
Private Property
New Private Property
Protected Method Invoked
Private Method Invoked
事件,一样可以操作, EventInfo :-)
饮水思源,不忘初心。
要面包,也要有诗和远方。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了