随笔- 22  文章- 1  评论- 13 

 

 1using System;
 2using System.Reflection;
 3
 4namespace Test
 5{
 6
 7
 8    public class A
 9    {
10        public virtual string method(int i) return i.ToString(); }
11    }

12
13    public class B
14    {
15        public virtual string method(int i, int o) return i.ToString()+o.ToString(); }
16    }

17
18    class Mymethodinfo
19    {
20        public static void Main()
21        {
22            A MyA = new A();
23            B MyB = new B();
24
25            // Get the Type and MethodInfo.
26            Type MyTypea = typeof(A);//Type.GetType("A");
27            MethodInfo Mymethodinfoa = MyTypea.GetMethod("method");
28
29            Type MyTypeb = typeof(B);//Type.GetType("B");
30
31            MethodInfo Mymethodinfob = MyTypeb.GetMethod("method");
32
33            // Get and display the Invoke method.
34            Console.Write("\nFirst method - " + MyTypea.FullName +
35                " returns " + Mymethodinfoa.Invoke(MyA, new object[] 100}));
36            Console.Write("\nSecond method - " + MyTypeb.FullName +
37                " returns " + Mymethodinfob.Invoke(MyB, new object[] 100200 }));
38            Console.Read();
39        }

40    }

41
42}
 posted on 2007-07-05 17:59 gamebaby 阅读(30) 评论(0) 编辑 收藏