Attribute特性有哪些
这篇文章主要讲解了"Attribute特性有哪些",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"Attribute特性有哪些"吧!
复制代码 代码如下:
/*
*特性
*/
复制代码 代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
///
/// DisAttribute 的摘要说明
///
public class DisAttribute : Attribute
{
private string _message;
///
/// 描述
///
public string Message
{
get { return _message; }
}
public DisAttribute(string message)
{
this._message = message;
}
}
/*
*类
*/
复制代码 代码如下:
using System;
using System.Collections.Generic;
using System.EnterpriseServices;
using System.Linq;
using System.Web;
using System.Web.DynamicData;
///
/// User 的摘要说明
///
[DisAttribute("User"),TableName("user"),Description("user")]
public class User
{
private int? _id;
///
/// Id
///
[DisAttribute("主键")]
public int? Id
{
get { return _id; }
set { _id = value; }
}
private string _name;
///
/// 名称
///
[DisAttribute("名称")]
public string Name
{
get { return _name; }
set { _name = value; }
}
}
/*
*获取
*/
复制代码 代码如下:
//获取特性
User u = new User();
Type _t = u.GetType();
foreach (Attribute a in _t.GetCustomAttributes(true))
{
if (a.GetType().ToString() == "DisAttribute")
{
DisAttribute _da = (DisAttribute)a;
if (_da != null)
{
Response.Write(_da.Message + "
");
}
}
}
//获取所有属性
u.Id = 888888;
u.Name = "陈奕迅";
foreach (PropertyInfo item in _t.GetProperties())
{
//特性
Attribute atr = item.GetCustomAttribute(typeof(DisAttribute));
if (atr.GetType().ToString() == "DisAttribute")
{
DisAttribute _da = (DisAttribute)atr;
if (_da != null)
{
Response.Write(_da.Message + "
");
}
}
}
感谢各位的阅读,以上就是"Attribute特性有哪些"的内容了,经过本文的学习后,相信大家对Attribute特性有哪些这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是,小编将为大家推送更多相关知识点的文章,欢迎关注!