0

我正在尝试创建一个具有两个(或更多)离散设置集的程序,它们都符合相同的界面。特别是我想做类似以下的事情,使用设计器生成的设置:

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

这对于 Go 的接口来说是微不足道的,因为可以分配任何提供方法的类(?),但是我如何在 C# 中实现它,具有严格的接口实现规则?

注意:C#/.NET 2.0

4

5 回答 5

2

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

于 2009-12-15T19:08:34.967 回答
1

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

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

于 2009-12-15T18:55:14.477 回答
1

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

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

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

然后让你的“A”和“B”实现这个接口并返回你想要的问候语。因此,您的 Properties 类将实现 IMySettings 而不是直接类:

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

因此,您的代码将如下所示,而不是使用“MySettings”:

IMySettings settings = Properties.A;
于 2009-12-15T18:59:54.737 回答
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>

可能有错别字,但总体思路就在那里。希望有帮助。

于 2009-12-15T22:09:54.130 回答
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;
        }
    }

}
于 2012-09-02T08:09:18.617 回答