.NET程序内,访问私有或者保护成员的技巧

类别:Asp 点击:0 评论:0 推荐:

如果是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

除了Field、Property、Method,对象还有一种成员?对,事件,一样可以操作,答案是EventInfo :-)

本文地址:http://com.8s8s.com/it/it7793.htm