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

    不是路不平,而是你不行,到底行不行,看你停不停,只要你不停,早晚你能行!
  • 相关阅读:
    Qt 利用XML文档,写一个程序集合 二
    Qt 利用XML文档,写一个程序集合 一
    Qt QpushButton 实现长按下功能
    Qt 将字符串转成16进制显示
    Qt-QML-Canvas-雷达扫描仪表简单
    Qt-QML-电子罗盘
    Qt-QML-Canvas写个小小的闹钟
    Qt-Qt5最新增加程序图标方式
    Qt-QMl-自定义自己想要的TabView
    设置position:fixed后元素脱离标准流的解决方法
  • 原文地址:https://www.cnblogs.com/90bablogs/p/3132663.html
Copyright © 2020-2023  润新知