1. 包括方法、属性、事件、字段及构造函数,还可以获得每个成员的名称、限定符和参数等信息。
2.利用反射可以在运行时根据得到的类型信息动态创建类型的实例,设置字段,调用方法,设置属性和激发事件
// CLR开始在一个进程中运行时,它会立即为System.Type类型(在MSCorLib.dll中定义)创建一个特殊的类型对象System.RuntimeType
// 其它运行类型对象均为System.RuntimeType类型的实例。
// Object.GetType() 返回当前实例的运行时类型,更详细的说:
// Object.GetType()返回的是存储在指定对象中的“类型对象指针”成员中的地址,
// 即返回的是指定对象的类型对象指针。
// typeof 表达式采用的形式:typeof(type)
// 其中:type 要获得其 System.Type 对象的类型。
// 传入的自符串参数必须是完全限定名:
// Type myType1 = Type.GetType("System.Int32");
// System.Reflection.Assembly.GetType(<args>)
// System.Reflection.Assembly.GetTypes()
//
using System;
using System.Reflection;
{
public class Class_08_21
{
public static void Main()
{
//因为System.Type类型对象本身也是System.RuntimeType类型对象的“实例”。
// 获取 System.Type 自身的 Type 实例
Type t = typeof(System.Type);
Type t1 = t.GetType();
Type t2 = t1.GetType();
Console.WriteLine(t);
Console.WriteLine(t1);
Console.WriteLine(t2);
// 获取一些该 Type 的性质
Console.WriteLine("IsAbstract : {0}", t.IsAbstract);
//Console.WriteLine("IsAbstract : {0}", t.GetType().IsAbstract);
Console.WriteLine("IsClass : {0}", t.IsClass);
Console.WriteLine("IsArray : {0}", t.IsArray);
Console.WriteLine("IsValueType : {0}", t.IsValueType);
Console.WriteLine("IsPublic : {0}", t.IsPublic);
Console.WriteLine("Type 的 Fields :");
foreach (FieldInfo fi in t.GetFields())
{
Console.WriteLine("\t{0}", fi);
}
foreach (FieldInfo fi in t1.GetFields())
{
Console.WriteLine("\t{0}", fi);
}
Console.WriteLine("GetMethod : {0}", t.GetMethod("GetMethod", new Type[] { typeof(string), typeof(Type[]) }));
}
}