泛型反射

. 泛型回顾

1. 泛型参数可以有多个。

2. 开放类型(泛型类)和封闭类型(泛型类的实例)。

3. 泛型类的继承

4. 泛型约束(where T : new() – 目前.NET只支持无参构造函数的约束)

       (1)构造函数约束;

       (2)基类约束;

       (3)struct/class约束;

       (4)构造器约束;

5. 泛型方法的类型参数,可以用在该方法的形参,方法体,返回值三处。

6. 泛型方法的重载,类型参数和方法参数列表都是构成重载的元素。

7. 泛型方法的重写

程序示例:

 1 public class Father
 2     {
 3         public virtual T Shout<T>() where T : new()
 4         {
 5             return new T();
 6         }
 7     }
 8     public class Son : Father
 9     {
10         //约束自动从父类继承下来
11         public override T Shout<T>()
12         {
13             return new T();
14         }
15 }
View Code

8. 协变和逆变

协变:子类对象转父类对象。(out T)

逆变:父类对象转子类对象。(in T)

. 反射

1. 程序集的组成

2. 程序示例

 1 private void btnGetAssemblyTest_Click(object sender, EventArgs e)
 2         {
 3             //获取当前运行的程序里的程序集
 4             Assembly[] asss = AppDomain.CurrentDomain.GetAssemblies();
 5             
 6             Dog d = new Dog();
 7 
 8             //***********************反射创建成员************
 9             //1. 获取当前正在运行的程序集(AssemBly)对象
10             Assembly ass = this.GetType().Assembly;
11             //2. 获取程序集中Dog类的类型(Type)对象
12             Type tDog = ass.GetType("反射.Dog");
13             //3. 获取Dog类的字段
14             FieldInfo fInfo = tDog.GetField("name");
15             //4. 获取方法
16             MethodInfo mInfo = tDog.GetMethod("ShouHi");
17 
18             //***********************反射调用成员*************
19             //5. 根据Dog的Type对象实例化一个Dog对象
20             Dog d2 = Activator.CreateInstance(tDog) as Dog;
21             //Dog d3 = Activator.CreateInstance<Dog>();
22 
23             //6. 使用Dog类的name字段对象,为d2实例的name字段赋值
24             fInfo.SetValue(d2, "小白");
25 
26             //7. 调用反射中的方法
27             string strRes = mInfo.Invoke(d2, null).ToString();
28         }
29 
30         private void btnLoadAssembly_Click(object sender, EventArgs e)
31         {
32             //根据路径加载Assembly
33             string strPath = @"C:Users腾Documentsvisual studio 2013Projects泛型反射反射libs反射.exe";
34             Assembly ass = Assembly.LoadFrom(strPath); 
35         }
36 
37         private void btnLoad_Click(object sender, EventArgs e)
38         {
39             BLL.Class1 c = new BLL.Class1();
40 
41             //并不是所有外部添加的引用都会加载,只有用到才会被JIT加载进来
42             Assembly[] asss = AppDomain.CurrentDomain.GetAssemblies();
43         }
44 
45         private void btnGetType_Click(object sender, EventArgs e)
46         {
47             Assembly ass = this.GetType().Assembly;
48             //1. 通过类的全名称获取类的类型对象
49             Type t = ass.GetType("反射.Dog");
50             //2. 通过程序集获取所有的公共(public)类型
51             Type[] types = ass.GetExportedTypes();
52             //3. 通过程序集获取所有的类型
53             Type[] typesAll = ass.GetTypes();
54 
55             //获取单个类型对象            
56             //1. 通过类 直接获取 类型对象
57             Type t2 = typeof(Dog);
58             //2. 通过对象 来获取 类型对象
59             Type t3 = this.GetType();
60 
61             Dog d3 = new Dog();
62             Type t4 = d3.GetType();
63         }
View Code

3. 小案例 – 记事本插件

原文地址:https://www.cnblogs.com/HuoAA/p/4078353.html