.NET 구성 API를 사용하여 사용자 지정 구성 섹션을 작성하고 있습니다. 두 개의 하위 요소 중 하나를 정확하게 포함 할 수있는 섹션을 정의하고 싶습니다.어떻게 상호 배타적 인 사용자 정의 구성 요소를 설정합니까?
예 : 현재
<MyCustomSection>
<myItems>
<myItemType name="1">
<firstSubTypeConfig />
</myItemType>
<myItemType name="2">
<secondSubTypeConfig />
</myItemType>
</myItems>
</MyCustomSection>
, 나는이 부분은 다음과 같이 정의했다 :
public class MyCustomSection : ConfigurationSection
{
public override bool IsReadOnly()
{
return false;
}
[ConfigurationProperty("myItems")]
public MyItemsCollection MyItems
{
get { return (MyItemsCollection)base["myItems"]; }
set { base["myItems"] = value; }
}
}
[ConfigurationCollection(typeof(MyItemType), AddItemName="myItemType",
CollectionType=ConfigurationElementCollectionType.BasicMap)]
public class MyItemsCollection : ConfigurationElementCollection
{
public override bool IsReadOnly()
{
return false;
}
public override ConfigurationElementCollectionType CollectionType
{
get { return ConfigurationElementCollectionType.BasicMap; }
}
protected override string ElementName
{
get { return "myItemType"; }
}
protected override ConfigurationElement CreateNewElement()
{
return new MyItemType();
}
protected override object GetElementKey(ConfigurationElement element)
{
return (element as MyItemType).Name;
}
}
public class MyItemType : ConfigurationElement
{
public override bool IsReadOnly()
{
return false;
}
[ConfigurationProperty("name", IsRequired=true)]
public string Name
{
get { return (string)base["name"]; }
set { base["name"] = value; }
}
[ConfigurationProperty("firstSubTypeConfig")]
public FirstSubTypeConfig FirstSubTypeConfig
{
get { return (FirstSubTypeConfig)base["firstSubTypeConfig"]; }
set { base["firstSubTypeConfig"] = value; }
}
[ConfigurationProperty("secondSubTypeConfig")]
public SecondSubTypeConfig SecondSubTypeConfig
{
get { return (SecondSubTypeConfig)base["secondSubTypeConfig"]; }
set { base["secondSubTypeConfig"] = value; }
}
}
public class FirstSubTypeConfig : ConfigurationElement
{
public override bool IsReadOnly()
{
return false;
}
}
public class SecondSubTypeConfig : ConfigurationElement
{
public override bool IsReadOnly()
{
return false;
}
}
구성은 난 단지 지정하는 경우에도 secondSubTypeConfig 요소를 추가 할 구성 섹션을 저장, 현재도 수정 프로그램 저장되며, firstSubTypeConfig 요소는 입니다.
내 첫 번째 생각은 FirstSubTypeConfig
과 SecondSubTypeConfig
에 대한 공통 기본 클래스를 소개하는 것이지만 구성 API가이를 처리 할 수 있는지 또는 어떻게 처리할지 명확하지 않습니다.
어떻게 상호 배타적 인 사용자 정의 구성 요소를 설정합니까?
예는 너무 추상적입니다. 당신이 실제로하고 싶은 것에 더 가깝도록 업데이트하면 당신을 도울 수 있습니다. 이 시점에서 그것은 너무 모호하여 모든 접근법을 다루기 위해 페이지를 작성해야하고 트랙을 벗어나 많은 시간을 낭비 할 위험이 크다. 이름, 유형 (특히 두 개의 "부속 유형")을 갱신하고 유형을 어떻게 사용하는지 설명하십시오. 다른 "SubTypeconfig"클래스가 있어야합니까? 그들의 둘 이상이 될 것입니까? 얼마나 많은 "myItemType"이있을 것입니까? 동일한 "SubTypeconfig"를 공유 할 수 있습니까? –