Attribute/特性心得随笔
2013-11-05 00:00
295 查看
<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/特性心得随笔
- Python的一些特性(随笔)
- 微软老将Philip Su的离职信:回首12年职场生涯的心得和随笔
- 微软老将Philip Su的离职信:回首12年职场生涯的心得和随笔
- 【Android测试】【随笔】搜狗、腾讯技术交流会心得
- 冰之随笔一(c#反射、特性)
- 心得20--JDK新特性4-框架开发之内省
- 一周听企业管理讲座学习随笔、心得(一)
- 心得随笔【项目管理】
- 菜鸟学U3D心得随笔
- python读写csv文件的心得(随笔一:简单的读写csv文件)
- ADO访问Access数据库错误解决心得随笔
- 心得20--JDK新特性4-框架开发之内省
- 心得22--JDK新特性6-反射1-反射入门及Class类
- 写点随笔关于建站的心得——新手建站必备
- 【编码随笔】了解C++11新特性
- 心得21--JDK新特性5-Apache开发的内省API
- Python学习随笔七:高级特性
- 心得22--JDK新特性6-反射1-反射入门及Class类