配置文件+反射确实去除了选择语句的繁琐,带来了优美的赶脚!

 

首先改进了一下类(接上文):

 

 

namespace classlib
{
    /// 
    /// interface igreetingstrategy
    /// 
    /// editor:v-liuhch createtime:2015/6/28 11:01:58
    public interface igreetingstrategy
    {
        string greetingtype { get; }
        void setgreetingwords(itextcontrol textcontrl);
    }

    /// 
    /// class englishgreeting
    /// 
    /// editor:v-liuhch createtime:2015/6/28 11:02:38
    public class englishgreeting : igreetingstrategy
    {
        public string greetingtype
        {
            get { return english; }
        }

        public void setgreetingwords(itextcontrol textcontrl)
        {
            textcontrl.text = hello,readers;
        }
    }

    /// 
    /// class chinesegreeting
    /// 
    /// editor:v-liuhch createtime:2015/6/28 11:02:56
    public class chinesegreeting : igreetingstrategy
    {
        private string greetingtype;
        public chinesegreeting(string greetingtype)
        {

            this.greetingtype = greetingtype;
        }
        public chinesegreeting() : this(中文) { }
        public chinesegreeting(xmlnode section) {
            xmlattribute attr = section.selectsinglenode(params).attributes[greetingtype];//获取属性值
            greetingtype = attr.value;//为字段赋值
        }
        public string greetingtype
        {
            get { return greetingtype; }
        }

        public void setgreetingwords(itextcontrol textcontrl)
        {
            textcontrl.text = 你好啊,小读者!;
        }
    }

    /// 
    /// class generalclass:这个类可能还有很多的字段,属性,方法,这里只是简写下
    /// ps:generalclass是一个普通的类型,这个类内部维护着igreetingstrategy,调用的时候还是根据多态具体调用。
    /// 
    /// editor:v-liuhch createtime:2015/6/28 11:08:04
    public class generalclass
    {
        private igreetingstrategy gs;
        public generalclass(igreetingstrategy gs)
        {
            this.gs = gs;
        }
        public string generalproperty
        {
            get
            {
                //做一些额外的工作,这里省略
                return  + gs.greetingtype + ;
            }
        }
        public void generalmethod(itextcontrol textcontrl)
        {
            //做一些额外的工作,这里省略
            gs.setgreetingwords(textcontrl);
            textcontrl.text =  + textcontrl.text + ;
            //省略。。。。。。。
        }


    }

}

 

 

 

然后在配置文件中定义好我们要使用的具体类和自定义标签的处理程序:

 

 

 

  
  
  
  
    
   
  
 

 

 

这里,chinesegreeting是我们要使用的类,上面定义的是处理greetingstrategy的类;

 

接着,写这个类的具体实现:

 

 

namespace classlib
{
    public class greetingconfigurationhandler : iconfigurationsectionhandler
    {
        /*
         处理有参数的构造函数的对象的创建:
         */
        /// 
        /// 创建配置节处理程序。
        /// 
        ///父对象。
        ///配置上下文对象。
        ///节 xml 节点。
        /// 创建的节处理程序对象。
        /// 
        /// editor:v-liuhch createtime:2015/6/30 20:34:54
        public object create(object parent, object configcontext, system.xml.xmlnode section)
        {
            //获取节点type属性的值
            type t = type.gettype(section.attributes[type].value);
            object obj=null;

            try
            {
                /* 2,在要实例的类中加入一个构造函数,接收一个xmlnode节点,将greeting_stragetgy的节点在此传递,然后在这个构造函数中进行处理;*/
                //如果t包含有参数为xmlnode的构造函数,直接使用这个构造函数
                type[] paras = { typeof(xmlnode) };
                constructorinfo constructors = t.getconstructor(paras);
                if (constructors != null)
                {
                    object[] paramters = { section };
                    return activator.createinstance(t, paramters); //传入读取到的构造函数的参数
                }

                if (section.selectsinglenode(params) == null)  //无参数构造函数
                {
                    obj = activator.createinstance(t);
                }
                else  //有参数构造函数
                {
                    /*1,在此类中对策略类进行处理,取得params节点的属性值,然后传递给具体实例化的类;*/

                    //获取params节点的属性greetingtype的值
                    xmlattribute attr = section.selectsinglenode(params).attributes[greetingtype];
                    object[] parameters = { attr.value };
                    obj = activator.createinstance(t, parameters); //传入读取到的构造函数的参数
                } 
            }
            catch (exception)
            {

                return null;
            }
            
            return obj ;
        }
    }
}

 

在创建方法中,我们先判断chinesegreeting类有没有一个参数为节点的构造方法,如果有的话,就直接将section当作参数,在利用反射创建类型实例的时候传进去;

 

如果没有这样的构造方法,我们就在这个处理类里面读取xml文件中的参数,然后在类型实例化的时候传进去;

 

两种方式比较,其实都是一样的,只过是这个参数读取的早晚的问题;个人对比了下,觉得在这个类里面读取配置文件中的构造函数参数的方式更加灵活,个人偏爱。

 

写个东西测试下:

 

 

 #region 自定义节点存储类型信息——反射方法
            igreetingstrategy greetingstrategy = (igreetingstrategy)configurationmanager.getsection(greetingstrategy);
            if (greetingstrategy != null)
            {
                generalclass generalclass = new generalclass(greetingstrategy);
                ltrgreetingtype.text = generalclass.generalproperty;
                generalclass.generalmethod(ltrgreetingword);
            }
            #endregion

 

 

 

嘿嘿,相对方便。

 

 

 

 

感觉反射强大在把变化抽出来,但是抽出来的这个变化放到哪里去最容易改动或者是后期维护成本较低,于是配置文件这时候就该上了。。。。。。