2013-04-18 3 views
0

configurationElementCollection의 요소에서 nameValueCollection을 사용하려면 어떻게해야합니까? 이 같은
뭔가 :configurationElementCollection의 요소 내에 nameValueCollection

<connectionsSection> 
    <vendors> 
     <add name="nhonho" userName="name" password="password"> 
     <add key="someKey" value="someValue" />   
     </add> 
    </vendors> 
    </connectionsSection> 

나는까지 구성 요소 '벤더'을 만드는 등 얻었다.

public class Connections : ConfigurationSection 
{ 
    private static string SectionName = "connectionsSection"; 

    private static Connections _section; 

    public static Connections Section 
    { 
     get 
     { 
      return _section ?? 
       (_section = (Connections)ConfigurationManager.GetSection(SectionName)); 
     } 
    } 

    [ConfigurationProperty("vendors")] 
    public VendorCollection Vendors 
    { 
     get 
     { 
      return base["vendors"] as VendorCollection; 
     } 
    } 
} 


public class VendorCollection : ConfigurationElementCollection 
{ 
    private IList<VendorElement> _vendors = new List<VendorElement>(); 

    public override ConfigurationElementCollectionType CollectionType 
    { 
     get 
     { 
      return ConfigurationElementCollectionType.AddRemoveClearMap; 
     } 
    } 

    public VendorElement Get(string proName) 
    { 
     return BaseGet(proName) as VendorElement; 
    } 

    public void Add(VendorElement element) 
    { 
     BaseAdd(element); 
    } 

    public void Clear() 
    { 
     BaseClear(); 
    } 
    public void Remove(VendorElement element) 
    { 
     BaseRemove(element.Name); 
    } 

    public void Remove(string name) 
    { 
     BaseRemove(name); 
    } 

    public void RemoveAt(int index) 
    { 
     BaseRemoveAt(index); 
    } 

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

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

public class VendorElement : ConfigurationElement 
{ 
    [ConfigurationProperty("name")] 
    public string Name { get { return base["name"] as String; } } 

    [ConfigurationProperty("userName")] 
    public string UserName { get { return base["userName"] as String; } } 

    [ConfigurationProperty("password")] 
    public string Password { get { return base["password"] as String; } } 

    [ConfigurationProperty("", IsDefaultCollection= true)] 
    public NameValueCollection Extensions 
    { 
     get { return base[""] as NameValueCollection; } 
    } 
} 

답변

0

나는 실수로 내 문제를 해결의 bitbucket 프로젝트에 대한 링크를 발견했다.

/// <summary> 
/// Hold name value collection - allow duplication of name and values 
/// sample : 
/// <something> 
///  <add name="name1" value="value1" /> 
///  <add name="name1" value="value1" /> 
/// </something> 
/// </summary> 
public class NameValueCollection : ConfigurationElementCollection 
{ 
    protected override ConfigurationElement CreateNewElement() 
    { 
     return new NameValueElement(); 
    } 

    protected override object GetElementKey(ConfigurationElement element) 
    { 
     //to allow duplication of value 
     return element.ElementInformation.LineNumber; 
    } 
} 
/// <summary> 
/// this is the value element 
/// </summary> 
public class NameValueElement : ConfigurationElement 
{ 
    [ConfigurationProperty("name")] 
    public string Name 
    { 
     get { return (String)this["name"]; } 
     set { this["name"] = value; } 
    } 

    [ConfigurationProperty("value")] 
    public string Value 
    { 
     get { return (String)this["value"]; } 
     set { this["value"] = value; } 
    } 
} 

SRC : https://bitbucket.org/kkurni/custom-configuration-element/src/13997e83b899/NameValueCollection.cs

관련 문제