废话不多说,有代码如下: class Program
{
static void Main(string[] args)
{
List<Func<string, string>> p = new List<Func<string, string>>(); string[] rule = new string[] { "m2", "m1", "m3" };
// 怎样根据字符串rule的信息把相应的函数存入函数List<>中?比如根据上面这个rule的定义,完成后的p应该是这个样子
// p.Add(m2);
// p.Add(m1);
// p.Add(m3);
// rule的内容是随时变化的,所以不可能通过一大串的if than来给p赋值,该怎么写?多谢
} static string m1(string s)
{
return s + "," + s;
} static string m2(string s)
{
return s + "....." + s;
} static string m3(string s)
{
return s + "=>" + s;
}
}// 怎样根据字符串rule的信息把相应的函数存入函数List<>中?比如根据上面这个rule的定义,完成后的p应该是这个样子
// p.Add(m2);
// p.Add(m1);
// p.Add(m3);
// rule的内容是随时变化的,所以不可能通过一大串的if than来给p赋值,该怎么写?多谢
{
static void Main(string[] args)
{
List<Func<string, string>> p = new List<Func<string, string>>(); string[] rule = new string[] { "m2", "m1", "m3" };
// 怎样根据字符串rule的信息把相应的函数存入函数List<>中?比如根据上面这个rule的定义,完成后的p应该是这个样子
// p.Add(m2);
// p.Add(m1);
// p.Add(m3);
// rule的内容是随时变化的,所以不可能通过一大串的if than来给p赋值,该怎么写?多谢
} static string m1(string s)
{
return s + "," + s;
} static string m2(string s)
{
return s + "....." + s;
} static string m3(string s)
{
return s + "=>" + s;
}
}// 怎样根据字符串rule的信息把相应的函数存入函数List<>中?比如根据上面这个rule的定义,完成后的p应该是这个样子
// p.Add(m2);
// p.Add(m1);
// p.Add(m3);
// rule的内容是随时变化的,所以不可能通过一大串的if than来给p赋值,该怎么写?多谢
解决方案 »
- [求助]求个WINFORM生成条形码的远吗,能扫的
- 如何获取子网页窗口的句柄
- [急]如何使用VS2008制作单个安装文件[在线等]
- XML修改问题
- log的记录
- Hashtable可否和DropList控件邦定在一起?
- 没有人知道这个问题怎样解决吗??????????????????????????
- 实现自定义文件对话框
- C#中s=TCPlistener.AcceptSocket()d的错误
- 各位请指点以下学习 c# 的免费电子书下载网站,给分感谢!
- 20090819定义的全局变量怎么不能生效?
- 如何判断操作系统类型?是winxp,winxp sp1,sp2,sp3,还是win2003 ,win2003 sp1? vista or vista sp1?
Type type = typeof(Program);
foreach (string ruleItem in rule)
{
MethodInfo mi = type.GetMethod(ruleItem, System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic); object retVal = mi.Invoke(null, null);
p.Add(retVal.ToString());
}
p.Add(retVal.ToString());
不能这么用,p是个List <Func <string, string>>类型,你这是往里面装string类型,怎么可能编译通过。
m1,m2,m3,......mn是程序员动态增加的,所以不可能在主程序中一个一个的指定,赋值,只能用反射取得函数的名字,再根据rule来给p赋值可是怎么赋值?高手呀,你为什么不出现、
反射不出委托对象的啊,只能反射出方法信息,然后通过该方法信息来调用方法,你可以试试这样作:
class Program
{
static void Main(string[] args)
{
List<System.Reflection.MethodInfo> p = new List<System.Reflection.MethodInfo>(); string[] rule = new string[] { "m2", "m1", "m3" };
foreach (string m in rule)
{
p.Add(typeof(Program).GetMethod(m, System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Instance));
}
foreach (System.Reflection.MethodInfo mi in p)
{
Console.WriteLine("方法名:" + mi.Name);
Console.WriteLine("方法调用的结果:" + mi.Invoke(null, new object[] { "abc" }));
Console.WriteLine();
} Console.WriteLine("按<Enter>退出。");
Console.ReadLine();
} static string m1(string s)
{
return s + "," + s;
} static string m2(string s)
{
return s + "....." + s;
} static string m3(string s)
{
return s + "=>" + s;
}
static void Main(string[] args)
{
List<Func<string, string>> p = new List<Func<string, string>>(); string[] rule = new string[] { "m2", "m1", "m3" }; foreach (string s in rule)
{
MethodInfo mi = typeof(Program).GetMethod(s, BindingFlags.Static | BindingFlags.NonPublic);
if (mi != null)
{
Delegate d = Delegate.CreateDelegate(typeof(Func<string, string>), mi);
if (d != null)
{
p.Add((Func<string,string>)d);
}
}
}
} static string m1(string s)
{
return s + "," + s;
} static string m2(string s)
{
return s + "....." + s;
} static string m3(string s)
{
return s + "=>" + s;
}
(天行健)这位老大,您是何方神圣?太牛逼了,我根本没想到这题会在几个小时内被解决,我在加拿大,我们公司甚至有前微软的程序员,我操,就是这微软程序员都不会做,没办法我才跑到csdn上来问你太强了,好崇拜,明天上班给分再次感谢,帮了我大忙了
using System.Collections.Generic;
using System.Reflection;class Program
{
static void Main(string[] args)
{
Dictionary<string, Func<string, string>> p = new Dictionary<string, Func<string, string>>();
string[] rule = new string[] { "m2", "m1", "m3" }; foreach (string s in rule)
{
p.Add(s,(Func<string, string>)typeof(Program).GetField(s).GetValue(null));
} //test
Console.WriteLine(p["m1"]("aaa"));
Console.WriteLine(p["m2"]("bbb"));
Console.WriteLine(p["m3"]("ccc"));
Console.ReadKey();
} public static Func<string, string> m1 = delegate(string s) { return s + "," + s; };
public static Func<string, string> m2 = delegate(string s) { return s + "....." + s; };
public static Func<string, string> m3 = delegate(string s) { return s + "=>" + s; };
}