反射的强大功能

代码
1 //1.通过反射技术,分析当前程序集(Assembly:exe,dll)的结构
2   var currentAssembly = Assembly.GetExecutingAssembly();
3 Console.WriteLine("当前程序集是:{0}", currentAssembly.FullName);
4 //1.1查看Assembly级别的元数据
5   var found = currentAssembly.GetCustomAttributes(
6 typeof(AssemblyTitleAttribute), false);
7 if (found.Length > 0)
8 {
9 var title = (AssemblyTitleAttribute)found[0];
10 Console.WriteLine("\t标题是:{0}",title.Title);
11 }
12 //1.2查看Assembly内部的构造
13   Console.WriteLine("\t内部类型及其结构:");
14
15 foreach (var type in currentAssembly.GetTypes())
16 {
17 Console.WriteLine("\t\t类型名:{0}", type.FullName);
18 foreach (var member in type.GetMembers())
19 {
20 Console.WriteLine("\t\t\t{0}{1}",
21 member.MemberType.ToString().PadRight(20),
22 member.Name);
23 }
24 }

 

代码
1 //1.3查看Assembly中的资源
2 //1.3.1简单资源,字符串
3   Console.WriteLine("Application Name:" + Properties.Resources.ApplicationName);
4
5 //1.3.2复杂资源,例如文件
6   var resourceKey = string.Format("{0}.Template.txt", typeof(Program).Namespace);
7 var stream = currentAssembly.GetManifestResourceStream(resourceKey);
8 var reader = new StreamReader(stream);
9 var content = reader.ReadToEnd()
10 .Replace("$time$", DateTime.Now.ToString())
11 .Replace("$author$", "chenxizhang")
12 .Replace("$content$", "这是一个测试报告");
13
14 Console.WriteLine();
15 Console.WriteLine("读取文件资源,结果是:");
16 Console.WriteLine(content);

 

代码
1 //2.通过反射技术,动态实例化对象
2 //传统做法:Employee e=new Employee();
3  
4 var className = "_07_ReflectionSample.Employee";
5 var employee1 = currentAssembly.CreateInstance(className) as IEmployee;
6 employee1.Name = "ares";
7 Console.WriteLine("第一个员工的名称:{0}", employee1.Name);
8
9 var employee2 = currentAssembly.CreateInstance(className, true, BindingFlags.Public | BindingFlags.Instance, null, new object[] { "ares chen" }, null, null) as IEmployee;
10 Console.WriteLine("第二个员工的名称:{0}", employee2.Name);
11
12 //3.通过反射技术,动态执行方法
13 //传统做法:A(),B()
14 var methodName = "A";
15 var method = typeof(Program).GetMethod(methodName);
16 if (method != null)
17 {
18 method.Invoke(null, null);
19 }
20
21 //4.动态加载外部程序集(不引用)
22 //传统做法,先添加引用
23 //_07_ClassLibray.ManagementModule module = new _07_ClassLibray.ManagementModule();
24 //module.Show();
25
26 var _fileName = "07_ClassLibray.dll";
27 var _className = "_07_ClassLibray.ManagementModule";
28 var _methodName = "Show";
29 var _externalAssembly = Assembly.LoadFrom(_fileName);
30 var _instance = _externalAssembly.CreateInstance(_className);
31 var _method = _instance.GetType().GetMethod(_methodName);
32 _method.Invoke(_instance, null);
33
34
35 Console.Read();
36 }
37
38 public static void A()
39 {
40 Console.WriteLine("A方法在工作");
41 }
42
43 public static void B()
44 {
45 Console.WriteLine("B方法在工作");
46 }
47 }
48
49
50 class Employee : _07_ReflectionSample.IEmployee
51 {
52 //构造函数constructor
53 public Employee() { }
54 public Employee(string name) {
55 Name = name;
56 }
57
58
59 private string _name;//字段Field
60 public string Name//属性Property,其实是特殊的方法
61 {
62 get { return _name; }
63 set { _name = value; }
64 }
65
66 //方法Method
67 public override string ToString()
68 {
69 return base.ToString();
70 }
71 //事件
72 public event EventHandler NameChanged;
73 }

 

注:反射动态加载外部程序集时,需要手动copy 07_ClassLibray类库文件的dll文件到该应用程序目录下(07_ClassLibray类库文件)

posted @ 2010-07-05 11:27  peter cheng  阅读(404)  评论(0编辑  收藏  举报