Attribute/特性心得隨筆
更新時間:2013年11月05日 16:19:45 作者:
從事asp.net工作的相關(guān)人員對Attribute并不陌生吧,本文就來為大家介紹下Attribute特性,下面有個不錯的示例,喜歡的朋友感受下
復(fù)制代碼 代碼如下:
<p>/*</p><p>*特性</p><p>*/</p>
復(fù)制代碼 代碼如下:
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;
}
}
/*
*類
*/
復(fù)制代碼 代碼如下:
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; }
}
}
/*
*獲取
*/
復(fù)制代碼 代碼如下:
//獲取特性
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>");
}
}
}
相關(guān)文章
asp.net基于windows服務(wù)實現(xiàn)定時發(fā)送郵件的方法
這篇文章主要介紹了asp.net基于windows服務(wù)實現(xiàn)定時發(fā)送郵件的方法,結(jié)合實例形式較為詳細的分析了asp.net調(diào)用Windows系統(tǒng)服務(wù)的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-11-11在ASP.NET Core中實現(xiàn)一個Token base的身份認證實例
以前在web端的身份認證都是基于Cookie | Session的身份認證,本篇文章主要介紹了在ASP.NET Core中實現(xiàn)一個Token base的身份認證實例,有興趣的可以了解一下。2016-12-12ASP.NET MVC中為DropDownListFor設(shè)置選中項的方法
這篇文章主要介紹了ASP.NET MVC中為DropDownListFor設(shè)置選中項的方法,需要的朋友可以參考下2014-10-1012306動態(tài)驗證碼啟發(fā)之ASP.NET實現(xiàn)動態(tài)GIF驗證碼(附源碼)
這篇文章主要介紹了受到12306動態(tài)驗證碼啟發(fā),實現(xiàn)ASP.NET動態(tài)GIF驗證碼,需要的朋友可以參考下2015-08-08ASP.NET MVC中HtmlHelper控件7個大類中各個控件使用詳解
本文主要介紹HtmlHelper類控件的使用方法,給初涉MVC的朋友一些幫助,有需要的朋友可以參考一下。2016-03-03Json返回時間的格式中出現(xiàn)亂碼問題的兩種解決方案
使用Json返回數(shù)據(jù)的時候時間的格式一般都會變了,變成我們不認識的一些字符,那么當(dāng)我們遇到這些問題的時候我們該怎么解決呢,今天我就來小說一下這個的解決方法2013-10-10