载入中,请稍候……

通过反射调用private方法

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

订阅: http://www.miniboke.com/Feed/Article_58.aspx
引用: http://www.miniboke.com/Trackback/GUWQDkOxTYHyrJGyxANc.aspx (UTF-8)
FTP操作范例 < 通过反射调用private方法 > 后台线程定期执行GC.Collect()

  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.     } 
被阅862次, 0投一票反射
  • 看完了要说点啥么?
  • 昵称 (不填说不了话)
  • 信箱地址 (不会被公开,但是不填也说不了话)
  • 网址 (这个不填也成)

Powered by MiniBoke v2.0.0.8 Build 0828

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

粤ICP备07500939号