Gone with the wind

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
Using directives

 

namespace DynaMet

{

 

class ApplyDynamic

{

private static MethodInfo GetMethodFor<T,U>(String s, params object[] 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;

}


 

 

public static void 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(), new object[] { o });

}


}


 

public static void apply<S, U, T, A1>(S s, String methodName, A1 a)

where S : IEnumerable
<U>

where T : 
new()

{

MethodInfo m 
= GetMethodFor<T, U>(methodName, new object[] { a });

foreach (U o in s)

{

object[] args = new object[] { o, a };

m.Invoke(
new T(), args);

}


}


 

public static void 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, new object[] { a1, a2 });

foreach (Object o in s)

{

object[] args = new object[] { o, a1, a2 };

m.Invoke(
new T(), args);

}


}


 

 

public static void apply<S, U, T>(S s, String methodName, params object[] args) where S : IEnumerable<U> where T:new()

{

MethodInfo m 
= GetMethodFor<T, U>(methodName, args);

foreach (U o in s)

{

object[] args2 = new object[args.Length + 1];

args2[
0= o;

Array.Copy(args, 
0, args2, 1, args.Length);

m.Invoke(
new T(), args2);

}


}


 

 

}


 

class Dynamic

{

public virtual void Print()

{

Console.WriteLine(
"No arguments");

}


 

public void Print(string printMe)

{

Console.WriteLine(
"One Argument :{0}", printMe);

}


 

public void Print(string printMe, int printThisToo)

{

Console.WriteLine(
"Two Argument: {0},{1}", printMe,printThisToo);

}


 

private int t;

public void Do()

{

t
++;

}


 

static void 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);

}


}


}


posted on 2004-10-25 00:06  Gone with the wind  阅读(105)  评论(0)    收藏  举报