最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#根據(jù)反射和特性實(shí)現(xiàn)ORM映射實(shí)例分析

 更新時(shí)間:2015年04月02日 12:06:48   作者:xugang  
這篇文章主要介紹了C#根據(jù)反射和特性實(shí)現(xiàn)ORM映射的方法,實(shí)例分析了反射的原理、特性與ORM的實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下

本文實(shí)例講述了C#根據(jù)反射和特性實(shí)現(xiàn)ORM 映射的方法。分享給大家供大家參考。具體如下:

(一)關(guān)于反射

什么是反射?

反射就是在運(yùn)行時(shí),動(dòng)態(tài)獲取對(duì)象信息的方法。比如:運(yùn)行時(shí)獲得對(duì)象有哪些屬性,方法,委托等。

反射的作用?

能夠?qū)崿F(xiàn)運(yùn)行時(shí),動(dòng)態(tài)調(diào)用對(duì)象的方法,以及動(dòng)態(tài)設(shè)置、獲取屬性值等。

反射的示例:

using System;
using System.Reflection;
namespace CS_Test
{
  public class MyStudent
  {
    private string sName;
    public string SName
    {
      get { return sName; }
      set { sName = value; }
    }
    private int sAge;
    public int SAge
    {
      get { return sAge; }
      set { sAge = value; }
    }
  }
  class Test_Attribute
  {
    [STAThread]
    static void Main(string[] args)
    {
      MyStudent stu1 = new MyStudent();
      stu1.SName = "劉德華";
      stu1.SAge = 40;
      // 獲得所有屬性
      PropertyInfo[] pros = stu1.GetType().GetProperties();
      // 顯示所有屬性值
      foreach (PropertyInfo pro in pros)
       Console.WriteLine(pro.Name+":"+pro.GetValue(stu1,null));
      //更改stu1對(duì)象的SAge值
      stu1.GetType().GetProperty("SAge").SetValue(stu1, 30, null);
      // 顯示所有屬性值
      foreach (PropertyInfo pro in pros)
       Console.WriteLine(pro.Name+":"+pro.GetValue(stu1, null));
    }
  }
}

(二)關(guān)于特性

什么是 Attribute?

它是對(duì)運(yùn)行時(shí)的對(duì)象或?qū)ο蟮膶傩?、方法、委托等進(jìn)行描述的類。
Attribute 的作用?
用于在運(yùn)行時(shí),描述你的代碼或者影響你的程序的行為。

注意:

既然Attribute 是類,那么它的定義與定義類一樣。
唯一不同的就是,自定義Attribute 類必須繼承于System.Attribute 空間。
特性的示例:

//描述數(shù)據(jù)庫字段的 Attribute
public class DataFieldAttribute : Attribute
{
    public DataFieldAttribute(string fieldName,string fieldType)
    {
      this._fieldName = fieldName;
      this._fieldType = fieldType;
    }
    // 數(shù)據(jù)庫字段名
    private string _fieldName;
    public string FieldName
    {
      get { return _fieldName; }
      set { _fieldName = value; }
    }
    // 數(shù)據(jù)庫字段類型
    private string _fieldType;
    public string FieldType
    {
      get { return _fieldType; }
      set { _fieldType = value; }
    }
}

通過自定義Attribute,我們定義了類屬性和數(shù)據(jù)庫字段的一一對(duì)應(yīng)關(guān)系,于是對(duì)MyStudent 類的Name、Age 屬性都加上Attribute 的描述,指定他們對(duì)應(yīng)的數(shù)據(jù)庫字段名以及類型。
代碼更改如下:

public class MyStudent
{
    private string _name;
    //使用“特性”描述對(duì)應(yīng)的數(shù)據(jù)庫字段名、類型
    [DataFieldAttribute("SName", "varchar")]
    public string Name
    {
      get { return _name; }
      set { _name = value; }
    }
    private int _age;
    [DataFieldAttribute("SAge", "int")]
    public int Age
    {
      get { return _age; }
      set { _age = value; }
    }
}

(三)ORM 映射規(guī)則的定義

給實(shí)體類增加DataFieldAttribute 的描述,其實(shí)就是增加了O(對(duì)象)/ R(關(guān)系數(shù)據(jù)庫)的映射規(guī)則。
下面我們就通過反射的方法實(shí)現(xiàn):在運(yùn)行時(shí)動(dòng)態(tài)獲取O/R  Mapping 的映射規(guī)則:

