载入中,请稍候……

通过反射调用private方法

Admin 于 2008-09-29 03:12:26 发表.NET

  1. using System; 
  2. using System.Reflection; 
  3.  
  4. public class SimpleClass 
  5.     private void aPrivateMethod() 
  6.     { 
  7.         Console.WriteLine("you called me!"); 
  8.     } 
  9.  
  10. public class Tester 
  11.     public void Test() 
  12.     { 
  13.         //code snipped to invoke method 
  14.         //create an object of our class and get Type 
  15.         SimpleCass targetObj = new SimpleClass(); 
  16.         Type classType = targetObj.GetType(); 
  17.  
  18.         //get method: be careful to BindingFlags options. 
  19.         MethodInfo thePrivateMethod = classType.GetMethod("aPrivateMethod"
  20.                 BindingFlags.NonPublic | BindingFlags.Instance); 
  21.  
  22.         //invoke method on the object 
  23.         thePrivateMethod.Invoke(targetObj, null); 
  24.  
  25.     } 
被阅1026次, 0票反射 发表评论
1 / 1 / 1 | « 1 » |

Powered by MiniBoke v2.0.0.8 Build 0828

Copyright © 2008 迷你博客. All rights reserved.

粤ICP备07500939号