.Net實戰之反射相關類之間的人體經絡關係

--1.類的部分組成成員ui

--2.巧記成員之間的關係spa

 

   [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.程序集code

   // 加載程序集(大腦)
            //獲取當前運行目錄下的指定程序集名稱
            Assembly ass = Assembly.Load("ReflectionDemo");
            //Assembly.LoadFile(),Assembly.LoadFrom()加載指定文件的程序集

--4.類型orm

    // 加載中樞神經的 全部經絡

            //獲取該程序集下的全部類型 
            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.字段、屬性、方法、特性blog

   //獲取字段 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);
            }

 

 下一篇,反射的數據操做 繼承

相關文章
相關標籤/搜索