2009-12-15 4 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로 표시하여 쉽게 deserialize 할 수 있도록하십시오.

+0

왜? 생성 된 설정 클래스 정의에 인터페이스를 추가하면됩니다. 단점은 설정을 변경할 때마다 덮어 쓰게된다는 것입니다.어쨌든 색 구성표에는 여전히 적합합니다. – Harry

1

C#에서도 인터페이스를 사용할 수 있습니다.

그러나 디자이너가 생성 한 설정을 계속 사용하려면 facade 클래스를 작성해야합니다.

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

만약 내가 제대로 이해하고,이 작동하지 않습니다 -

또 다른 옵션, 그리고 원래의 질문에 아마도 가장 가까운 답은 명시 적 캐스트를 통해 가능, 일반적인 속성 클래스를 만든 다음 채울 것입니다 설정 클래스가 자동 생성되어이 종류의 인터페이스를 구현하지 않기 때문입니다. –

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

} 
관련 문제