Attribute/特性心得随笔

雪让人的觉得只有一个字——冷。大地一片银白,一片清洁,而雪花仍如柳絮,如棉花,如鹅毛从天空飘飘洒洒。
 
<p>/*</p><p>*特性</p><p>*/</p>

 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; /// <summary>
/// DisAttribute 的摘要说明
/// </summary>
public class DisAttribute : Attribute
{
private string _message;
/// <summary>
/// 描述
/// </summary>
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;
/// <summary>
/// User 的摘要说明
/// </summary>
[DisAttribute("User"),TableName("user"),Description("user")]
public class User
{
private int? _id;
/// <summary>
/// Id
/// </summary>
[DisAttribute("主键")]
public int? Id
{
get { return _id; }
set { _id = value; }
}
private string _name;
/// <summary>
/// 名称
/// </summary>
[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 + "<br>");
}
}
}
//获取所有属性
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 + "<br>");
}
}
}

到此这篇关于Attribute/特性心得随笔就介绍到这了。伤人之语,如水覆地,难以挽回。更多相关Attribute/特性心得随笔内容请查看相关栏目,小编编辑不易,再次感谢大家的支持!

您可能有感兴趣的文章
.NET获取枚举DescriptionAttribute描述信息性能改进的多种方法

使CheckBoxList的Attributes属性生效(修改微软的一个bug)