Attribute/特性心得随笔
程序员文章站
2024-02-27 15:15:21
复制代码 代码如下:
/*
*特性
*/
复制代码 代码如下...
复制代码 代码如下:
<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>");
}
}
}