static void Main(string[] args)
{
  MyStudent stu1 = new MyStudent();
  stu1.Name = "劉德華";
  stu1.Age = 40;
  //通過反射的方法來動(dòng)態(tài)獲取此映射規(guī)則
  PropertyInfo[] infos = stu1.GetType().GetProperties();
  object[] objs_fieldAttr = null;
  foreach (PropertyInfo info in infos)
  {
    // GetCustomAttributes: 
    // 返回實(shí)體對(duì)象中每個(gè)屬性對(duì)應(yīng)的“特性”信息(數(shù)據(jù)庫字段名、類型)
    objs_fieldAttr = info.GetCustomAttributes(
               typeof(DataFieldAttribute), false);
    if (objs_fieldAttr != null)
    {
      Console.Write("實(shí)體類的屬性名:" + info.Name);
      Console.Write(" -> 數(shù)據(jù)庫字段名:");
      Console.WriteLine(((DataFieldAttribute)objs_fieldAttr[0]).FieldName);
    }
  }
}

顯示結(jié)果:

實(shí)體類的屬性名:Name -> 數(shù)據(jù)庫字段名:SName
實(shí)體類的屬性名:Age -> 數(shù)據(jù)庫字段名:SAge
 
接下來的工作就是:怎樣根據(jù)這種方法動(dòng)態(tài)地從對(duì)象中獲取映射規(guī)則,然后動(dòng)態(tài)構(gòu)造Insert、Update、Delete 等 SQL 語句。這就是實(shí)現(xiàn)自己的ORM 的原理。
 
這里的代碼僅僅是舉例,而要真正實(shí)現(xiàn)一個(gè)ORM,我們還需要考慮的很多,比如:

1、實(shí)體類對(duì)應(yīng)于哪張數(shù)據(jù)庫表?
2、數(shù)據(jù)庫表中的 PK  和 FK(如果有的話)怎么表示?

完整代碼如下:

using System;
using System.Reflection;
namespace CS_Test
{
  public class MyStudent
  {
    private string _name;
    //使用“特性”描述對(duì)應(yīng)的數(shù)據(jù)庫字段名、類型
    [DataFieldAttribute("SName", "varchar")]
    public string Name
    {
      get { return _name; }
      set { _name = value; }
    }
    private int _age;
    [DataFieldAttribute("SAge", "int")]
    public int Age
    {
      get { return _age; }
      set { _age = value; }
    }
  }
  //描述數(shù)據(jù)庫字段的 Attribute
  public class DataFieldAttribute : Attribute
  {
    public DataFieldAttribute(string fieldName,string fieldType)
    {
      this._fieldName = fieldName;
      this._fieldType = fieldType;
    }
    // 數(shù)據(jù)庫字段名
    private string _fieldName;
    public string FieldName
    {
      get { return _fieldName; }
      set { _fieldName = value; }
    }
    // 數(shù)據(jù)庫字段類型
    private string _fieldType;
    public string FieldType
    {
      get { return _fieldType; }
      set { _fieldType = value; }
    }
  }
  class Test_Attribute
  {
    [STAThread]
    static void Main(string[] args)
    {
      MyStudent stu1 = new MyStudent();
      stu1.Name = "劉德華";
      stu1.Age = 40;
      // 獲得所有屬性
      PropertyInfo[] pros = stu1.GetType().GetProperties();
      // 顯示所有屬性值
      foreach (PropertyInfo pro in pros)
       Console.WriteLine(pro.Name+":"+pro.GetValue(stu1,null));
      //更改stu1對(duì)象的SAge值
      stu1.GetType().GetProperty("Age").SetValue(stu1, 30, null);
      // 顯示所有屬性值
      foreach (PropertyInfo pro in pros)
       Console.WriteLine(pro.Name+":"+pro.GetValue(stu1, null));

      //通過反射的方法來動(dòng)態(tài)獲取此映射規(guī)則
      PropertyInfo[] infos = stu1.GetType().GetProperties();
      object[] objs_fieldAttr = null;
      foreach (PropertyInfo info in infos)
      {
        // GetCustomAttributes: 
        // 返回實(shí)體中每個(gè)屬性對(duì)應(yīng)的“特性”信息(數(shù)據(jù)庫字段名、類型)
        objs_fieldAttr = info.GetCustomAttributes(
                typeof(DataFieldAttribute), false);
        if (objs_fieldAttr != null)
        {
          Console.Write("實(shí)體類的屬性名:" + info.Name);
          Console.Write(" -> 數(shù)據(jù)庫字段名:");
          Console.WriteLine(((DataFieldAttribute)objs_fieldAttr[0]).FieldName);
        }
      }
    }
  }
}

