自定义配置部分中的Enum智能感知

时间:2011-04-12 18:12:16

标签: asp.net enums schema intellisense

我想在自定义配置部分中使用枚举。所以我实现了一个枚举DatabaseMode和相应的属性。

我还在System.Configuration.ConfigurationElement中实现了相应的属性。但是为了使IntelliSense能够在web.config中工作,我需要提供以xsd格式镜像相同结构的模式定义(xsd)。

我的问题是架构应该如何支持枚举?

具有不同选项的枚举:

public enum DatabaseMode
{
   Development,
   Deployment,
   Production
}

存储有关模式信息的属性:

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

在我的架构文件的重要部分下面:

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

1 个答案:

答案 0 :(得分:1)

您实际上可以在XSD中定义枚举。对于您的示例DatabaseMode属性,XSD片段将如下所示:

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

希望有所帮助。

一个有问题的问题,万一其他人想要回答的问题是,一旦创建了XSD,它应该放在哪里以便Visual Studio在web.config文件中识别它?

更新:我在SO上找到了上述问题here的答案。

相关问题