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();

        }
    }
}
时间: 2024-09-20 12:04:55

C#类特性和属性特性的相关文章

[你必须知道的.NET] 第三回:历史纠葛:特性和属性

1. 引言 attribute是.NET框架引入的有一技术亮点,因此我们有必要花点时间走进一个发现attribute登堂入室的入口.因为.NET Framework中使用了大量的定制特性来完成代码约定,[Serializable].[Flags].[DllImport].[AttributeUsage]这些的构造,相信我们都见过吧,那么你是否了解其背后的技术. 提起特性,由于高级语言发展的历史原因,不免让人想起另一个耳熟能详的名字:属性.特性和属性,往往给初学者或者从C++转移到C#的人混淆的概

.NET简谈特性(代码属性)

由于很久没有写关于基础知识的东西了,今天来总结一下关于.NET平台中的代码属性的概念:只是基础介绍,文章的本意是带领初学者学会使用特性,而不是讨论高深的底层技术:[王清培版权所有,转载请给出署名] 1.什么叫特性Attribute 特性对一般.NET编程人员来说并不陌生,自从我们开始接触C#语言的时候,我们多多少少已经接触了关于特性的使用:但是对于初学者来说真的不明白特性的意义,特性到底是属于类还是类实例,这些概念搞不清楚对理解代码的逻辑真的很顺利:这篇文章我将带您来了解特性的作用,其实特性的真

返本求源——DOM元素的特性与属性

抛砖引玉 很多前端类库(比如dojo与JQuery)在涉及dom操作时都会见到两个模块:attr.prop.某天代码复查时,见到一段为某节点设置文本的代码: attr.set(node, 'innerText', 'Hello World!') 这段代码执行后并未生效,虽说innerText不是标准属性,尚未被ff支持,可用的是chrome,这个属性是被支持的.既然显示的文本没变,那就查看一下元素吧. innerText被添加到了html标签上,而换成prop模块后,成功的为节点替换文本. 以上

上层建筑——DOM元素的特性与属性(dojo/dom-attr)

上一篇返本求源中,我们从DOM基础的角度出发,总结了特性与属性的关系.本文中,我们来看看dojo框架是如何处理特性与属性的.dojo框架中特性的处理位于dojo/dom-attr模块属性的处理为与dojo/dom-prop模块中. attr.set() 方法的函数签名为: require(["dojo/dom-attr"], function(domAttr){ result = domAttr.set("myNode", "someAttr",

hql语句查询实体类News的属性category为“生活类新闻”的LIST,HQL语句怎么写?

问题描述 hql语句查询实体类News的属性category为"生活类新闻"的LIST,HQL语句怎么写? 用SSH框架,写一个实现类,查询实体类News的属性category为"生活类新闻"的LIST 返回一个LIST,该怎么写???????????????????? 实体类为News 属性为category 数据库中表名为t_news category为属性 import java.util.List; import org.springframework.st

PHP中如何定义类及其成员属性与操作

 类的概念:类是具有相同属性和操作的一组对象的集合.它为属于该类的所有对象提供了统一的抽象描述,其内部包括属性和操作两个主要部分.在面向对象的编程语言中,类是一个独立的程序单位,它应该有一个类名并包括属性说明和操作说明两个主要部分. 1.类的定义: i. 类的关键字定义使用class 1.定义一个空类 Class Person{}; 2.定义一个有成员属性和操作的类 Class Person{ 成员属性........ 操    作........ } 3.定义一个不能被继承的类,使用final

js类的静态属性和实例属性的理解_javascript技巧

复制代码 代码如下: <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>测试</title> </head> <body> <script type="text/javascript"><!-- function Man

返璞归真 asp.net mvc (12) - asp.net mvc 4.0 新特性之移动特性

原文:返璞归真 asp.net mvc (12) - asp.net mvc 4.0 新特性之移动特性 [索引页][源码下载] 返璞归真 asp.net mvc (12) - asp.net mvc 4.0 新特性之移动特性 作者:webabcd 介绍asp.net mvc 之 asp.net mvc 4.0 新特性之移动特性 为不同的客户端提供不同的视图 手动重写 UserAgent,从而强制使用对应的视图 示例1.演示如何为不同的客户端提供不同的视图Global.asax.cs /* * 为

objective ...-基于oc的ios开发,如何给一个系统类添加一个属性?

问题描述 基于oc的ios开发,如何给一个系统类添加一个属性? 基于oc的ios开发,如何给一个系统类添加一个属性? 目前我知道可以用继承或类别实现.但不知道哪种方式好 解决方案 用类别: .h文件 #import @interface UIButton (TLButton) @property(nonatomic,strong,readwrite)NSNumber* cityId; @property(nonatomic,strong,readwrite)NSNumber* typeId; @