.net使用自定義類(lèi)屬性實(shí)例_.Net教程
推薦:.NET實(shí)現(xiàn)在網(wǎng)頁(yè)中預(yù)覽Office文件的3個(gè)方法近日公司要搞一個(gè)日常的文檔管理的東東,可以上傳、下載各種文件,如果是office文件呢還必須得支持預(yù)覽功能,其他的都好說(shuō)但是唯獨(dú)office預(yù)覽功能比較麻煩,但是不能不做,廢話不多說(shuō)了一步步來(lái)吧。分析了下網(wǎng)易郵箱的文件預(yù)覽功能,他用的是微軟的組件,最早叫Office
一般來(lái)說(shuō),在.net中可以使用Type.GetCustomAttributes獲取類(lèi)上的自定義屬性,可以使用PropertyInfo.GetCustomAttributes獲取屬性信息上的自定義屬性。
下面以定義一個(gè)簡(jiǎn)單數(shù)據(jù)庫(kù)表的映射實(shí)體類(lèi)來(lái)說(shuō)明相關(guān)的使用方法,基于自定義類(lèi)屬性和自定義類(lèi)中的屬性的自定義屬性,可以方便的進(jìn)行類(lèi)標(biāo)記和類(lèi)中屬性的標(biāo)記
創(chuàng)建一個(gè)類(lèi)的自定義屬性,用于標(biāo)識(shí)數(shù)據(jù)庫(kù)中的表名稱(chēng),需要繼承自Attribute類(lèi):
代碼如下: [AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
public sealed class TableAttribute : Attribute
{
private readonly string _TableName = "";
public TableAttribute(string tableName)
{
this._TableName = tableName;
}
public string TableName
{
get { return this._TableName; }
}
}
創(chuàng)建一個(gè)屬性的自定義屬性,用于標(biāo)識(shí)數(shù)據(jù)庫(kù)表中字段的名稱(chēng),需要繼承自Attribute類(lèi):
代碼如下: [AttributeUsage(AttributeTargets.Property, Inherited = false, AllowMultiple = false)]
public class FieldAttribute : Attribute
{
private readonly string _FieldName = ""; ///數(shù)據(jù)庫(kù)的字段名稱(chēng)
private System.Data.DbType _Type = System.Data.DbType.String; ///數(shù)據(jù)庫(kù)的字段類(lèi)型
public FieldAttribute(string fieldName)
{
this._FieldName=fieldName;
}
public FieldAttribute(string fieldName,System.Data.DbType type)
{
this._FieldName=fieldName;
this._Type=type;
}
public string FieldName
{
get { return this._FieldName; }
}
public System.Data.DbType Type
{
get{return this._Type;}
}
}
創(chuàng)建一個(gè)數(shù)據(jù)實(shí)體基類(lèi):
代碼如下: public class BaseEntity
{
public BaseEntity()
{
}
/// <summary>
/// 獲取表名稱(chēng)
/// </summary>
/// <returns></returns>
public string GetTableName()
{
Type type = this.GetType();
object[] objs = type.GetCustomAttributes(typeof(TableAttribute), true);
if (objs.Length <= 0)
{
throw new Exception("實(shí)體類(lèi)沒(méi)有標(biāo)識(shí)TableAttribute屬性");
}
else
{
object obj = objs[0];
TableAttribute ta = (TableAttribute)obj;
return ta.TableName; //獲取表名稱(chēng)
}
}
/// <summary>
/// 獲取數(shù)據(jù)實(shí)體類(lèi)上的FieldAttribute
/// </summary>
/// <param name="propertyName"></param>
/// <returns></returns>
public FieldAttribute GetFieldAttribute(string propertyName)
{
PropertyInfo field = this.GetType().GetProperty(propertyName);
if (field == null)
{
throw new Exception("屬性名" + propertyName + "不存在");
}
object[] objs = field.GetCustomAttributes(typeof(FieldAttribute), true);
if (objs.Length <= 0)
{
throw new Exception("類(lèi)體屬性名" + propertyName + "沒(méi)有標(biāo)識(shí)FieldAttribute屬性");
}
else
{
object obj = objs[0];
FieldAttribute fieldAttribute=(FieldAttribute)obj;
fieldAttribute.FieldValue=field.GetValue(this,null);
return fieldAttribute;
}
}
}
創(chuàng)建數(shù)據(jù)實(shí)體:
分享:asp.net中控制反轉(zhuǎn)怎么理解?對(duì)IOC的解釋為:Inversion of control is a common characteristic of frameworks, so saying that these lightweight containers are special because they use inversion of control is like saying my car is special because it has wheels. 我想對(duì)這一概念執(zhí)行
- .NET實(shí)現(xiàn)在網(wǎng)頁(yè)中預(yù)覽Office文件的3個(gè)方法
- asp.net中控制反轉(zhuǎn)怎么理解?
- delphi選擇文件夾例子
- asp.net中C++單例實(shí)現(xiàn)問(wèn)題分析
- 簡(jiǎn)單理解Web Service三種實(shí)現(xiàn)方式
- Asp.net中Ajax與JQuery的ready函數(shù)沖突怎么辦
- asp.net中Repeater控件用法筆記
- asp.net中導(dǎo)出excel數(shù)據(jù)的方法匯總
- Asp.Net 上傳圖片并生成高清晰縮略圖
- 服務(wù)器安全狗導(dǎo)致ASP.NET網(wǎng)站運(yùn)行出錯(cuò)的一個(gè)案例
- Asp.Net其他頁(yè)面如何調(diào)用Web用戶(hù)控件寫(xiě)的分頁(yè)
- ASP.NET中上傳并讀取Excel文件數(shù)據(jù)示例
.Net教程Rss訂閱編程教程搜索
.Net教程推薦
- 用事實(shí)說(shuō)話!AJAX應(yīng)用程序開(kāi)發(fā)七宗罪
- ASP.NET 設(shè)計(jì)中的 N 個(gè)技巧
- 三層+存儲(chǔ)過(guò)程實(shí)現(xiàn)分頁(yè)示例代碼
- ASP.NET中Datagrid常見(jiàn)錯(cuò)誤
- 談對(duì)程序開(kāi)發(fā)中異常的處理
- 在.net中如何利用數(shù)據(jù)工廠實(shí)現(xiàn)多數(shù)據(jù)庫(kù)的操作
- 講解asp.net的異常處理機(jī)制
- c# 連接字符串?dāng)?shù)據(jù)庫(kù)服務(wù)器端口號(hào)
- 基于C#的接口基礎(chǔ)教程之二
- ASP.NET調(diào)用oracle存儲(chǔ)過(guò)程實(shí)現(xiàn)快速分頁(yè)
- 相關(guān)鏈接:
- 教程說(shuō)明:
.Net教程-.net使用自定義類(lèi)屬性實(shí)例
。