当前位置: 移动技术网 > IT编程>开发语言>.net > Attribute/特性心得随笔

Attribute/特性心得随笔

2017年12月12日  | 移动技术网IT编程  | 我要评论

女装少年百物语,游戏玩家,红糖哥

复制代码 代码如下:

<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>");
}
}
}

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网