Attribute/特性心得随笔
更新时间:2013年11月05日 16:19:45 作者:
从事asp.net工作的相关人员对Attribute并不陌生吧,本文就来为大家介绍下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>");
}
}
}
相关文章
.NET的file文件上传控件使用方法 修改web.config文件上传大文件
这篇文章主要介绍了.NET修改web.config文件上传大文件的方法,大家参考使用吧2014-01-01
WCF如何绑定netTcpBinding寄宿到控制台应用程序详解
这篇文章主要给大家介绍了关于WCF如何绑定netTcpBinding寄宿到控制台应用程序的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用WCF具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧2019-07-07
ASP.NET Core 6.0 基于模型验证的数据验证功能
这篇文章主要介绍了ASP.NET Core 6.0 基于模型验证的数据验证,本文描述的数据验证方案,是基于官方的模型验证(Model validation),需要的朋友可以参考下2022-07-07


最新评论