ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

C# 反射相关

2022-01-26 21:35:17  阅读:223  来源: 互联网

标签:反射 Console C# ----------------------- BindingFlags WriteLine 相关 Assembly o3


参考: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();

 

标签:反射,Console,C#,-----------------------,BindingFlags,WriteLine,相关,Assembly,o3
来源: https://www.cnblogs.com/sun-shadow/p/15848153.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有