希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • C#中兩個(gè)byte如何相加

    C#中兩個(gè)byte如何相加

    可能有的看到這個(gè)題目就會(huì)覺得這不簡單嗎?直接用+號(hào)相加就行了,可是當(dāng)你實(shí)際操作運(yùn)行的時(shí)候就會(huì)發(fā)現(xiàn)有錯(cuò)誤了,那么是什么錯(cuò)誤?那該如何讓C#中兩個(gè)byte相加呢?通過下面這篇文章來一起學(xué)習(xí)學(xué)習(xí)吧。
    2016-11-11
  • C# 通過NI-VISA操作Tektronix TBS 2000B系列示波器的實(shí)現(xiàn)步驟

    C# 通過NI-VISA操作Tektronix TBS 2000B系列示波器的實(shí)現(xiàn)步驟

    這篇文章主要介紹了C# 通過NI-VISA操作Tektronix TBS 2000B系列示波器的實(shí)現(xiàn)步驟,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下
    2021-02-02
  • 教你C#將CSV轉(zhuǎn)為Excel的實(shí)現(xiàn)方法

    教你C#將CSV轉(zhuǎn)為Excel的實(shí)現(xiàn)方法

    這篇文章主要介紹了C#?將CSV轉(zhuǎn)為Excel,轉(zhuǎn)換之后可執(zhí)行更多關(guān)于數(shù)據(jù)編輯、格式設(shè)置等操作,代碼簡單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2022-03-03
  • C#中單問號(hào)(?)和雙問號(hào)(??)的用法整理

    C#中單問號(hào)(?)和雙問號(hào)(??)的用法整理

    本文詳細(xì)講解了C#中單問號(hào)(?)和雙問號(hào)(??)的用法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-05-05
  • C#實(shí)現(xiàn)設(shè)置電腦顯示器參數(shù)

    C#實(shí)現(xiàn)設(shè)置電腦顯示器參數(shù)

    這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)設(shè)置電腦顯示器參數(shù),文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下
    2022-12-12
  • C#使用晚綁定來實(shí)現(xiàn)壓縮Access數(shù)據(jù)庫的方法

    C#使用晚綁定來實(shí)現(xiàn)壓縮Access數(shù)據(jù)庫的方法

    這篇文章主要介紹了C#使用晚綁定來實(shí)現(xiàn)壓縮Access數(shù)據(jù)庫的方法,項(xiàng)目開發(fā)中有一定的實(shí)用價(jià)值,需要的朋友可以參考下
    2014-08-08
  • Unity屏幕雪花另類實(shí)現(xiàn)方式示例

    Unity屏幕雪花另類實(shí)現(xiàn)方式示例

    這篇文章主要介紹了Unity屏幕雪花另類實(shí)現(xiàn)方式示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • unity置灰處理的實(shí)現(xiàn)

    unity置灰處理的實(shí)現(xiàn)

    本文主要介紹了unity置灰處理的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-07-07
  • Unity實(shí)現(xiàn)人物平滑轉(zhuǎn)身

    Unity實(shí)現(xiàn)人物平滑轉(zhuǎn)身

    這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)人物平滑轉(zhuǎn)身,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-01-01
  • C#實(shí)現(xiàn)FTP客戶端的案例

    C#實(shí)現(xiàn)FTP客戶端的案例

    這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)FTP客戶端的小案例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-07-07

最新評(píng)論

黑河市| 怀安县| 垫江县| 普兰店市| 松阳县| 通榆县| 正安县| 肇庆市| 宜黄县| 靖边县| 儋州市| 娱乐| 于田县| 江川县| 云南省| 青冈县| 左云县| 边坝县| 徐汇区| 师宗县| 新营市| 保康县| 介休市| 清丰县| 雅江县| 固阳县| 巴彦县| 衡阳县| 甘德县| 岢岚县| 松原市| 肇州县| 蕲春县| 西畴县| 太原市| 德阳市| 新沂市| 从化市| 正安县| 蓝山县| 胶州市|