using   System; 
using   System.Reflection; 
using   System.Collections.Generic; 
public   class   A 

        private     void   method   ()   {Console.WriteLine(   "I   am   a   private   method   in   class   A ");} 

  
public   class   B 

        public     void     method   ()   {Console.WriteLine(   "I   am   a   public   method   in   class   B ");} 

  
public   class   MyClass 

public   static   void   Main() 

RL(); Console.WriteLine   ( "\nReflection.MethodInfo\n "); 
                A   MyA   =   new   A(); 
                B   MyB   =   new   B(); 
  
                //   Get   the   Type   and   MethodInfo. 
                Type   t   =   typeof(A); 
MethodInfo   mi1   =   t.GetMethod( "method ",   BindingFlags.NonPublic   |  BindingFlags.Instance); mi1.Invoke(MyA,null); Type   tt   =   typeof(B); 
MethodInfo   mi2   =   tt.GetMethod( "method "); 
mi2.Invoke(MyB,   null); 
  
            RL(); 
} #region   Helper   methods private   static   void   WL(object   text,   params   object[]   args) 

Console.WriteLine(text.ToString(),   args); 
} private   static   void   RL() 

Console.ReadLine(); 
} private   static   void   Break()   

System.Diagnostics.Debugger.Break(); 
} #endregion 

调试的时候显示
mi1.Invoke(MyA,null);
未处理的nullreferenceexception
未将对象引用设置到对象的实例
希望大家赐教

解决方案 »

  1.   

    似乎是因为没有获取mil的实例,mil显示的是null
      

  2.   

    mil未被实例化使用CreateInstance实例化
      

  3.   

    你多写了一个空格“ ”,我改了一下代码如下:
    using System;
    using System.Reflection;
    using System.Collections.Generic;
    public class A
    {
    private void method() { Console.WriteLine("I   am   a   private   method   in   class   A "); }
    }public class B
    {
    public void method() { Console.WriteLine("I   am   a   public   method   in   class   B "); }
    }public class MyClass
    {
    public static void Main()
    {
    RL(); Console.WriteLine("\nReflection.MethodInfo\n ");
    A MyA = new A();
    B MyB = new B(); //   Get   the   Type   and   MethodInfo. 
    Type t = typeof(A);
    MethodInfo mi1 = t.GetMethod("method", BindingFlags.NonPublic | BindingFlags.Instance); mi1.Invoke(MyA, null); Type tt = typeof(B);
    MethodInfo mi2 = tt.GetMethod("method");
    mi2.Invoke(MyB, null); RL();
    } #region   Helper   methods private static void WL(object text, params   object[] args)
    {
    Console.WriteLine(text.ToString(), args);
    } private static void RL()
    {
    Console.ReadLine();
    } private static void Break()
    {
    System.Diagnostics.Debugger.Break();
    } #endregion
    }
      

  4.   

    谢谢楼上的,原因找到了,因为“method”后么被csdn加了个空格,导致参数不对
      

  5.   

    MethodInfo mi1 = t.GetMethod("method ", BindingFlags.NonPublic | BindingFlags.Instance); ---------------
    MethodInfo mi1 = t.GetMethod("method", BindingFlags.NonPublic | BindingFlags.Instance);"method "
    ---->"method"
      

  6.   

    把你上面调用的方法
    mi1.Invoke(MyA, null);改成:
    t.InvokeMember("method", BindingFlags.InvokeMethod, System.Type.DefaultBinder, MyA, null);另外:Class A的method应为public
      

  7.   

    或者改成:
     MethodInfo mi1 = MyA.GetType().GetMethod("method ");
     mi1.Invoke(MyA, null);
     MethodInfo mi2 = MyB.GetType().GetMethod("method");
     mi2.Invoke(MyB, null);
      

  8.   

    To:tigerlgf 
       感谢你的回复,不好意思啊,结贴太早!而且现在还不能加分。
       不管怎样,多谢!大家共同进步!
      

  9.   

    把你上面调用的方法 
    mi1.Invoke(MyA,   null); 改成: 
    t.InvokeMember("method",   BindingFlags.InvokeMethod,   System.Type.DefaultBinder,   MyA,   null); 另外:Class   A的method应为public我这段代码是csdn另一个帖子里面的。
    是一位网友写的。
    有点疑问:Class A的method为private时程序也可以通过?希望tigerlgf赐教!