• C#类特性和属性学习(转载)


    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Linq;
    using System.Text;
      
    using System.Reflection;
      
    namespace ConsoleApplication6
    {
        /// <summary>
        /// AttributeTargets.Class可以对类应用属性
        /// Inherited能否由派生类或重写成员继承
        /// AllowMultiple能否为一个程序元素指定多个指示属性实例
        /// 也就是说AllowMultiple=false 对于一个类型,该特性只能用一次
        /// 若一个Class类型前面出现多个TableAttribute,则会出现编译错误
        /// </summary>
        [AttributeUsage(AttributeTargets.Class,Inherited=true,AllowMultiple=false)]
        public class TableAttribute : Attribute
        {
            private string _tableName;
            public TableAttribute()
            {
       
            }
            public TableAttribute(string tableName)
            {
                this._tableName = tableName;
            }
            public string TableName
            {
                get
                {
                    return this._tableName;
                }
                set
                {
                    this._tableName = value;
                }
            }
        }
        /// <summary>
        /// 列特性
        /// AttributeTargets.Property可以对属性应用特性
        /// </summary>
        [AttributeUsage(AttributeTargets.Property,Inherited=false,AllowMultiple=false)]
        public class ColumAttribute : Attribute
        {
            private string _columName;
            private DbType _dbType;
      
            public ColumAttribute()
            {
       
            }
            public ColumAttribute(string columName)
            {
                this._columName = columName;
            }
            public ColumAttribute(string columName, DbType dbType)
            {
                this._columName = columName;
                this._dbType = dbType;
            }
            public string ColumName
            {
                get
                {
                    return this._columName;
                }
                set
                {
                    this._columName = value;
                }
            }
            public DbType DbTypeAttr
            {
                get
                {
                    return this._dbType;
                }
                set
                {
                    _dbType = value;
                }
            }
        }
      
        [Table("User")]
        public class User
        {
            [Colum("userId",DbType.Int32)]
            public int UserId { get; set; }
            [Colum("userName", DbType.String)]
            public string UserName { get; set; }
        }
      
        class Program
        {
            static void Main(string[] args)
            {
                User u = new User();
                u.UserId = 6;
                u.UserName = "allen";
      
      
                Type myObjType = u.GetType();
                Dictionary<string,string> columName = new Dictionary<string,string>();
                //获取自定义特性
                object temp = myObjType.GetCustomAttributes(typeof(TableAttribute),false).First();
                TableAttribute myAttr = temp as TableAttribute;
                Console.WriteLine("表名:{0}", myAttr.TableName);
      
                Console.WriteLine("列的名称和值:");
                foreach (PropertyInfo pi in myObjType.GetProperties())
                {
                    object attr = pi.GetCustomAttributes(false).First();
                    ColumAttribute cattr = attr as ColumAttribute;
                    Console.WriteLine("{0}:{1}",cattr.ColumName,pi.GetValue(u,null));
                }
      
                Console.ReadKey();
      
            }
        }
    }
    转自:http://www.cnblogs.com/liulun/archive/2010/02/25/1673324.html
  • 相关阅读:
    Codeforces Round #547 F1&F2. Same Sum Blocks(贪心)
    Codeforces Round #547 D. Colored Boots(贪心)
    Codeforces Round #547 C. Polycarp Restores Permutation(二分枚举/数学+模拟)
    CCF 201812-4 数据中心(最小生成树)
    CCF【小明放学&小明上学】
    TIME_WAIT状态
    ping的详细过程
    两段不相邻子段和之和最大
    神水一题之“Who's in the Middle”
    日进一步之“A Knight's Journey”
  • 原文地址:https://www.cnblogs.com/johnwonder/p/1675417.html
Copyright © 2020-2023  润新知