无网不进  
软硬件开发

目录结构:

 

contents structure [+]

反射是.NET中的重要机制,通过反射,可以在运行时获得程序或程序集中每一个类型(包括类、结构、委托、接口和枚举等)的成员和成员的信息。有了反射,即可对每一个类型了如指掌。另外我还可以直接创建对象,即使这个对象的类型在编译时还不知道。

System.Type 类--通过这个类可以访问任何给定数据类型的信息。
System.Reflection.Assembly类--它可以用于访问给定程序集的信息,或者把这个程序集加载到程序中。

一.基本介绍

1.1 System.Type类的使用

System.Type 类对于反射起着核心的作用。但它是一个抽象的基类,Type有与每种数据类型对应的派生类,我们使用这个派生类的对象的方法、字段、属性来查找有关该类型的所有信息。
获取给定类型的Type引用有3种常用方式:
使用 C# typeof 运算符。

Type t = typeof(string);

使用对象GetType()方法。

string s = "grayworm";
Type t = s.GetType();

还可以调用Type类的静态方法GetType()。

Type t = Type.GetType("System.String");

上面这三类代码都是获取string类型的Type,在取出string类型的Type引用t后,我们就可以通过t来探测string类型的结构了。

复制代码
string n = "grayworm";
Type t = n.GetType();
foreach (MemberInfo mi in t.GetMembers())
{
Console.WriteLine("{0}/t{1}",mi.MemberType,mi.Name);
}  
复制代码

1.1.1 查看类中的构造方法

复制代码
NewClassw nc = new NewClassw();
Type t = nc.GetType();
ConstructorInfo[] ci = t.GetConstructors();    //获取类的所有构造函数
foreach (ConstructorInfo c in ci) //遍历每一个构造函数
{
ParameterInfo[] ps = c.GetParameters();    //取出每个构造函数的所有参数
foreach (ParameterInfo pi in ps)   //遍历并打印所该构造函数的所有参数
{
Console.Write(pi.ParameterType.ToString()+" "+pi.Name+",");
}
Console.WriteLine();
}
复制代码

1.1.2 用构造函数动态生成对象

复制代码
Type t = typeof(NewClassw);
Type[] pt = new Type[2];
pt[0] = typeof(string);
pt[1] = typeof(string);
//根据参数类型获取构造函数
ConstructorInfo ci = t.GetConstructor(pt);
//构造Object数组,作为构造函数的输入参数
object[] obj = new object[2]{"grayworm","hi.baidu.com/grayworm"};   
//调用构造函数生成对象
object o = ci.Invoke(obj);    
//调用生成的对象的方法测试是否对象生成成功
//((NewClassw)o).show();    
复制代码

1.1.3 用Activator生成对象

复制代码
Type t = typeof(NewClassw);
//构造函数的参数
object[] obj = new object[2] { "grayworm", "hi.baidu.com/grayworm" };   
//用Activator的CreateInstance静态方法,生成新对象
object o = Activator.CreateInstance(t,"grayworm","hi.baidu.com/grayworm");
//((NewClassw)o).show();
复制代码

1.1.4 类的属性(Property)

这里需要区分属性和字段的区别,属性就是在字段后面多了一个set;或是get;方法,例如:
private string property{set;get;}
如果去掉后面的括号就变成字段了,例如:
private string field;


查看Public修饰的属性

复制代码
NewClassw nc = new NewClassw();
Type t = nc.GetType();
PropertyInfo[] pis = t.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
foreach(PropertyInfo pi in pis)
{
Console.WriteLine(pi.Name);
}
复制代码

C#还提供了查看静态的属性,只需要再在后面加上BindingFlags.Static,就可以了。不过,需要注意,官方文档指出BindingFlags.Instance或是BindingFlags.Static必不可少。

1.1.5 查看类中的方法

复制代码
NewClassw nc = new NewClassw();
Type t = nc.GetType();
MethodInfo[] mis = t.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);//这里获得的只是public修饰的方法,要获得所有的方法,参照上面的获取属性的代码,指定标识符就可以了
foreach (MethodInfo mi in mis)
{
Console.WriteLine(mi.ReturnType+" "+mi.Name);
}
复制代码

1.1.6 查看类的字段 (Field)

复制代码
NewClassw nc = new NewClassw();
Type t = nc.GetType();
FieldInfo[] fis = t.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
foreach (FieldInfo fi in fis)
{
Console.WriteLine(fi.Name);
}
复制代码

1.1.7 用反射生成对象,并调用属性、方法和字段进行操作

复制代码
NewClassw nc = new NewClassw();
Type t = nc.GetType();
object obj = Activator.CreateInstance(t);
//取得ID字段
FieldInfo fi = t.GetField("ID",BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
//给ID字段赋值
fi.SetValue(obj, "k001");
//取得MyName属性
PropertyInfo pi1 = t.GetProperty("MyName",BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
//给MyName属性赋值
pi1.SetValue(obj, "grayworm", null);
PropertyInfo pi2 = t.GetProperty("MyInfo");
pi2.SetValue(obj, "hi.baidu.com/grayworm", null);
//取得show方法
MethodInfo mi = t.GetMethod("show");
//调用show方法
mi.Invoke(obj, null);
复制代码

1.2 System.Reflection.Assembly类的使用

Assembly类可以获得程序集的信息,也可以动态的加载程序集,以及在程序集中查找类型信息,并创建该类型的实例。
使用Assembly类可以降低程序集之间的耦合,有利于软件结构的合理化。

1.2.1 通过程序集名称返回Assembly对象

Assembly ass = Assembly.Load("ClassLibrary831");//添加程序集的名称

1.2.2 通过DLL文件名称返回Assembly对象

Assembly ass = Assembly.LoadFrom("ClassLibrary831.dll");//添写DLL文件的路径

1.2.3 通过Assembly获取程序集中类

Type t = ass.GetType("ClassLibrary831.NewClass");   //参数必须是类的全名
//获取程序集中所有的类
Type[] t = ass.GetTypes();

二.解析自定义特性

为了简便编程,我们往往需要自定义特性,在程序中可以通过反射获得特性的相关信息。
例如:

复制代码
    class Program
    {
        [MyTag("test")]
        public void init() {
        }
        static void Main(string[] args)
        {
            Type cp= typeof(Program);
            //获得指定的方法
            MethodInfo method= cp.GetMethod("init",System.Reflection.BindingFlags.Public|System.Reflection.BindingFlags.NonPublic|System.Reflection.BindingFlags.Instance);
            //获得特性
            MyTag attr = method.GetCustomAttribute(typeof(MyTag)) as MyTag;
            Console.WriteLine(attr.value);
            Console.ReadLine();
        }
    }
    [AttributeUsage(AttributeTargets.Method, Inherited = true)]
    class MyTag : System.Attribute
    {
        public string value = String.Empty;
        public MyTag(String info)
        {
            this.value = info;
        }
    }
复制代码
posted on 2018-05-28 18:33  无网不进  阅读(186)  评论(0编辑  收藏  举报