Attribute高級(jí)應(yīng)用:簡(jiǎn)化ANF自定義控件初始化過(guò)程_.Net教程
推薦:如何在VB.NET中利用Split和Replace函數(shù)計(jì)算字?jǐn)?shù)Split函數(shù)使你能夠?qū)㈤L(zhǎng)字符串分離為單獨(dú)的字;但是如果在字與字之間不止一個(gè)空格,Split就會(huì)返回一個(gè)錯(cuò)誤的結(jié)果。為了防止這種情況發(fā)生,你可以在使用Split之前用Replace函數(shù)來(lái)替換多個(gè)空格的出現(xiàn)。列表A給出了一個(gè)例子。 列表A Private Sub CountWords()Dim
Attribute應(yīng)用,簡(jiǎn)化ANF自定義控件初始化過(guò)程
研究ANF的源碼,讓我獲益良多。其中很多思想,都是非常值得學(xué)習(xí)的。其中換膚的方式,寶玉已經(jīng)介紹過(guò)了,《Asp.Net Forums2.0深入分析》之 Asp.Net Forums是如何實(shí)現(xiàn)代碼分離和換皮膚的。不過(guò),當(dāng)一個(gè)自定義控件中服務(wù)器端控件比較多的時(shí)候,InitializeSkin方法的實(shí)現(xiàn)代碼就有點(diǎn)煩人了,比如看一下AdminSiteSettings的代碼。實(shí)在是非常之煩人。模式都一樣,如TextBox DisableSiteReason=skin.FindControl("DisableSiteReason") as TextBox之類(lèi)。所以今天想利用Attribute來(lái)簡(jiǎn)化一下。
首先我們要添加一個(gè)Attribute類(lèi),暫且就叫做BindControlAttribute:
using System;using System.Collections.Generic;using System.Text;using System.Reflection;namespace AspNetForums.Controls{[AttributeUsage(AttributeTargets.Field|AttributeTargets.Property,AllowMultiple=false)]class BindControlAttribute:Attribute{string _ctrlID;public BindControlAttribute(string ctrlID){_ctrlID = ctrlID;}public string ControlID{get { return _ctrlID; }}}}
這個(gè)類(lèi)Attribute功能比較簡(jiǎn)單,就是讓這個(gè)Attribute記錄字段要綁定到的控件的ID。
第二步就是修改SkinnedForumWebControl了,主要是添加一個(gè)方法:
private void InitializeFields(Control skin){FieldInfo[] fields = this.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly);foreach (FieldInfo fi in fields){if (fi.IsDefined(typeof(BindControlAttribute), false)){BindControlAttribute bind = fi.GetCustomAttributes(typeof(BindControlAttribute), false)[0] as BindControlAttribute;object ctrl = skin.FindControl(bind.ControlID);fi.SetValue(this, ctrl);}}}
其實(shí)在fi.IsDefined(typeof(BindControlAttribute), false)這里我本想再加一個(gè)條件的,就是限制字段的類(lèi)型是System.Web.UI.Control或者它的子類(lèi),可是試了幾種方法,都沒(méi)能成功。請(qǐng)知道的指點(diǎn)一下...
記得要引用System.Reflection名稱(chēng)空間,還要改一下CreateChildControls方法的實(shí)現(xiàn):
protected override void CreateChildControls(){Control skin = null;if (inlineSkin != null){inlineSkin.InstantiateIn(this);InitializeSkin(this);}else{// Load the skinskin = LoadSkin();//Initialize the fieldsInitializeFields(skin);//就加這一行// Initialize the skinInitializeSkin(skin);Controls.Add(skin);}}
到這里,實(shí)現(xiàn)的任務(wù)就完成了。下面就是應(yīng)用了。應(yīng)用是比較簡(jiǎn)單的,只需要在定義字段的時(shí)候,加上這個(gè)BindControlAttribute就行了。如:
uing System;using System.Collections;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using AspNetForums.Components;using AspNetForums.Enumerations;namespace AspNetForums.Controls{/// /// 論壇組列表服務(wù)器控件/// public class ForumGroupView : SkinnedForumWebControl{#region 成員字段private ForumContext forumContext = ForumContext.Current;private string skinFilename = "View-ForumGroupView.ascx";[BindControl("forumGroupRepeater")]//定義字段時(shí)加這么一行private Repeater repeater;#endregion public ForumGroupView(){// Assign a default template nameif (SkinFilename == null)SkinFilename = skinFilename;}#endregion #region 控件初始化// *********************************************************************// Initializeskin///// /// Initializes the user control loaded in CreateChildControls. Initialization/// consists of finding well known control names and wiring up any necessary events./// /// // ********************************************************************/ protected override void InitializeSkin(Control skin){//repeater = (Repeater) skin.FindControl("forumGroupRepeater");//這一行就沒(méi)有用了。DataBind();}
感覺(jué)是不是好點(diǎn)呢?
第一次用反射,著實(shí)費(fèi)了我不小的功夫(主要是看書(shū)不認(rèn)真:-))。我要獲取私有字段的時(shí)候,試了好多次才試出來(lái),后邊三年BindingFlags貌似都得加上,少一個(gè)都不行。另外,我發(fā)現(xiàn),Attribute構(gòu)造函數(shù)是在調(diào)用GetCustomAttributes函數(shù)時(shí)才調(diào)用的。不知道是否有二班的情況呢?
分享:細(xì)說(shuō).Net開(kāi)發(fā)中的Visual Basic.Net概要 VB正在不斷地發(fā)展中,它具備了以前VB編程人員作夢(mèng)都想擁有的性能,想象一下你將隨心所欲的利用這些性能,是不是很令人激動(dòng)?然而,這個(gè)計(jì)劃于2001年第四季度上市銷(xiāo)售的VB版本可能會(huì)給你帶來(lái)些小麻煩,因?yàn)橐耆莆账枰粋(gè)較長(zhǎng)的學(xué)習(xí)周期,而且其中
- asp.net如何得到GRIDVIEW中某行某列值的方法
- .net SMTP發(fā)送Email實(shí)例(可帶附件)
- js實(shí)現(xiàn)廣告漂浮效果的小例子
- asp.net Repeater 數(shù)據(jù)綁定的具體實(shí)現(xiàn)
- Asp.Net 無(wú)刷新文件上傳并顯示進(jìn)度條的實(shí)現(xiàn)方法及思路
- Asp.net獲取客戶(hù)端IP常見(jiàn)代碼存在的偽造IP問(wèn)題探討
- VS2010 水晶報(bào)表的使用方法
- ASP.NET中操作SQL數(shù)據(jù)庫(kù)(連接字符串的配置及獲取)
- asp.net頁(yè)面?zhèn)髦禍y(cè)試實(shí)例代碼
- DataGridView - DataGridViewCheckBoxCell的使用介紹
- asp.net中javascript的引用(直接引入和間接引入)
- 三層+存儲(chǔ)過(guò)程實(shí)現(xiàn)分頁(yè)示例代碼
.Net教程Rss訂閱編程教程搜索
.Net教程推薦
- 解析.NET FileStreams如何將DTD插入XML文件中
- 談ASP.NET中XML數(shù)據(jù)的處理
- 幾種判斷asp.net中session過(guò)期方法的比較
- 怎樣用Asp.net代碼在虛擬主機(jī)實(shí)現(xiàn)Urlrewrite
- 總結(jié)歸納DataBinder.Eval使用方法
- .net中使用DatagridView的增刪改方法
- 淺析ASP.NET全局異常處理
- 學(xué)習(xí)Asp.Net經(jīng)常會(huì)用到的函數(shù)集
- ASP.Net中保護(hù)自定義的服務(wù)器控件
- .net全局定時(shí)定期執(zhí)行某些操作在Global.asax中具體實(shí)現(xiàn)
- 相關(guān)鏈接:
復(fù)制本頁(yè)鏈接| 搜索Attribute高級(jí)應(yīng)用:簡(jiǎn)化ANF自定義控件初始化過(guò)程
- 教程說(shuō)明:
.Net教程-Attribute高級(jí)應(yīng)用:簡(jiǎn)化ANF自定義控件初始化過(guò)程
。