一个开源的Asp.net2.0博客系统
- using System;
- using System.Reflection;
- public class SimpleClass
- {
- private void aPrivateMethod()
- {
- Console.WriteLine("you called me!");
- }
- }
- public class Tester
- {
- public void Test()
- {
- //code snipped to invoke method
- //create an object of our class and get Type
- SimpleCass targetObj = new SimpleClass();
- Type classType = targetObj.GetType();
- //get method: be careful to BindingFlags options.
- MethodInfo thePrivateMethod = classType.GetMethod("aPrivateMethod",
- BindingFlags.NonPublic | BindingFlags.Instance);
- //invoke method on the object
- thePrivateMethod.Invoke(targetObj, null);
- }
- }