WEB开发网
开发学院软件开发C语言 反射与委托(简单实例) 阅读

反射与委托(简单实例)

 2010-09-30 21:05:08 来源:WEB开发网   
核心提示:写了一段反射与委托的Demo,我尽量加了注释,反射与委托(简单实例),希望能给还不太了解反射与委托的朋友以启示,1usingSystem;2usingSystem.Collections.Generic;3usingSystem.Linq;4usingSystem.Text;5usingSystem.Reflectio

写了一段反射与委托的Demo。

我尽量加了注释,希望能给还不太了解反射与委托的朋友以启示。

 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using System.Text;
 5using System.Reflection;
 6
 7namespace ThreadDemo
 8{
 9    /**//// <summary>
10    /// 反射实例
11    /// </summary>
12    class Reflection
13    {
14        static void Main(string[] args)
15        {
16            Assembly asm = Assembly.Load("ThreadDemo");//加载程序集
17            Type[] typeInfo = asm.GetTypes();//获取程序集类型数组
18            foreach (Type types in typeInfo)
19            {
20                Console.WriteLine("程序集中类型:{0}", types.Name);
21                MethodInfo[] mInfo = types.GetMethods();
22                foreach (MethodInfo information in mInfo)
23                {
24                    Console.WriteLine("此类型所有的方法:{0}", information.Name);
25                }
26                Console.WriteLine("\n");
27            }
28            Console.WriteLine("\n\n");
29            Console.WriteLine("开始!");
30
31            Type inType = typeof(ThreadTest);
32            object obj = Activator.CreateInstance(inType);
33            string[] s = { "mlg" };//方法的参数
34            object objName = Activator.CreateInstance(inType, s);//创建指定类型的实例
35
36            MethodInfo methodInfo = inType.GetMethod("ThreadMethod");//根据字符串来获取方法信息
37            // methodInfo.Invoke(obj, null);
38            methodInfo.Invoke(objName, null);//调用类型中的方法
39            Console.ReadLine();
40
41        }
42    }
43}
44
45

1 2  下一页

Tags:反射 委托 简单

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接