110,499
社区成员
发帖
与我相关
我的任务
分享
public class MyClass
{
public static int Method1(int p1)
{
return p1;
}
public static string Method2(string p1,string p2)
{
return p1 + p2;
}
public static List<int> Method3(int p1, int p2)
{
List<int> list = new List<int>();
list.Add(p1);
list.Add(p2);
return list;
}
}
//添加定义的方法,但不执行
array.Add(MyClass.Method1(1));
array.Add(MyClass.Method2("2","3"));
array.Add(MyClass.Method3(4, 5));
//循环出来挨个执行
foreach (var item in array)
{
item();
}
public delegate TResult MethodDelegateHandle<TResult, TParams>(params TParams[] args);
MyClass myClass = new MyClass();
Dictionary<Delegate, object> methods
= new Dictionary<Delegate, object>();
methods.Add(new MethodDelegateHandle<int, int>(myClass.Method1), (new int[]{1}));
methods.Add(new MethodDelegateHandle<string, string>(myClass.Method2), new string[] { "a", "b" });
foreach (var item in methods)
{
object obj = item.Key.DynamicInvoke(item.Value);
Console.WriteLine(obj);
}
public int Method1(params int[] a)
{
Console.WriteLine("Method1 : ");
return a[0];
}
public string Method2(params string[] s)
{
Console.WriteLine("Method2 : ");
return s[0] + s[1];
}
List<Action> array = new List<Action>();
//添加定义的方法,但不执行
array.Add(() => MyClass.Method1(1));
array.Add(() => MyClass.Method2("2","3"));
array.Add(() => MyClass.Method3(4, 5));
//循环出来挨个执行
foreach (var item in array)
{
item();
}