开发者

IntelliSense for Enum in Custom Configuration Section

开发者 https://www.devze.com 2023-02-24 08:12 出处:网络
I want to use an enum in my Custom Configuration Section. So I implemented an enum DatabaseMode and the according prope开发者_如何学JAVArty.

I want to use an enum in my Custom Configuration Section. So I implemented an enum DatabaseMode and the according prope开发者_如何学JAVArty.

I also implemented the according Property in my System.Configuration.ConfigurationElement. But for the IntelliSense to work in the web.config I need to provide the schema definition (xsd) mirroring the same structure in xsd format.

My question is how the schema is supposed to look to support the enum?

The enum with the different options:

public enum DatabaseMode
{
   Development,
   Deployment,
   Production
}

The property storing the information about the Mode:

[ConfigurationProperty(databaseAlias)]
public DatabaseElement Database
{
   get { return (DatabaseElement)this[databaseAlias]; }
   set { this[databaseAlias] = value; }
}

Below the important part of my schema file:

<xs:element name="database">
  <xs:complexType>
    <xs:attribute name="server" type="xs:anyURI" use="required" />
    <xs:attribute name="name" type="xs:string" use="required" />
    <xs:attribute name="user" type="xs:string" use="required" />
    <xs:attribute name="password" type="xs:string" use="required" />
  </xs:complexType>
</xs:element>


You can actually define an enumeration in XSD's. For your example DatabaseMode property, the XSD fragment will look like this:

<xs:attribute name="databaseMode"> <!-- I'm assuming you're using camelCasing -->
    <xs:simpleType>
        <xs:restriction base="xs:string">
            <xs:enumeration value="development" />
            <xs:enumeration value="deployment" />
            <xs:enumeration value="production" />
        </xs:restriction>
    </xs:simpleType>
</xs:attribute>

Hope that helps.

A rleated question, in case anyone else wants to answer is, once the XSD is created, where should it be placed so that Visual Studio recognizes it within the web.config file?

UPDATE: I found the answer to my above question here on SO.

0

精彩评论

暂无评论...
验证码 换一张
取 消