.net 开发 配置文件的灵活使用

来源:互联网 发布:北京联合大学网络排名 编辑:程序博客网 时间:2024/05/17 08:52

配置文件的编写代码:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="spring" type="spring_test.springClass,spring_test"/>
    <section name="hellowChild_1" type="spring_test.IFactory.HelloClassChild_1,spring_test"/>
    <section name="hellowChild_2" type="spring_test.IFactory.HelloClassChild_2,spring_test"/>
  </configSections>
  <spring Name="spring_class:this is a test" ></spring>
  <hellowChild_1  Name="HelloClassChild_1 : this is a  HelloClassChild_1 class"  NameSpace="spring_test.IFactory"></hellowChild_1>
  <hellowChild_2  Name="HelloClassChild_2 : this is a  HelloClassChild_2 class"  NameSpace="spring_test.IFactory"></hellowChild_2>
</configuration>

类的编写代码:

public HelloClassChild_2()
        {
             
        }
        public HelloClassChild_2(string Name, string NameSpace)
        {
            this.Name = Name;
            this.NameSpace = NameSpace;
        }

        [System.Configuration.ConfigurationProperty("Name", IsRequired = true)]
        public string Name
        {
            get
            {
                return this["Name"].ToString();
            }
            set
            {
                this["Name"] = value;
            }
        }

        [System.Configuration.ConfigurationProperty("NameSpace", IsRequired = true)]
        public string NameSpace
        {
            get
            {
                return this["NameSpace"].ToString();
            }
            set
            {
                this["NameSpace"] = value;
            }
        }
        public string GetWriteLine(string Name,string NameSpace)
        {
            return string.Format("程序加载的类为:{0} \n所属的命名空间为:{1}",  Name,  NameSpace);
        }

        public void WriteLineString(string Name, string NameSpace)
        {
            Console.WriteLine(GetWriteLine(Name,NameSpace));
        }

 

类的实例化代码:

  static void Main(string[] args)
        {
            var dataClass1 = System.Configuration.ConfigurationManager.GetSection("spring");
            springClass spring = dataClass1 as spring_test.springClass;
            Console.WriteLine(spring.Name);

            var dataClass2 = System.Configuration.ConfigurationManager.GetSection("hellowChild_1");
            IFactory.IHello iFactory_1 = dataClass2 as IFactory.IHello;
            iFactory_1.WriteLineString(iFactory_1.Name, iFactory_1.NameSpace);


            var dataClass3 = System.Configuration.ConfigurationManager.GetSection("hellowChild_1");
            IFactory.IHello iFactory_2 = dataClass2 as IFactory.IHello;
            iFactory_2.WriteLineString(iFactory_2.Name, iFactory_2.NameSpace);
            
            Console.Read();
        }

 


<script type="text/javascript"><!--google_ad_client = "ca-pub-1944176156128447";/* cnblogs 首页横幅 */google_ad_slot = "5419468456";google_ad_width = 728;google_ad_height = 90;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击