国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

使用C#輕松編寫.Net組件

2024-07-10 13:07:27
字體:
供稿:網(wǎng)友
注冊(cè)會(huì)員,創(chuàng)建你的web開發(fā)資料庫,

在.net框架提出之前,編寫組件被視為是一種需要高深技巧的工作,令很多人望而生畏。而.net的出現(xiàn),使得組件的編寫變得如此平易近人,而.net framework的核心語言c#,更是被稱為面向組件的語言。在這里,我將向大家介紹如何使用c#編寫在.net framework環(huán)境下運(yùn)行的組件,包括如何編寫組件類,如何添加域、屬性以及事件,如何編譯和分發(fā)組件。
  
  
  
  
  
  
  
  首先看下面這段足夠簡(jiǎn)單的代碼實(shí)例(在后面我們將慢慢將它變成一個(gè)五臟俱全的組件):
  
  using system;
  namespace componentcs
  {
  public class stringcomponent
  {
  private string[] stringsset;
  public int stringlength
  {
  get
  {
  return stringsset.length;
  }
  }
  public void modify(int index,string value)
  {
  if ((index < 0) || (index >= stringsset.length))
  {
  throw new indexoutofrangeexception();
  }
  else
  {
  stringsset[index]=value;
  onmodify();
  }
  }
  public stringcomponent()
  {
  stringsset = new string[]
  {
  "c# string 0",
  "c# string 1",
  "c# string 2",
  "c# string 3"
  };
  }
  public string getstring(int index)
  {
  if ((index < 0) || (index >= stringsset.length))
  {
  throw new indexoutofrangeexception();
  }
  return stringsset[index];
  }
  }
  }

一般地,我們首先創(chuàng)建一個(gè)命名空間(namespace)用來封裝這個(gè)組件中一系列的類:
  
  namespace compcs
  
  命名空間的使用非常靈活,它可以被嵌套,也可以將其內(nèi)部的類分別寫在多個(gè)文件中,相應(yīng)地,還可以在一個(gè)源文件中聲明多個(gè)非嵌套的命名空間。下面是一個(gè)使用嵌套的命名空間的示例代碼:
  
  namespace nestit{ namespace nestednamespace { class myclass { public static void dosth() { ... } } }}
  
  你可以這樣引用類myclass:
  
  nestit.nestednamespace.myclass.dosth();
  
  還是回到我們的命名空間compcs,我們使用下面的語句聲明了一個(gè)類stringcomponent:
  
  public class stringcomponent
  
  命名空間中的類是必需的,因?yàn)閏#所有的代碼都必須封裝在一個(gè)個(gè)類中,所以沒有類的命名空間沒有任何價(jià)值。
  
  下面我們?yōu)檫@個(gè)類添加一個(gè)公共(public)域:
  
  private string[] stringsset;
  
  此外,它還可能需要定義一些屬性,下面是定義一個(gè)只讀的屬性的例子:
  
  public int stringlength{ get { return stringsset.length; }}
  
  c#中的屬性更充分地體現(xiàn)了對(duì)象的封裝性,不直接操作類的數(shù)據(jù)內(nèi)容而是通過訪問器進(jìn)行訪問,它借助于get 和set訪問器對(duì)屬性的值進(jìn)行讀寫。而在c++中,這是需要程序員手工完成的一項(xiàng)工作。
  
  在屬性的訪問聲明中:
  
     .只有set 訪問器表明屬性的值只能進(jìn)行設(shè)置而不能讀出
  
     .只有g(shù)et 訪問器表明屬性的值是只讀的不能改寫
  
     .同時(shí)具有set 訪問器和get 訪問器表明屬性的值的讀寫都是允許的
  
  你或許會(huì)發(fā)現(xiàn)域和屬性是如此相似,確實(shí)屬性和域的語法比較類似的,但是它們是絕對(duì)不同的,區(qū)別在于你不能把屬性當(dāng)做變量那樣使用,也不能把屬性作為引用型參數(shù)或輸出參數(shù)來進(jìn)行傳遞,相反的,對(duì)于域,沒有這些限制。
  
  下面的代碼定義了這個(gè)類的構(gòu)造函數(shù):
  
  public stringcomponent()
  
  構(gòu)造函數(shù)必須與類同名,它可以重載,但是不能有返回值,因此它也沒有返回值類型前綴。當(dāng)用戶新建一個(gè)類的實(shí)例時(shí),構(gòu)造函數(shù)就會(huì)自動(dòng)執(zhí)行,同時(shí),c#的垃圾收集機(jī)制開始對(duì)這個(gè)實(shí)例進(jìn)行管理,并且將在適當(dāng)?shù)臅r(shí)候回收資源。
