以下代码可以正常运行,但把方法改为非静态就报错using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;namespace ConsoleApplicationTest
{
    class ReflectionFu
    {
        private string doReplace(string FuName, string[] Parameter)
        {
            MethodInfo method = this.GetType().GetMethod(FuName, BindingFlags.NonPublic | BindingFlags.Static, null, new Type[] { typeof(string[]) }, null);
            object[] args = { Parameter };            if (method != null)
            {
                return (string)method.Invoke(method, args);
            }
            else
            {
                return "";
            }
        }
        private static string GetLatestNewsList(string[] Parameter)
        {
            return Parameter.Length.ToString();
        }
        static void Main(string[] args)
        {
            ReflectionFu re = new ReflectionFu();
            string[] w = { "1", "2", "3", "4", " 5" };
            Console.WriteLine(re.doReplace("GetLatestNewsList", w));
            Console.ReadKey();
        }
    }
}改为非静态方法后,报“对象与目标类型不匹配。”using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;namespace ConsoleApplicationTest
{
    class ReflectionFu
    {
        private string doReplace(string FuName, string[] Parameter)
        {
            MethodInfo method = this.GetType().GetMethod(FuName, BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[] { typeof(string[]) }, null);
            object[] args = { Parameter };            if (method != null)
            {
                return (string)method.Invoke(method, args);
            }
            else
            {
                return "";
            }
        }
        private string GetLatestNewsList(string[] Parameter)
        {
            return Parameter.Length.ToString();
        }
        static void Main(string[] args)
        {
            ReflectionFu re = new ReflectionFu();
            string[] w = { "1", "2", "3", "4", " 5" };
            Console.WriteLine(re.doReplace("GetLatestNewsList", w));
            Console.ReadKey();
        }
    }
}=====================================
请问错在那里了?

解决方案 »

  1.   

    BindingFlags.Public | BindingFlags.Instance
      

  2.   

    method.Invoke(method, args);
    第一个参数应该是类的实例
      

  3.   

    Assembly.CreateInstance先创建一个对象实例。
      

  4.   

    (string)method.Invoke(method, args);
    //是什么东西,调用啥啊?没看懂这句,到底想干啥?
    //而且还弄在返回的位置。
      

  5.   

     
    1)调用 FuName 传来的方法名,对应的方法啊。
    2)返回的位置  只是作显示一下结果而已
      

  6.   

    return (string)method.Invoke(this, args);
      

  7.   


    看了半天,明白了,是调用反射出的GetLatestNewsList方法并传参
    你传入的参数是string[] 但是,Invoke参数位置需要的是object[]改为
    method.Invoke(method, new object[] { "1", "2", "3", "4", "5"} ).ToString();//就行了,为什么要转字符,这个还没有想通
      

  8.   

    至于为什么能在静态的能用,非静态不能用,个人看法:因为string是object派生的,也就是说,用原有的object内存空间来存放string是OK的,
    然而你不使用静态,计算机就会为的操作开辟新的存储空间。
    但是C#的强类型也太强了,类型不同大多都不能等同,例,习惯在C里边用char a = string[0];类似的朋友发现,这个根本不让run。纯属猜想,无实际资料可查
      

  9.   

    懂得 BindingFlags.Instance 的人不需要注释就能给出答案,不懂的人看了注释也给不出答案。
      

  10.   

    来看看msdn上的例子(照抄也得抄得仔细):http://msdn.microsoft.com/zh-cn/library/5fed8f59(VS.95).aspxhttp://msdn.microsoft.com/zh-cn/library/system.reflection.bindingflags(v=VS.95).aspx
      

  11.   


          public string doReplace(string FuName, string[] Parameter)
            {
                MethodInfo method = this.GetType().GetMethod(FuName, BindingFlags.NonPublic|BindingFlags.Instance, null, new Type[] { typeof(string[]) }, null);
                object[] args = { Parameter };            if (method != null)
                {
                    return (string)method.Invoke(this, args);
                }
                else
                {
                    return "";
                }
            }
            private  string GetLatestNewsList(string[] Parameter)
            {
                return "len:"+Parameter.Length.ToString();
            }
      

  12.   

    string returnValue1 = classSampleType.InvokeMember("ClassMethod", BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance, null, null, new object[] { }) as string;
      

  13.   

    动态的函数要用实例来调用,静态函数要用类对象调用。。
    class.dosomething();  静态函数。。class c=new class();
    c. dosomething();  动态函数。。