Attribute/特性心得随笔

所属分类: 网络编程 / ASP.NET 阅读数: 1835
收藏 0 赞 0 分享
复制代码 代码如下:

<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>");
}
}
}
更多精彩内容其他人还在看

asp.net下大文件上传知识整理

asp.net下大文件上传知识整理
收藏 0 赞 0 分享

Asp.Net常用函数

Asp.Net常用函数
收藏 0 赞 0 分享

asp.net下用url重写URLReWriter实现任意二级域名的方法第1/2页

asp.net下用url重写URLReWriter实现任意二级域名的方法
收藏 0 赞 0 分享

在.NET中利用XMLHTTP下载文件的代码

在.NET中利用XMLHTTP下载文件的代码
收藏 0 赞 0 分享

在ASP.NET 中实现单点登录

在ASP.NET 中实现单点登录
收藏 0 赞 0 分享

c# .net 生成图片验证码的代码

c# .net 生成图片验证码的代码
收藏 0 赞 0 分享

asp.net中MD5 16位和32位加密函数

asp.net中MD5 16位和32位加密函数
收藏 0 赞 0 分享

自己常用到的自定义公共类(已测试通过)

自己常用到的自定义公共类(已测试通过)
收藏 0 赞 0 分享

ASP.NET 2.0下随机读取Access记录的实现方法

ASP.NET 2.0下随机读取Access记录的实现方法
收藏 0 赞 0 分享

.NET(C#)连接各类数据库代码-集锦

.NET(C#)连接各类数据库代码-集锦
收藏 0 赞 0 分享
查看更多