【问题标题】:IntelliSense for Enum in Custom Configuration Section自定义配置部分中的 IntelliSense for Enum
【发布时间】:2011-04-12 18:12:15
【问题描述】:

我想在我的自定义配置部分中使用枚举。所以我实现了一个枚举 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>

【问题讨论】:

    标签: asp.net enums schema intellisense


    【解决方案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 的答案。

    【讨论】:

      猜你喜欢
      • 2011-02-28
      • 2014-01-10
      • 2010-10-25
      • 2010-11-12
      • 2013-09-17
      • 2011-07-04
      • 2012-12-12
      • 2013-09-21
      相关资源
      最近更新 更多