.net使用自定义类属性
.net中可以使用Type.GetCustomAttributes获取类上的自定义属性,可以使用PropertyInfo.GetCustomAttributes获取属性信息上的自定义属性。
下面以定义一个简单数据库表的映射实体类来说明相关的使用方法,基于自定义类属性和自定义类中的属性的自定义属性,可以方便的进行类标记和类中属性的标记
创建一个类的自定义属性,用于标识数据库中的表名称,需要继承自Attribute类:
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
public sealed class TableAttribute : Attribute
{
private readonly string _TableName = "";
public TableAttribute(string tableName)
{
this._TableName = tableName;
}
public string TableName
{
get { return this._TableName; }
}
}
创建一个属性的自定义属性,用于标识数据库表中字段的名称,需要继承自Attribute类
[AttributeUsage(AttributeTargets.Property, Inherited = false, AllowMultiple = false)]
public class FieldAttribute : Attribute
{
private readonly string _FieldName = ""; ///数据库的字段名称
private System.Data.DbType _Type = System.Data.DbType.String; ///数据库的字段类型
public FieldAttribute(string fieldName)
{
this._FieldName=fieldName;
}
public FieldAttribute(string fieldName,System.Data.DbType type)
{
this._FieldName=fieldName;
this._Type=type;
}
public string FieldName
{
get { return this._FieldName; }
}
public System.Data.DbType Type
{
get{return this._Type;}
}
}
创建一个数据实体基类:
public class BaseEntity
{
public BaseEntity()
{
}
/// <summary>
/// 获取表名称
/// </summary>
/// <returns></returns>
public string GetTableName()
{
Type type = this.GetType();
object[] objs = type.GetCustomAttributes(typeof(TableAttribute), true);
if (objs.Length <= 0)
{
throw new Exception("实体类没有标识TableAttribute属性");
}
else
{
object obj = objs[0];
TableAttribute ta = (TableAttribute)obj;
return ta.TableName; //获取表名称
}
}
/// <summary>
/// 获取数据实体类上的FieldAttribute
/// </summary>
/// <param name="propertyName"></param>
/// <returns></returns>
public FieldAttribute GetFieldAttribute(string propertyName)
{
PropertyInfo field = this.GetType().GetProperty(propertyName);
if (field == null)
{
throw new Exception("属性名" + propertyName + "不存在");
}
object[] objs = field.GetCustomAttributes(typeof(FieldAttribute), true);
if (objs.Length <= 0)
{
throw new Exception("类体属性名" + propertyName + "没有标识FieldAttribute属性");
}
else
{
object obj = objs[0];
FieldAttribute fieldAttribute=(FieldAttribute)obj;
fieldAttribute.FieldValue=field.GetValue(this,null);
return fieldAttribute;
}
}
}
创建数据实体
[Table("Wincms_Dictionary")] ///映射到数据库的Wincms_Dictionary表
public class Wincms_Dictionary : BaseEntity
{
private int _DictionaryId;
public Wincms_Dictionary()
{
}
[Field("DictionaryId",DbType.Int32)] ///映射到数据库的Wincms_Dictionary表中的字段
public int DictionaryId
{
get { return this._DictionaryId; }
set
{
this._DictionaryId = value;
}
}
}
///基于实体类获取实体对应的表名称和字段名称
public class Test
{
public static void main(string[] args)
{
Wincms_Dictionary dict=new Wincms_Dictionary();
Console.WriteLine("表名称:"+GetTableName(dict));
Console.WriteLine("字段名称:"+GetFieldName(dict,"DictionaryId"));
Console.Read();
}
///获取实体表名称
public static string GetTableName(BaseEntity entity)
{
return entity.GetTableName();
}
///获取实体字段名称
public static string GetFieldName(BaseEntity entity,string propertyName)
{
FieldAttribute fieldAttribute=entity.GetFieldAttribute(propertyName);
return fieldAttribute.FieldName;
}
}
输出结果为:
表名称:Wincms_Dictionary
字段名称:DictionaryId
.net使用自定义类属性的更多相关文章
- 自定义类属性设置及setter、getter方法的内部实现
属性是可以说是面向对象语言中封装的一个体现,在自定义类中设置属性就相当于定义了一个私有变量.设置器(setter方法)以及访问器(getter方法),其中无论是变量的定义,方法的声明和实现都是系统自动 ...
- WPF中Image控件绑定到自定义类属性
首先我们定义一个Student类,有ID,Name,Photo(保存图片路径). using System; using System.Collections.Generic; using Syste ...
- 自定义类在PropertyGrid上的展示方法
自定义类在PropertyGrid上的展示方法 零.引言 PropertyGrid用来显示某一对象的属性,但是并不是所有的属性都能编辑,基本数据类型(int, double等)和.Net一些封装的类型 ...
- SpriteBuilder中不能编辑自定义类或不能给节点添加属性的解决
不能编辑自定义类 你选中一个Sub File(CCBFile)节点,在这个例子中,该节点的Custom class区域灰化禁用且不能修改.这是因为你需要在该Sub File引用的CCB文件中修改Cus ...
- Qt+QGIS二次开发:自定义类实现查询矢量数据的属性字段值(图查属性)
在GIS领域,有两种重要的查询操作,图查属性和属性查图. 本文主要介绍如何在QGIS中通过从QgsMapToolIdentify中派生自定义类实现查询矢量数据的属性字段值(图查属性). 重点参考资料: ...
- 第六种方式,python使用cached_property缓存装饰器和自定义cached_class_property装饰器,动态添加类属性(三),selnium webdriver类无限实例化控制成单浏览器。
使用 from lazy_object_proxy.utils import cached_property,使用这个装饰器. 由于官方的行数比较少,所以可以直接复制出来用自己的. class cac ...
- [Yii2.0] 以Yii 2.0风格加载自定义类或命名空间 [配置使用Yii2 autoloader]
Yii 2.0最显著的特征之一就是引入了命名空间,因此对于自定义类的引入方式也同之前有所不同.这篇文章讨论一下如何利用Yii 2.0的自动加载机制,向系统中引入自定义类和命名空间.本文旨在抛砖引玉,如 ...
- JS面向对象(1) -- 简介,入门,系统常用类,自定义类,constructor,typeof,instanceof,对象在内存中的表现形式
相关链接: JS面向对象(1) -- 简介,入门,系统常用类,自定义类,constructor,typeof,instanceof,对象在内存中的表现形式 JS面向对象(2) -- this的使用,对 ...
- Android之探究viewGroup自定义子属性参数的获取流程
通常会疑惑,当使用不同的布局方式时,子view得布局属性就不太一样,比如当父布局是LinearLayout时,子view就能有效的使用它的一些布局属性如layout_weight.weightSum. ...
随机推荐
- 添加到sudo组里
$visudo //切记,此处没有vi和sudo之间没有空格 1.移动光标,到最后一行(最好是找到root ALL=(ALL) ALL,在下面添加一行) 2.按a,进入append模式3.输入 ...
- jquery 美化弹出提示 漂亮的Dialog 对话框
三个不同的效果,分别是普通的警告,确认/取消,带一个输入框 本例用了jquery.alertify.js,请到演示页面查看 css文件也请到演示页面查看 JavaScript Code <scr ...
- Hibernate (开源对象关系映射框架)
一.基本介绍1.它对JDBC进行了非常轻量级的对象封装,它将POJO与数据库表建立映射关系,是一个全自动的orm(对象关系映射)框架,hibernate可以自动生成SQL语句,自动执行: Hibern ...
- Es6懒加载
const Login = resolve => require(['@/components/Login'], resolve) 注(当路由被访问的时候才加载这个组件)
- Windows 消息循环(1) - 概览
本文从消息循环是如何驱动程序的这个角度,对 Windows 消息循环进行概览性介绍. 使用 EN5 课件获得更好的阅读体验: [希沃白板5]课件分享 : <Windows培训 - 消息循环> ...
- pool创建多进程
这中方式用的比较多,毕竟要控制并发数量,不可能不限制并发数 #_*_coding:utf-8_*_ from multiprocessing import Pool import time def f ...
- 我的OI生涯 第二章
第二章 很快就开学了,我们几个到了16班,开哥茂嵩到了15班,MJX和ZZJ去了四班,YST去了九班. 竞赛课改成了一周两节,每周都学习一点新的语言,这期间来了一些新同学例如于北,LTY,他们刚来感觉 ...
- Codeforces 1037 H. Security
\(>Codeforces \space 1037\ H. Security<\) 题目大意 : 有一个串 \(S\) ,\(q\) 组询问,每一次给出一个询问串 \(T\) 和一个区间 ...
- luoguP3232 [HNOI2013]游走 贪心 + 概率期望 + 高斯消元
首先,题目中的无向简单连通图代表着没有自环,重边... 总分的期望 = 每条边的期望之和...................每条边的期望又可以拆成$u \to v$的期望和$v \to u$的期望 ...
- HDU5904 LCIS 水题
http://acm.hdu.edu.cn/showproblem.php?pid=5904:// 说是LCIS其实和LCIS没有一点儿关系的水题. 代码 #include<cstdio> ...