zoukankan      html  css  js  c++  java
  • .Net实战之反射相关类之间的人体经络关系

    --1.类的部分组成成员

    --2.巧记成员之间的关系

       [MyTable("T_UserInfo")]
        public class UserInfo : Person, UserService
        {
    
            private int _age2;
            private int _age;
            [DisplayName("年龄")]
            public int Age
            {
                get
                {
                    return _age;
                }
                set
                {
                    _age = value;
                }
            }
            [DisplayName("姓名")]
            public string Name { get; set; }
            public void ShowUserInfo()
            {
                Console.WriteLine(string.Format("name:{0},age:{1}", Name, _age));
            }
    
            protected void ShowName()
            {
                Console.WriteLine("showName:" + Name);
            }
        }
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
        public class MyTableAttribute : Attribute
        {
            private string _tableName;
            public MyTableAttribute(string name)
            {
                _tableName = name;
            }
            public string Name { get { return _tableName; } }
        }

    --3.程序集

       // 加载程序集(大脑)
                //获取当前运行目录下的指定程序集名称
                Assembly ass = Assembly.Load("ReflectionDemo");
                //Assembly.LoadFile(),Assembly.LoadFrom()加载指定文件的程序集

    --4.类型

        // 加载中枢神经的 所有经络
    
                //获取该程序集下的所有类型 
                Type[] types = ass.GetTypes();
    
                //根据类的全名称(命门空间.类名)获取类的信息 
                Type type1 = ass.GetType("ReflectionDemo.person");
    
                //忽略大小写或找不到该类型抛出异常
                //Type type2 = ass.GetType("ReflectionDemo.person2", throwOnError: true, ignoreCase: true);
    
                //获取程序集中公共类型
                Type[] publicTypes = ass.GetExportedTypes();
    
                //获取类的类型
                Type classUserType = typeof(UserInfo);
    
                //获取实例的类型
                UserInfo ui = new UserInfo();
                Type instanceType = ui.GetType();
    
                //获取类型的名称
                Console.WriteLine(string.Format("typeFullName:{0},typeName:{1}", instanceType.FullName, instanceType.Name));
    
                //是否继承自某个类
                Console.WriteLine("是否继承自某个类-----" + typeof(UserInfo).IsSubclassOf(typeof(Person)));
    
                //是否实现了某个接口(接口的实现类型 是否是 指定的类型)
                Console.WriteLine("是否实现了某个接口-----" + typeof(UserService).IsAssignableFrom(typeof(UserInfo)));
    
                //是否是public的类型
                Console.WriteLine("是否是public的类型-----" + classUserType.IsPublic);

    --5.字段、属性、方法、特性

       //获取字段 BindingFlags位标记 获取字段不同的方式
                //t.GetField();t.GetFields()
                FieldInfo fiAge = t.GetField("_age",
    BindingFlags.Public |
    BindingFlags.NonPublic |
    BindingFlags.Static |
    BindingFlags.Instance |
    BindingFlags.DeclaredOnly);
                //获取属性 类似Field
                t.GetProperties();
                PropertyInfo pi = t.GetProperty("");
                //pi.CanRead;//能读
                //pi.CanWrite;//能写
                //获取方法
                MethodInfo[] methods = t.GetMethods();
                MethodInfo method = t.GetMethod("ShowUserInfo");
                //获取特性
                MyTableAttribute tabAttr = t.GetCustomAttribute(typeof(MyTableAttribute)) as MyTableAttribute;
                if (tabAttr != null)
                {
                    Console.WriteLine("select * from " + tabAttr.Name);
                }

     下一篇,反射的数据操作 

  • 相关阅读:
    FW: MBA的学费一般在多少?学MBA有啥用?
    【悟】资本思维:这才是真正"赚大钱的逻辑"
    关于印发《北京市引进人才管理办法(试行)》的通知
    基于wordpress电商解决方案 + POS在线轻量级系统
    SAP Fasion FMS solution 时尚商品行业解决方案
    SAP IS-Retail Promotion and POS Bonus Buy integration
    SAP CARR for retails solution SAP零售集成方案
    Forest-一款比httpClient,okhttp更优雅人性化的http请求组件
    IDEA 日志插件 MyBatis Log Plugin 在线格式化日志工具
    Jenkins ERROR: Server rejected the 1 private key(s)
  • 原文地址:https://www.cnblogs.com/zjflove/p/5866068.html
Copyright © 2011-2022 走看看