本篇文章暂不介绍 反射获取:非public
修饰的方法、字段、属性
等。
前置类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 public class Animal { public int ID = 1001 ; public int Age { get ; set ; } public string Name { get ; set ; } public Animal () { } public Animal (string name, int age ) { Name = name; Age = age; } public void Speak () { Console.WriteLine("沉默中..." ); } public void Speak (string msg ) { Console.WriteLine(Name + " says: " + msg); } public string Calculate () { return "test" ; } public static string MSG = "hello!" ; public static int Exp { get ; set ; } public static int Calculate (int a, int b ) { return a + b; } public void RefMethod1 (int a, ref int b ) { b = a + 1 ; } public void RefMethod2 (ref int a, ref int b ) { int temp = a; a = b; b = temp; } } public class Cat :Animal { public Cat () : base () { } }
判断是类,获取基类 1 2 3 4 5 6 Console.WriteLine("IsClass: " + typeof (Cat).IsClass); Console.WriteLine("BaseClass: " + typeof (Cat).BaseType);
成员 成员是指:除了Assembly,Module外所有的类型,如:构造方法,方法,属性,字段,委托等所有的集合。
反射创建对象 方式多多,这里记录两种方式
1 2 3 4 5 6 7 8 9 10 11 Type type = typeof (Animal); var kitty1 = (Animal)Activator.CreateInstance(type);var kitty2 = (Animal)Activator.CreateInstance(type, new object [] { "Kitty" , 3 });ConstructorInfo constructor0 = type.GetConstructor(Type.EmptyTypes); ConstructorInfo constructor2 = type.GetConstructor(new Type[] { typeof (string ), typeof (Int32) }); var tom1 = constructor0.Invoke(new object [] { }) as Animal;var tom2 = constructor2.Invoke(new object [] { "Tom" , 3 }) as Animal;
类的属性 通过GetProperty
、GetProperties
获取一个或多个属性。
1 2 3 4 5 6 7 8 9 10 11 12 13 var ageProperty = typeof (Animal).GetProperty("Age" );Animal dog = new Animal(); Console.WriteLine(ageProperty.GetValue(dog)); ageProperty.SetValue(dog, 999 ); Console.WriteLine(ageProperty.GetValue(dog)); var property = typeof (Animal).GetProperty("Exp" );Console.WriteLine(property.GetValue(null , null )); property.SetValue(null , 999 ); Console.WriteLine(property.GetValue(null ));
类的字段 通过GetField
、GetFields
获取一个或多个字段。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 FieldInfo[] fields = type.GetFields(); Animal bird = new Animal(); FieldInfo field = type.GetField("ID" ); Console.WriteLine(field.GetValue(bird)); field.SetValue(bird, 999 ); Console.WriteLine(field.GetValue(bird)); var field = typeof (Animal).GetField("MSG1" );Console.WriteLine(field.GetValue(null )); field.SetValue(null , "hi" ); Console.WriteLine(field.GetValue(null ));
类中的方法 通过GetMethod
、GetMethods
获取一个或多个方法。特殊的有构造方法,使用GetConstructor
获取。
构造方法 上面通过反射创建对象时有记录到,不再赘述。
非静态方法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 Animal bird = new Animal(); var method0 = typeof (Animal).GetMethod("Speak" , Type.EmptyTypes);method0.Invoke(bird,new Type[] {}); var method1 = typeof (Animal).GetMethod("Speak" , new Type[] { typeof (string ) });method1.Invoke(bird, new object [] { "早上好" }); var method2 = typeof (Animal).GetMethod("Calculate" , Type.EmptyTypes);object result = method2.Invoke(bird, new Type[] { });object [] objs = new object [] { 100 , null };var method4 = typeof (Animal).GetMethod("refMethod1" );method4.Invoke(bird, objs); Console.WriteLine(objs[0 ]+" : " + objs[1 ]); object [] objs = new object [] { 0 , 99 };var method3 = typeof (Animal).GetMethod("RefMethod2" );method3.Invoke(bird, objs); Console.WriteLine(objs[0 ]+" : " + objs[1 ]);
静态方法 1 2 3 4 var method4 = typeof (Animal).GetMethod("Calculate" , new Type[] { typeof (Int32), typeof (Int32) });object sum = method4.Invoke(null , new object []{ 100 , 100 });Console.WriteLine(sum.ToString());
泛型类 泛型可以当作普通的类进行使用,但是实例化泛型时必须指定泛型类型 。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 public class MyClass <T >{ public MyClass () { Console.WriteLine("执行构造方法" ); } public void ShowInfo (T a ) { Console.WriteLine(a); } } public class Program { static void Main (string [] args ) { Type type = typeof (MyClass<int >); Type type = typeof (MyClass<>).MakeGenericType(new Type[] { typeof (int ) }); var obj = Activator.CreateInstance(type); ConstructorInfo method = typeof (MyClass<int >).GetConstructor(Type.EmptyTypes); var obj = method.Invoke(new object [] { }); MethodInfo method = typeof (MyClass<int >).GetMethod("ShowInfo" ); method.Invoke(obj, new object [] { 100 }); } }
这里对泛型绑定了int
类型,可以通过GetGenericTypeDefinition
去除泛型与参数类型的绑定。下面举例重新绑定为string类型。
1 2 3 4 5 6 7 8 9 10 11 12 Console.WriteLine(type.FullName); Type _type = type.GetGenericTypeDefinition(); Console.WriteLine(_type.FullName); Type new_type = _type.MakeGenericType(new Type[] { typeof (string ) }); var o2 = Activator.CreateInstance(new_type);var method2 = new_type.GetMethod("ShowInfo" );method2.Invoke(o2, new object [] { "hello" });