参考:https://www.cnblogs.com/Kare/p/4601436.html
https://www.cnblogs.com/sun-shadow/p/14478623.html 这个自己的 需要整合一下
1 class Program 2 { 3 public class NewClassw 4 { 5 public NewClassw() 6 { 7 } 8 public NewClassw(int aa) 9 { 10 a = aa; 11 } 12 13 public int a = 0; 14 private int b = 2; 15 public int A 16 { 17 get 18 { 19 return a; 20 } 21 set 22 { 23 a = value; 24 } 25 } 26 27 public void MyFuncA() 28 { 29 30 } 31 32 private void MyFuncB() 33 { 34 35 } 36 37 public void MyTestA() 38 { 39 Console.WriteLine("TestA :"+this.ToString()); 40 } 41 42 } 43 44 static void Main(string[] args) 45 { 46 NewClassw nc = new NewClassw(); 47 Type t = nc.GetType(); 48 49 //1.获取构造函数的信息------------------ 50 ConstructorInfo[] ci = t.GetConstructors(); //获取类的所有构造函数 51 foreach (ConstructorInfo c in ci) //遍历每一个构造函数 52 { 53 ParameterInfo[] ps = c.GetParameters(); //取出每个构造函数的所有参数 54 foreach (ParameterInfo pi in ps) //遍历并打印所该构造函数的所有参数 55 { 56 Console.Write(pi.ParameterType.ToString() + " " + pi.Name + ","); 57 } 58 Console.WriteLine(); 59 } 60 //2.使用构造函数生成对象------------------ 61 Type[] pt = new Type[0]; 62 ConstructorInfo ci2 = t.GetConstructor(pt); 63 object o = ci2.Invoke(null); 64 ((NewClassw)o).MyTestA(); 65 //3.用Activator的CreateInstance静态方法,生成新对象----------------------- 66 Console.WriteLine("3.用Activator的CreateInstance静态方法,生成新对象-----------------------"); 67 object o3 = Activator.CreateInstance(t); 68 ((NewClassw)o3).MyTestA(); 69 //4.查看类属性----------------------- 70 Console.WriteLine("4.查看类属性-----------------------"); 71 PropertyInfo[] pis = t.GetProperties(); 72 foreach (PropertyInfo pi in pis) 73 { 74 Console.WriteLine(pi.Name); 75 } 76 //5.查看类方法----------------------- 77 Console.WriteLine("5.查看类方法-----------------------"); 78 MethodInfo[] mis = t.GetMethods(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance| BindingFlags.DeclaredOnly); 79 foreach (MethodInfo mi in mis) 80 { 81 Console.WriteLine(mi.ReturnType + " " + mi.Name); 82 } 83 //6.查看类字段----------------------- 84 Console.WriteLine("6.查看类字段-----------------------"); 85 FieldInfo[] fis = t.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly); 86 foreach (FieldInfo fi in fis) 87 { 88 Console.WriteLine(fi.Name); 89 } 90 //7.给实例化类字段o3赋值 91 Console.WriteLine("7.对实例化字段进行读写-----------------------"); 92 FieldInfo aField = t.GetField("a"); 93 aField.SetValue(o3,3); 94 Console.WriteLine(aField.GetValue(o3)); 95 //8.给实例化属性读写 96 Console.WriteLine("8.给实例化属性读写-----------------------"); 97 PropertyInfo aPropertyInfo = t.GetProperty("A"); 98 aPropertyInfo.SetValue(o3,999); 99 Console.WriteLine(aPropertyInfo.GetValue(o3)); 100 //9.调用实例化方法 101 Console.WriteLine("9.调用实例化方法-----------------------"); 102 MethodInfo methodInfo = t.GetMethod("MyTestA"); 103 methodInfo.Invoke(o3,null); 104 } 105 106 }
System.Reflection.Assembly类
Assembly类可以获得程序集的信息,也可以动态的加载程序集,以及在程序集中查找类型信息,并创建该类型的实例。
使用Assembly类可以降低程序集之间的耦合,有利于软件结构的合理化。
通过程序集名称返回Assembly对象
Assembly ass = Assembly.Load("ClassLibrary831");
通过DLL文件名称返回Assembly对象
Assembly ass = Assembly.LoadFrom("ClassLibrary831.dll");
通过Assembly获取程序集中类
Type t = ass.GetType("ClassLibrary831.NewClass"); //参数必须是类的全名
通过Assembly获取程序集中所有的类
Type[] t = ass.GetTypes();
//通过程序集的名称反射
Assembly ass = Assembly.Load("ClassLibrary831");
Type t = ass.GetType("ClassLibrary831.NewClass");
object o = Activator.CreateInstance(t, "grayworm", "http://hi.baidu.com/grayworm");
MethodInfo mi = t.GetMethod("show");
mi.Invoke(o, null);
//通过DLL文件全名反射其中的所有类型
Assembly assembly = Assembly.LoadFrom("xxx.dll的路径");
Type[] aa = a.GetTypes();