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


    配置文件的编写代码:

    <?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();
            }

    不是路不平,而是你不行,到底行不行,看你停不停,只要你不停,早晚你能行!
  • 相关阅读:
    HDU 3339 In Action 最短路+01背包
    hash与map的区别联系应用(转)
    POJ
    欧几里德与扩展欧几里德算法(转)
    POJ
    第三届蓝桥杯C++B组省赛
    第四届蓝桥杯C++B组省赛
    第五届蓝桥杯C++B组省赛
    第六届蓝桥杯C++B组省赛
    线段树为什么要开4倍空间
  • 原文地址:https://www.cnblogs.com/90bablogs/p/3132663.html
Copyright © 2020-2023  润新知