自定义配置部分中枚举的 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>
I want to use an enum in my Custom Configuration Section. So I implemented an enum DatabaseMode and the according property.
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您实际上可以在 XSD 中定义枚举。对于您的示例 DatabaseMode 属性,XSD 片段将如下所示:
希望有所帮助。
如果其他人想回答,一个相关的问题是,一旦创建了 XSD,它应该放在哪里,以便 Visual Studio 在 web.config 文件中识别它?
更新:我在此处找到了上述问题的答案。
You can actually define an enumeration in XSD's. For your example DatabaseMode property, the XSD fragment will look like this:
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.