(转载)实战分析web.confing中灵活

it2022-05-09  20

首先兄弟们要明白这段代码只是掩饰功能真正项目中会见便些MM:你要做什么?猪:~~我都交代了吧~~,其实就是利用大家都比较收悉的System.Configuration.Provider.ProviderBase,作一个演示   具体的作用就是,利用ProviderBase在web.confing中配置,得以在程序中灵活应用。MM:ProviderBase不就是那个什么提供程序的基类么,你还没有说具体的作用猪:我说说~~,利用Enum,选择在web.confing的配置的基类得以获得样式表的多种状态MM:我喜欢Style我看你那又不少.cs,先介绍下不许说谎否则修理你~~。                                                      关系图猪:,~~ConfingPage由于他继承了ConfigurationSection,使得他可以设置web.confing中可以配置的节点类,StyleNames-是节点的属性StyleClass-则是节点的内部节点如果~~大姐你不明白~~请看下边 <? xml version="1.0" ?> < configuration >    < configSections >      < section  name ="StyleEnum"  type ="SeemPigAlikeVolitation.ConfingPage, __code"  requirePermission ="false" />    </ configSections > <!-- StyleNames配置可以使用的样式 -->    < StyleEnum  StyleNames ="Read,StyleT" >      < StyleClass >        < add  name ="Read"  type ="SeemPigAlikeVolitation.StyleRead"   />        < add  name ="StyleT"  type ="SeemPigAlikeVolitation.StyleT"   />      </ StyleClass >    </ StyleEnum > MM   : ....~~阿不好意思~~刚才睡着了~~ ,你这个好像在那见过,对了叫什么配置节点~~老师讲过的,有点记不起来了 <configSections>是什么~~里面的又是什么咚咚  猪: (我讲的很无聊么~~),指定配置节和命名空间声明(MSDN),变相来说就是首先里面不是有section节点么 第一个属性就是name你看他不是等于StyleEnum,你在看下面不是有个节点也叫StyleEnum,对吧这个name就是设置节点的名称,type= 就是设置这个节点的类,至于__code(我还不太清楚,如果您知道请您给我留言不胜感激)不过我猜测是说得这个类放在App_Code中,RequirePermission =false是只得这个节点不受web的信任级别的影响MM:哦是这样啊 ,那下面的节点StyleEnum里面是什么,麻烦你快一点猫和老鼠药开始了~~猪:好好~~,StyleNames是属性-他的作用是确定有多少个StyleClass属性可以使用,而<StyleClass>是内迁节点作用是可以包含一组类型,里面的没个add都代表一个类型(口渴)MM:我去看猫和老鼠了~~你给我录音把猪:(心:你你~~去死吧),- -你去吧~~别忘了约会~~,以下是猪的录音部分://--这个是ConfingPage类的代码 using System;using System.Collections.Generic;using System.Text;using System.Configuration;namespace SeemPigAlikeVolitation{    public class ConfingPage : ConfigurationSection    {        public ConfingPage()        { }        [ConfigurationProperty("StyleNames", IsRequired=true)]        public string StyleNames        {            set {this["StyleNames"= value;}            get {return (string)this["StyleNames"];}                }        [ConfigurationProperty("StyleClass")]//--------ProviderSettingsValidation这个程序是用来验证节点的~~        [ConfigurationValidatorAttribute(typeof(ProviderSettingsValidation))]        public ProviderSettingsCollection StyleClass        {            get {                                                return (ProviderSettingsCollection)this["StyleClass"]; }        }    }} StyleNames-返回一个字符串代表可用的配置节点的集合 [ConfigurationProperty("StyleNames", IsRequired=true)]是属性在xml中显示的样式,后面那个是必须 请注意-这里有点要注意咯get,set和这个属性的名称最好一样这样可以避免产生两个错误: 1.避免产生堆栈溢出 2.避免数据错误的调用   StyleClass-返回的是继承System.Configuration.Provider.ProviderBase的类 [ConfigurationValidatorAttribute(typeof(ProviderSettingsValidation))]-是用来严正这个节点的正确性 这个属性比较特别他只有get因为它会读取配置节点中add的部分的type~~欧 using System;using System.Data;using System.Configuration;using System.Web;namespace SeemPigAlikeVolitation{    public class ProviderSettingsValidation : ConfigurationValidatorBase    {        public ProviderSettingsValidation()        {                   }        public override bool CanValidate(Type type)        {            return type == typeof(ProviderSettingsCollection);        }        public override void Validate(object value)        {            ProviderSettingsCollection providerCollection = value as ProviderSettingsCollection;            if (providerCollection != null)            {                foreach (ProviderSettings _provider in providerCollection)                {                    if (String.IsNullOrEmpty(_provider.Type))                    {                        throw new ConfigurationErrorsException("Type was not defined in the provider");                    }                    Type dataAccessType = Type.GetType(_provider.Type);                    if (dataAccessType == null)                    {                        throw (new InvalidOperationException("Provider's Type could not be found"));                    }                }            }        }    }} ConfigurationValidatorBase他就是用来验证节点类型 //-----得到对象集合的属性       public override bool CanValidate(Type type)         {             return type == typeof(ProviderSettingsCollection);         }   public override void Validate(object value)       //--反射的时候自动调用(估计调用方法也是反射) 漂亮的as ~~不会产生异常 ProviderSettingsCollection providerCollection = value as ProviderSettingsCollection; //--分析每一个add foreach (ProviderSettings _provider in providerCollection) { //---如果是字符串返回true String.IsNullOrEmpty(_provider.Type) //---验证类型是否存在,这里可以改成验证类型是否存在并且是否是指定的类型  Type dataAccessType = Type.GetType(_provider.Type);                     if (dataAccessType == null)   ~~晕录音机没电了~~艾偶得约会阿~~算了-- 第一篇就到这里了~~欢迎常沟通,技术交流,本人是猪宝贝拜拜 这个给家娱乐下-您猜猜我在干什么~~ using System;using System.Data;using System.Configuration;using System.Collections;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;namespace PageServer2007{    public  class Page: System.Web.UI.Page    {               protected override void OnInit(EventArgs e)        {                              this.EventBain(Page);            base.OnInit(e);                    }        public void EventBain(Control container)        {            foreach (Control ctl in container.Controls)            {                Button Now = ctl as Button;                if (Now != null && Now.ID.StartsWith("Redirect"))                {                    Now.Click += new EventHandler(Button_Click);                }                if (ctl is ButtonRedirect && ((ButtonRedirect)ctl).RedirectUrl!="")                {                  ( (ButtonRedirect)ctl).Click += new EventHandler(ButtonRedirect_Click);                }                else                {                    if (ctl.Controls.Count > 0)                        EventBain(ctl);                }            }        }        protected void Button_Click(object sender, EventArgs e)        {            Redirect(((Button)sender).ID.Split('_')[1+ ".aspx");        }        protected void ButtonRedirect_Click(object sender, EventArgs e)        {            Redirect(((ButtonRedirect)sender).RedirectUrl);        }        public void Redirect(string url)        {            try            {                              Response.Redirect(url);            }            catch (System.Exception)            {            }        }          }   }

如果您猜到这个是做什么的~~在最后的catch中虽然捕获的是System.Exception,但是还是有一种错误找捕获不了,~您不妨找找看~~当然(错误可以在web.confing中配,由于代码的需求有错误没有可能使用代码处理呢 ?我也在找答案)- -当然这只是推理游戏别当真

转载于:https://www.cnblogs.com/loveasm/archive/2009/11/26/1611641.html


最新回复(0)