C#具有相同界面的多个设置文件

C#具有相同界面的多个设置文件,c#,.net-2.0,settings,interface,application-settings,C#,.net 2.0,Settings,Interface,Application Settings,我试图创建一个具有两个(或更多)离散设置集的程序,这两个设置集都符合相同的接口。特别是,我想使用设计器生成的设置执行以下操作: IMySettings settings = Properties.A; Console.WriteLine(settings.Greeting); settings = Properties.B; Console.WriteLine(settings.Greeting); 这对于Go的接口来说是微不足道的,因为任何提供方法的类(?)都可以被分配,但是我如何在C#中用

我试图创建一个具有两个(或更多)离散设置集的程序,这两个设置集都符合相同的接口。特别是,我想使用设计器生成的设置执行以下操作:

IMySettings settings = Properties.A;
Console.WriteLine(settings.Greeting);
settings = Properties.B;
Console.WriteLine(settings.Greeting);
这对于Go的接口来说是微不足道的,因为任何提供方法的类(?)都可以被分配,但是我如何在C#中用它严格的接口实现规则实现它呢


注意:C#/.NET2.0也可以使用C#中的接口


但是,如果仍要使用设计器生成的设置,则必须编写facade类。

也可以使用C#中的接口


但是,如果仍要使用设计器生成的设置,则必须编写facade类。

我不确定您要问的是如何在C#中实现接口

如果是,只需制作如下内容:

Public Interface IMySettings {
   Public string Greeting {get;set;}
}
然后让你的“A”和“B”实现这个接口并返回你想要的问候语。因此,Properties类将实现IMySettings,而不是straight类:

Public class Properties {
   public IMySettings A {get;set;}
   public IMySettings B {get;set;}
}
因此,与使用“MySettings”不同,您的代码如下所示:

IMySettings settings = Properties.A;

我不确定您要问的是否是如何在C#中实现接口

如果是,只需制作如下内容:

Public Interface IMySettings {
   Public string Greeting {get;set;}
}
然后让你的“A”和“B”实现这个接口并返回你想要的问候语。因此,Properties类将实现IMySettings,而不是straight类:

Public class Properties {
   public IMySettings A {get;set;}
   public IMySettings B {get;set;}
}
因此,与使用“MySettings”不同,您的代码如下所示:

IMySettings settings = Properties.A;

VS中生成的Properties.Settings类不允许您执行此操作。考虑定义一个简单的DTO类并用XMLATRATUTE标记它,以便您可以容易地反序列化它。

属性。VS中生成的设置类不会让您这样做。考虑定义一个简单的DTO类并用XMLATRATION标记它,这样你就可以轻易地反序列化它。

< P>我不确定你是否可以通过设计器生成的设置来完成你的操作,但是我不经常使用它,所以我可能错了。然而,还有另一种方法可以做到这一点:创建自己的

以下是一个例子:

public class MyProperties : ConfigurationSection {
    [ConfigurationProperty("A")]
    public MySettings A
    {
        get { return (MySettings )this["A"]; }
        set { this["A"] = value; }
    }

    [ConfigurationProperty("B")]
    public MySettings B
    {
        get { return (MySettings )this["B"]; }
        set { this["B"] = value; }
    }
}

public class MySettings : ConfigurationElement {
    [ConfigurationProperty("greeting")]
    public string Greeting
    {
        get { return (string )this["greeting"]; }
        set { this["greeting"] = value; }
    }
}
然后,您的app.config/web.config需要以下内容:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="mySettings" type="Namespace.MyProperties, Assembly"/>
    </configSections>
    <mySettings>
        <A greeting="Hello from A!" />
        <B greeting="Hello from B" />
    </mySettings>
</configuration>


这里面可能有拼写错误,但总的想法是存在的。希望这能有所帮助。

我不确定您是否可以通过设计器生成的设置来完成所需的操作,但我不经常使用这些设置,因此可能会出错。然而,还有另一种方法可以做到这一点:创建自己的

以下是一个例子:

public class MyProperties : ConfigurationSection {
    [ConfigurationProperty("A")]
    public MySettings A
    {
        get { return (MySettings )this["A"]; }
        set { this["A"] = value; }
    }

    [ConfigurationProperty("B")]
    public MySettings B
    {
        get { return (MySettings )this["B"]; }
        set { this["B"] = value; }
    }
}

