2013-10-19 4 views
1

이 사이트의 두뇌 트러스트에이 문제를 제시하면 누군가 내 실수를 보게됩니다.중첩 된 ConfigurationElementCollections가있는 ConfigurationSection

이메일 텍스트를 다양한 내부 클래스의 속성에있는 정보와 함께 "편지 병합"해야하는 프로젝트를 진행 중입니다. 전자 메일 텍스트의 일반적인 기호는 "{member name}, {mobile phone} 등"처럼 보일 수 있습니다.

web.config의 ConfigurationSection을 사용하여 기호와 클래스를 정의하고 싶습니다.

<EmailSymbols> 
    <SymbolClasses> 

     <SymbolClass name="OHMember"> 
      <Symbol name="Member Name" template="{0} {1}"> 
       <add index="0" value="SMFirstName" /> 
       <add index="1" value="SMLastName" /> 
      </Symbol> 
      <Symbol name="Phone" template="{0}"> 
       <add index="0" value="SMPhone" /> 
      </Symbol> 
     </SymbolClass> 

     <SymbolClass name="Form"> 
      <Symbol name="Contact Name" dataname="ContactName" /> 
     </SymbolClass> 

    </SymbolClasses> 
</EmailSymbols> 

... 그리고 나는 그것을 구문 분석하려고 코드 : 여기 내 제안 된 구성 섹션입니다 문자 = ConfigurationManager가 :

public class EmailSymbols : ConfigurationSection { 

    [ConfigurationProperty("SymbolClasses", IsRequired = true)] 
    public SymbolClassCollection SymbolClasses { 
     get { 
      return this["SymbolClasses"] as SymbolClassCollection; 
     } 
    } 
} 

[ConfigurationCollection(typeof(SymbolClass), AddItemName = "SymbolClass")] 
public class SymbolClassCollection : ConfigurationElementCollection { 

    protected override ConfigurationElement CreateNewElement() { 
     return new SymbolClass(); 
    } 

    protected override object GetElementKey(ConfigurationElement element) { 
     return ((SymbolClass)element).Name; 
    } 
} 

[ConfigurationCollection(typeof(Symbol), AddItemName = "Symbol")] 
public class SymbolClass : ConfigurationElementCollection { 

    [ConfigurationProperty("name", IsRequired = true, IsKey = true)] 
    public String Name { 
     get { 
      return this["name"] as String; 
     } 
    } 

    protected override ConfigurationElement CreateNewElement() { 
     return new Symbol(); 
    } 

    protected override object GetElementKey(ConfigurationElement element) { 
     return ((Symbol)element).Name; 
    } 
} 

[ConfigurationCollection(typeof(TemplateValue), AddItemName = "add")] 
public class Symbol : ConfigurationElementCollection { 

    [ConfigurationProperty("name", IsRequired = true, IsKey = true)] 
    public String Name { 
     get { 
      return this["name"] as String; 
     } 
    } 

    [ConfigurationProperty("template", IsRequired = false)] 
    public String Template { 
     get { 
      return this["template"] as String; 
     } 
    } 

    [ConfigurationProperty("dataname", IsRequired = false)] 
    public String DataName { 
     get { 
      return this["dataname"] as String; 
     } 
    } 

    protected override ConfigurationElement CreateNewElement() { 
     return new TemplateValue(); 
    } 

    protected override object GetElementKey(ConfigurationElement element) { 
     return ((TemplateValue)element).Index; 
    } 
} 

public class TemplateValue : ConfigurationElement { 

    [ConfigurationProperty("index", IsRequired = false, IsKey = true)] 
    public Int32 Index { 
     get { 
      return this["index"] == null ? -1 : Convert.ToInt32(this["index"]); 
     } 
    } 

    [ConfigurationProperty("value", IsRequired = false)] 
    public String Value { 
     get { 
      return this["value"] as String; 
     } 
    } 
} 

나는이 문 섹션을 구문 분석

. EmailSymbols로 GetSection ("EmailSymbols");

"Unrecognized element 'Symbol'."라는 오류 메시지가 나타납니다.

이것은 단순히 .NET의 영역으로 내 주위에는 알지 못합니다. 누구나 줄 수있는 도움이 가장 감사 할 것입니다.

XML 정의가 올바른지, 올바른 형식인지 확인하십시오. SymbolClass 컬렉션이 필요합니다. 각 컬렉션에는 Symbol 컬렉션이 포함되어 있습니다. 각 컬렉션에는 TemplateValue 컬렉션이 들어 있습니다.

다시 귀하의 도움에 감사드립니다.

최고 감사합니다, 지미

+0

구성 파일의 맨 위에 섹션을 등록 했습니까? http://msdn.microsoft.com/en-us/library/2tw134k3.aspx –

+0

그랬습니다. 에 도달 할 때까지 파싱 한 다음 예외가 발생합니다 : "Unrecognized element 'Symbol'." "AddItemName"을 사용하여 "Symbol"을 선언했습니다. Symbol을 ConfigurationProperty로 추가하면 파서가 "Symbol"을 인식하지만 "Symbol"은 한 번만 사용할 수 있다고 알려줍니다. – JConnell

+0

중복입니다. 여기에 큰 대답이 있습니다. http://stackoverflow.com/questions/5661544/correct-implementation-of-a-custom-config-section-with-nested-collections – klev

답변

1

당신은 SymbolClass 클래스의 Init() 방법 오버라이드 (override)을 시도 할 수 있습니다 : 당신은 또한 [ConfigurationCollection(typeof(SymbolClass), AddItemName = "SymbolClass")]을 제거

protected override void Init() 
{ 
    base.Init(); 
    this.AddElementName = "Symbol"; 
} 

와 같은 클래스 선언 위에서 그것과 같은 다른 사람을 자신의 아무것도하지.