Attribute/特性心得隨筆
更新時(shí)間:2013年11月05日 16:19:45 作者:
從事asp.net工作的相關(guān)人員對(duì)Attribute并不陌生吧,本文就來(lái)為大家介紹下Attribute特性,下面有個(gè)不錯(cuò)的示例,喜歡的朋友感受下
復(fù)制代碼 代碼如下:
<p>/*</p><p>*特性</p><p>*/</p>
復(fù)制代碼 代碼如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// DisAttribute 的摘要說(shuō)明
/// </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 的摘要說(shuō)明
/// </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應(yīng)用程序性能的20種方法(簡(jiǎn)單有效)
增加asp.net應(yīng)用程序性能的20種方法小結(jié),需要的朋友可以參考下,對(duì)于服務(wù)器也需要一些設(shè)置。2010-01-01
Asp.NET 生成靜態(tài)頁(yè)面并分頁(yè)的代碼
主要的原理就是替換模板里的特殊字符。2010-03-03
.NET的file文件上傳控件使用方法 修改web.config文件上傳大文件
這篇文章主要介紹了.NET修改web.config文件上傳大文件的方法,大家參考使用吧2014-01-01
WCF如何綁定netTcpBinding寄宿到控制臺(tái)應(yīng)用程序詳解
這篇文章主要給大家介紹了關(guān)于WCF如何綁定netTcpBinding寄宿到控制臺(tái)應(yīng)用程序的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用WCF具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
ASP.NET?Core?6.0?基于模型驗(yàn)證的數(shù)據(jù)驗(yàn)證功能
這篇文章主要介紹了ASP.NET?Core?6.0?基于模型驗(yàn)證的數(shù)據(jù)驗(yàn)證,本文描述的數(shù)據(jù)驗(yàn)證方案,是基于官方的模型驗(yàn)證(Model validation),需要的朋友可以參考下2022-07-07
基于.NET程序默認(rèn)啟動(dòng)線程數(shù)講解
本篇文章小編為大家介紹,基于.NET程序默認(rèn)啟動(dòng)線程數(shù)講解。需要的朋友參考下2013-04-04

