如何向 WCF 客户端公开枚举属性
我想向 WCF 客户端应用程序公开枚举属性,但我只能看到枚举值。
这是枚举:
public enum TemplateType
{
[EnumDescription("Property Particulars")]
[EnumValue("PropertyParticulars")]
PropertyParticulars = 1,
[EnumDescription("Short Format Lists")]
[EnumValue("ShortFormatLists")]
ShortFormatLists,
[EnumDescription("Client Letters")]
[EnumValue("ClientLetters")]
ClientLetters,
[EnumDescription("Labels")]
[EnumValue("Labels")]
Labels
}
如何公开描述和值属性?
I want to expose enum attributes to WCF client application, but I can only see enum values.
Here is the enum:
public enum TemplateType
{
[EnumDescription("Property Particulars")]
[EnumValue("PropertyParticulars")]
PropertyParticulars = 1,
[EnumDescription("Short Format Lists")]
[EnumValue("ShortFormatLists")]
ShortFormatLists,
[EnumDescription("Client Letters")]
[EnumValue("ClientLetters")]
ClientLetters,
[EnumDescription("Labels")]
[EnumValue("Labels")]
Labels
}
How can I expose the Description and Value attributes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以从服务公开枚举,但枚举上的属性在通过线路发送时不会被序列化。这意味着该枚举的使用者只能看到枚举本身,而看不到您的任何属性。
您需要做的是使用
DataContract
属性来装饰您的枚举,并使用EnumMember
属性,以便序列化您的信息,但这仅允许您指定基础值 每个枚举值,而不是描述。You can expose enums from a service but the attributes on an enum are not serialized when they are sent over the wire. This means that consumers of this enum will only see the enum itself and none of your attributes.
What you need to do is dress up your enum with a
DataContract
attribute and the values with theEnumMember
attribute so that your information will be serialized, but this will only allow you to specify the underlying value of each enum value, not a description.如果打算公开枚举成员的显示文本,则有一个解决方法,可以在合约中以这种方式定义枚举:
SvcUtils 序列化会产生一个有趣的结果:
您可以通过反射读取 EnumMemberAttribute 值,然后就得到了它。 svcutil 序列化生成的 xsd 元数据文件也符合预期:
There is a workaround if the intention is to expose a display text for enum members, define your enum in this way in the contracts:
The SvcUtils serialization produces an interesting result:
You can read the EnumMemberAttribute Value by reflection and there you got it. Also the xsd metadata file produced by svcutil serialization is as expected:
我并不完全熟悉规范,但我怀疑这种元数据在 WSDL 中是否有等效的表示形式。因此,如果您在代理中生成类型,这在客户端将不可见。
但是,如果将所有 DataContract 放在客户端中引用的单独程序集中,则可以在客户端重用这些类型。在这种情况下,属性将是可见的。需要为您的服务引用检查“引用程序集中的重用类型”,但默认情况下此选项处于启用状态。
这里有一篇关于它的简短博客文章。我确信还有其他人...
I'm not fully versed in the specs, but I doubt this kind of metadata has an equivalent representation in WSDL. Thus, this will not be visible on the client side if you generate the types in your proxy.
However, if you put all your DataContracts in a separate assembly that you reference in the client, you can reuse those types on the client side. In that case, the attributes would be visible. "Reuse types in referenced assemblies" needs to be checked for your Service Reference, but this is on by default.
Here's a short blog post about it. I'm sure there are others...
交通灯值的枚举示例...
Example enum for the values of a traffic light...