.net使用自定義類屬性實例_.Net教程

      編輯Tag賺U幣
      教程Tag:暫無Tag,歡迎添加,賺取U幣!

      推薦:.NET實現在網頁中預覽Office文件的3個方法
      近日公司要搞一個日常的文檔管理的東東,可以上傳、下載各種文件,如果是office文件呢還必須得支持預覽功能,其他的都好說但是唯獨office預覽功能比較麻煩,但是不能不做,廢話不多說了一步步來吧。分析了下網易郵箱的文件預覽功能,他用的是微軟的組件,最早叫Office

       一般來說,在.net中可以使用Type.GetCustomAttributes獲取類上的自定義屬性,可以使用PropertyInfo.GetCustomAttributes獲取屬性信息上的自定義屬性。

       
      下面以定義一個簡單數據庫表的映射實體類來說明相關的使用方法,基于自定義類屬性和自定義類中的屬性的自定義屬性,可以方便的進行類標記和類中屬性的標記
       
      創建一個類的自定義屬性,用于標識數據庫中的表名稱,需要繼承自Attribute類:

       

      代碼如下: [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; }
              }
      }

       

      創建一個屬性的自定義屬性,用于標識數據庫表中字段的名稱,需要繼承自Attribute類:

       

       代碼如下: [AttributeUsage(AttributeTargets.Property, Inherited = false, AllowMultiple = false)]
      public class FieldAttribute : Attribute
      {
              private readonly string _FieldName = "";    ///數據庫的字段名稱
              private System.Data.DbType _Type = System.Data.DbType.String;   ///數據庫的字段類型
       
              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;}
              }
      }


       
      創建一個數據實體基類:

       

       

      代碼如下: public class BaseEntity
      {
              public BaseEntity()
              {
              }
       
               /// <summary>
              /// 獲取表名稱
              /// </summary>
              /// <returns></returns>
              public string GetTableName()
              {
                  Type type = this.GetType();
                  object[] objs = type.GetCustomAttributes(typeof(TableAttribute), true);
                  if (objs.Length <= 0)
                  {
                      throw new Exception("實體類沒有標識TableAttribute屬性");
                  }
                  else
                  {
                      object obj = objs[0];
                      TableAttribute ta = (TableAttribute)obj;
                      return ta.TableName;                            //獲取表名稱
                  }
              }
              /// <summary>
              /// 獲取數據實體類上的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("類體屬性名" + propertyName + "沒有標識FieldAttribute屬性");
                  }
                  else
                  {
                      object obj = objs[0];
                      FieldAttribute fieldAttribute=(FieldAttribute)obj;
                      fieldAttribute.FieldValue=field.GetValue(this,null);
                      return fieldAttribute;
                  }
              }
      }


       
      創建數據實體:

       

      分享:asp.net中控制反轉怎么理解?
      對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. 我想對這一概念執行

      共2頁上一頁12下一頁
      來源:模板無憂//所屬分類:.Net教程/更新時間:2014-10-15
      相關.Net教程