C#类特性和属性特性

简介: using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.
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();

        }
    }
}

目录
相关文章
|
1月前
|
C#
C#学习相关系列之数据类型类的三大特性(二)
C#学习相关系列之数据类型类的三大特性(二)
|
1月前
|
C#
58.c#:directory类
58.c#:directory类
12 0
|
1月前
|
C#
57.c#:directorylnfo类
57.c#:directorylnfo类
13 0
|
1月前
|
C#
深入C#中的String类
深入C#中的String类
11 0
|
1月前
|
C#
C#学习系列相关之多线程(二)----Thread类介绍
C#学习系列相关之多线程(二)----Thread类介绍
|
1月前
|
C#
C#学习相关系列之数据类型类----嵌套类和嵌套方法(三)
C#学习相关系列之数据类型类----嵌套类和嵌套方法(三)
|
1月前
|
存储 C# 索引
C#学习相关系列之数据类型类的定义(一)
C#学习相关系列之数据类型类的定义(一)
|
1月前
|
Java C#
C#学习相关系列之多线程(七)---Task的相关属性用法
C#学习相关系列之多线程(七)---Task的相关属性用法
|
1月前
|
存储 API C#
60.c#:steamreader类
60.c#:steamreader类
12 0
|
1月前
|
API C# vr&ar
59.c#:steamWriter类
59.c#:steamWriter类
16 0