以下代码可以正常运行,但把方法改为非静态就报错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();
}
}
}=====================================
请问错在那里了?
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();
}
}
}=====================================
请问错在那里了?
解决方案 »
- 接着问XtraGrid 问题
- 请各位大侠帮帮忙解决有关WinHTMLEditorControl使用出现的问题
- 100分求助:如何不触发TextBox的leave事件
- C#程序开发范例宝典 好吗
- 窗体跳转问题~~在线等
- 我的程序是生成.ini的方法保存用户的帐号,我不想让用户打开.ini就可以看到密码,我要是加密密码,我提取的时候又解不了,帮忙想个办法!
- 通过DataAdapter和DataSet读取数据失败(SQL)
- 请问怎样实现winform用户控件中自定义属性的下拉选项。
- C#发邮件正文
- 请问:哪有.net方面的电子书下载?
- 【在线等】把数据库的一个字段,存到一个空数组的代码
- c#中Dataset
第一个参数应该是类的实例
//是什么东西,调用啥啊?没看懂这句,到底想干啥?
//而且还弄在返回的位置。
1)调用 FuName 传来的方法名,对应的方法啊。
2)返回的位置 只是作显示一下结果而已
看了半天,明白了,是调用反射出的GetLatestNewsList方法并传参
你传入的参数是string[] 但是,Invoke参数位置需要的是object[]改为
method.Invoke(method, new object[] { "1", "2", "3", "4", "5"} ).ToString();//就行了,为什么要转字符,这个还没有想通
然而你不使用静态,计算机就会为的操作开辟新的存储空间。
但是C#的强类型也太强了,类型不同大多都不能等同,例,习惯在C里边用char a = string[0];类似的朋友发现,这个根本不让run。纯属猜想,无实际资料可查
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();
}
class.dosomething(); 静态函数。。class c=new class();
c. dosomething(); 动态函数。。