2009-12-15 33 views
0

我正尝试使用两个(或更多)离散设置组创建一个程序,它们都符合相同的接口。特别是我想要做的东西像下面,使用设计器生成的设置:具有相同接口的C#多个设置文件

IMySettings settings = Properties.A; 
Console.WriteLine(settings.Greeting); 
settings = Properties.B; 
Console.WriteLine(settings.Greeting); 

这将是微不足道一起去的接口,因为任何类,提供的方法可以分配,但我怎么能实现(?)这在C#中有其严格的接口实现规则?

注:C#/。NET 2.0

回答

2

的Properties.Settings类在VS中生成不会让你这样做。考虑定义一个简单的DTO类并用XmlAttribute标记它,以便您可以轻松地反序列化它。

+0

为什么?它只需将接口添加到生成的设置类定义中。缺点是每次更改设置时都会覆盖它。无论如何,它仍然是完美的配色方案。 – Harry 2014-11-23 08:11:37

1

您可以在C#中使用接口了。

但是,如果您仍想使用设计器生成的设置,则必须编写外观类。

1

我不确定你问的是如何在C#中实现一个接口。

如果是,只要是这样的:

Public Interface IMySettings { 
    Public string Greeting {get;set;} 
} 

然后,只需有你的“A”和“B”实现此接口,并返回所需的问候语。因此,您的属性类将实现IMySettings,而不是直类:

Public class Properties { 
    public IMySettings A {get;set;} 
    public IMySettings B {get;set;} 
} 

因此,而不是使用“MySettings”,你的代码看起来像这样:

IMySettings settings = Properties.A; 
+0

如果我没有理解错,这是不行的因为设置类是自动生成的,并没有实现这种接口。 – 2009-12-15 19:09:17

1

我不确定您是否可以通过设计器生成的设置进行操作,但我不经常使用它们,因此我可能会出错。但是,还有另外一种方法可以做到这一点:创建自己的ConfigurationSection

下面是一个例子:

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> 

有可能是在错别字,但总体思路是存在的。希望有所帮助。

0

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

另一种选择,也可能是最接近的答案,你原来的问题,是通过显式的创建一个通用属性的类,然后填充,可能 -

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; 
     } 
    } 

} 
相关问题