Using directives#region Using directives using System; using System.Collections.Generic; using System.Text; using System.Reflection; #endregion namespace DynaMet { class ApplyDynamic { privatestatic MethodInfo GetMethodFor<T,U>(String s, paramsobject[] args) { int len = args !=null? args.Length : 0; Type[] tArr =new Type[len +1]; tArr[0] =typeof(U); int i =1; if (args !=null) { foreach (object o in args) { tArr[i++] = o.GetType(); } } MethodInfo m =typeof(T).GetMethod(s, tArr); return m; } publicstaticvoid apply<S, U, T>(S s, String methodName) where S : IEnumerable<U> where T : new() { MethodInfo m = GetMethodFor<T,U>(methodName,null); foreach (U o in s) { m.Invoke(new T(), newobject[] { o }); } } publicstaticvoid apply<S, U, T, A1>(S s, String methodName, A1 a) where S : IEnumerable<U> where T : new() { MethodInfo m = GetMethodFor<T, U>(methodName, newobject[] { a }); foreach (U o in s) { object[] args =newobject[] { o, a }; m.Invoke(new T(), args); } } publicstaticvoid apply<S, U, T, A1, A2>(S s, String methodName, A1 a1, A2 a2) where S : IEnumerable<U> where T : new() { MethodInfo m = GetMethodFor<T, U>(methodName, newobject[] { a1, a2 }); foreach (Object o in s) { object[] args =newobject[] { o, a1, a2 }; m.Invoke(new T(), args); } } publicstaticvoid apply<S, U, T>(S s, String methodName, paramsobject[] args) where S : IEnumerable<U> where T:new() { MethodInfo m = GetMethodFor<T, U>(methodName, args); foreach (U o in s) { object[] args2 =newobject[args.Length +1]; args2[0] = o; Array.Copy(args, 0, args2, 1, args.Length); m.Invoke(new T(), args2); } } } class Dynamic { publicvirtualvoid Print() { Console.WriteLine("No arguments"); } publicvoid Print(string printMe) { Console.WriteLine("One Argument :{0}", printMe); } publicvoid Print(string printMe, int printThisToo) { Console.WriteLine("Two Argument: {0},{1}", printMe,printThisToo); } privateint t; publicvoid Do() { t++; } staticvoid Main(string[] args) { List<String> arr =new List<String>(); for (int i =0; i <5; i++) arr.Add(i.ToString()); ApplyDynamic.apply<List<String>, String, Dynamic>(arr, "Print",12); } } }