前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C#类特性和属性特性

C#类特性和属性特性

作者头像
liulun
发布2022-05-09 11:43:36
5190
发布2022-05-09 11:43:36
举报
文章被收录于专栏:liulun
代码语言:javascript
复制
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();

        }
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2010-02-25,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档