public class MySettings : ConfigurationElement {
    [ConfigurationProperty("greeting")]
    public string Greeting
    {
        get { return (string )this["greeting"]; }
        set { this["greeting"] = value; }
    }
}
然后,您的app.config/web.config需要以下内容:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="mySettings" type="Namespace.MyProperties, Assembly"/>
    </configSections>
    <mySettings>
        <A greeting="Hello from A!" />
        <B greeting="Hello from B" />
    </mySettings>
</configuration>


这里面可能有拼写错误,但总的想法是存在的。希望对您有所帮助。

您可以使用自定义代码生成器将接口插入生成的代码中(使用此处的方法:)。这很简单,也很整洁,但问题是,如果你在一个团队中工作,那么他们都需要更新他们的注册表来构建解决方案

另一个选项,可能是最接近原始问题的答案,是创建一个泛型属性类,然后进行填充(可能通过显式转换)-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace ConsoleApplication6
{

    // use this class if you plan to add lots more settings files in future (low maintenance)
    class GenericProps1 
    {
        public string TestString { get; private set; }

        // single cast for all settings files
        public static explicit operator GenericProps1(ApplicationSettingsBase props)
        {
            return new GenericProps1() { TestString = props.Properties["TestString"].DefaultValue.ToString() };
        }
    }

    // use this class if you do NOT plan to add lots more settings files in future (nicer code)
    class GenericProps2 
    {
        public string TestString { get; private set; }

        // cast needed for settings1 file
        public static explicit operator GenericProps2(Properties.Settings1 props)
        {
            return new GenericProps2() { TestString = props.TestString };
        }

        // cast needed for settings 2 file
        public static explicit operator GenericProps2(Properties.Settings2 props)
        {
            return new GenericProps2() { TestString = props.TestString };
        }

        // cast for settings 3,4,5 files go here...
    }


    class Program
    {
        // usage
        static void Main(string[] args)
        {
            GenericProps1 gProps1_1 = (GenericProps1)Properties.Settings1.Default;
            GenericProps1 gProps1_2 = (GenericProps1)Properties.Settings2.Default;
            //or
            GenericProps2 gProps2_1 = (GenericProps2)Properties.Settings1.Default;
            GenericProps2 gProps2_2 = (GenericProps2)Properties.Settings2.Default;
        }
    }

}

您可以使用自定义代码生成器将接口插入到生成的代码中(使用以下方法:)。这很简单,也很整洁,但问题是,如果你在一个团队中工作,那么他们都需要更新他们的注册表来构建解决方案

另一个选项,可能是最接近原始问题的答案,是创建一个泛型属性类,然后进行填充(可能通过显式转换)-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace ConsoleApplication6
{

    // use this class if you plan to add lots more settings files in future (low maintenance)
    class GenericProps1 
    {
        public string TestString { get; private set; }

        // single cast for all settings files
        public static explicit operator GenericProps1(ApplicationSettingsBase props)
        {
            return new GenericProps1() { TestString = props.Properties["TestString"].DefaultValue.ToString() };
        }
    }

    // use this class if you do NOT plan to add lots more settings files in future (nicer code)
    class GenericProps2 
    {
        public string TestString { get; private set; }

        // cast needed for settings1 file
        public static explicit operator GenericProps2(Properties.Settings1 props)
        {
            return new GenericProps2() { TestString = props.TestString };
        }

        // cast needed for settings 2 file
        public static explicit operator GenericProps2(Properties.Settings2 props)
        {
            return new GenericProps2() { TestString = props.TestString };
        }

        // cast for settings 3,4,5 files go here...
    }


    class Program
    {
        // usage
        static void Main(string[] args)
        {
            GenericProps1 gProps1_1 = (GenericProps1)Properties.Settings1.Default;
            GenericProps1 gProps1_2 = (GenericProps1)Properties.Settings2.Default;
            //or
            GenericProps2 gProps2_1 = (GenericProps2)Properties.Settings1.Default;
            GenericProps2 gProps2_2 = (GenericProps2)Properties.Settings2.Default;
        }
    }

}

如果我理解正确,这将不起作用,因为设置类是自动生成的,没有实现这种类型的接口。如果我理解正确,这将不起作用,因为设置类是自动生成的,没有实现这种类型的接口。为什么?只需将接口添加到生成的设置类定义中。缺点是每次更改设置时它都会被覆盖。不管怎样,它仍然适合配色方案。为什么?只需将接口添加到生成的设置类定义中。缺点是每次更改设置时它都会被覆盖。无论如何,它仍然是完美的配色方案。