然后,我們編寫了一個(gè)getstring()函數(shù),這個(gè)函數(shù)根據(jù)用戶傳入的index值,返回相應(yīng)的記錄:
  
  public string getstring(int index)
  { … return stringsset[index];}
  
  要注意的是其中的異常處理的方法:
  
  throw new indexoutofrangeexception();
  
  作為一個(gè)健壯的組件,異常處理機(jī)制是不可或缺的,雖然它可能會(huì)消耗掉一些資源,但是它帶來的安全性的提升會(huì)使你覺得消耗的資源簡(jiǎn)直微不足道。這里使用了一個(gè)系統(tǒng)定義的異常類indexoutofrangeexception(),事實(shí)上,更多的情況是你必須自己定義異常類,以適應(yīng)各種不同的情況。下面的代碼示例展示了如何定義一個(gè)異常類:
  
  public class myapplicationexception : applicationexception{ public string amsg;
  public myapplicatonexception(string strmsg)
  {
  amsg=strmsg;
  }
  }
  
  定義一個(gè)異常類與定義普通的類并沒有什么區(qū)別,唯一的區(qū)別在于異常類必須繼承自system.exception類。事實(shí)上,微軟公司推薦把所有用戶自定義的異常類作為applicationexception類的子類。把類myapplicationexception放到命名空間compcs中,這樣你就可以改寫getstring()函數(shù)中的異常處理方式。下面是一個(gè)帶有更完善的異常處理機(jī)制的getstring()方法:
  
  public string getstring(int index) { try { if ((index < 0) || (index >= stringsset.length)) { throw new myapplicationexception("參數(shù)超出范圍"); } } catch(myapplicationexception merr) { console.writeline(merr.amsg); } catch(exception err) { console.writeline(err.message); }
  return stringsset[index];
  }
  
  采用類似這樣的方式,你可以應(yīng)付比這復(fù)雜得多的情況。
  
  下面,我們來考慮給這個(gè)類添加事件。事件機(jī)制的引入使得開發(fā)者可以更靈活地開發(fā)程序。下面的代碼示例展示了如何定義一個(gè)事件:
  
  public event eventhandler modified;
  
  在c#中使用event關(guān)鍵字定義事件。把這個(gè)定義放到我們的類componentcs.stringcomponent中,然后我們添加一個(gè)函數(shù)modify(),這個(gè)函數(shù)修改字符數(shù)組stringsset中指定位置的值,同時(shí)引發(fā)onmodify事件,而在modify事件中,我們調(diào)用的是事件modified所指定的函數(shù):
  
  public void modify(int index,string value) { if ((index < 0) || (index >= stringsset.length)) { throw new indexoutofrangeexception(); } else { stringsset[index]=value; onmodify(); } }
  private void onmodify()
  {
  eventargs e=new eventargs();
  if(!(modified==null))
  modified(this,e);
  }
  
  然后我們可以用如下的方法調(diào)用:
  
  private void doit(){ stringcomponent mysc=new stringcomponent(); mysc.modified+=new eventhandler(called); mysc.modify(2,"another string");}public void called(object o,eventargs e){ console.writeline("changed");}
  
  
  在函數(shù)doit()中,我們首先建立了一個(gè)stringcomponent類的對(duì)象mysc,然后將它的mofidied事件關(guān)聯(lián)到called()方法:
  
  mysc.modified+=new eventhandler(called);
  
  注意“+=”符號(hào)的使用,相反地,如果使用“-=”符號(hào),可以取消這個(gè)事件的綁定。
  
  現(xiàn)在我們得到了一個(gè)雖然簡(jiǎn)單,但是比較完整的組件類:
  
  using system;
  namespace componentcs
  {
  public class stringcomponent
  {
  
  private string[] stringsset;
  public event eventhandler modified;
  public int stringlength
  {
  get
  {
  return stringsset.length;
  }
  }
  public void modify(int index,string value)
  {
  if ((index < 0) || (index >= stringsset.length))
  {
  throw new indexoutofrangeexception();
  }
  else
  {
  stringsset[index]=value;
  onmodify();
  }
  }
  private void onmodify()
  {
  eventargs e=new eventargs();
  if(!(modified==null))
  modified(this,e);
  }
  public stringcomponent()
  {
  stringsset = new string[]
  {
  "c# string 0",
  "c# string 1",
  "c# string 2",
  "c# string 3"
  };
  }
  public string getstring(int index)
  {
  if ((index < 0) || (index >= stringsset.length))
  {
  throw new indexoutofrangeexception();
  }
  return stringsset[index];
  }
  }
  }
  
  
  最后要做的就是把它編譯成.dll(動(dòng)態(tài)鏈接庫)文件,以便發(fā)布。發(fā)布成.dll文件最大的好處就是.dll文件中的內(nèi)容已經(jīng)編譯,可以大大加快程序運(yùn)行速度,此外還可以保護(hù)源代碼。
  
  將產(chǎn)生的.cs文件編譯成為.dll文件的方法如下:
  
  csc.exe /t:library /debug+ /out:mycom.dll example.cs
  
  這樣就輸出了名為mycom.dll的.dll文件。
  
  ok,我們已經(jīng)完成一個(gè)組件,麻雀雖小,五臟俱全,這就是一切組件的基礎(chǔ)了,整個(gè)過程花不了十分鐘。
   當(dāng)然,如果是一個(gè)具備實(shí)際使用價(jià)值的組件,我們要考慮的遠(yuǎn)遠(yuǎn)不止這些,但是可以看到,c#對(duì)組件的強(qiáng)大支持,可以大大提高我們的開發(fā)效率,從而使我們有更多的精力放在算法設(shè)計(jì)等方面,開發(fā)出更加出色的組件。 

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 苍溪县| 称多县| 革吉县| 峨眉山市| 南川市| 左权县| 长治县| 周至县| 泽普县| 镇赉县| 锡林郭勒盟| 宜城市| 浙江省| 神农架林区| 化隆| 嘉善县| 泾源县| 竹溪县| 安多县| 白河县| 阜城县| 南京市| 深州市| 淳安县| 延寿县| 蓝田县| 通州市| 盐城市| 沂源县| 新乡市| 集贤县| 疏附县| 定陶县| 绥化市| 安顺市| 明星| 彭泽县| 万安县| 连城县| 扶余县| 潼南县|