如何向 WCF 客户端公开枚举属性

发布于 2024-08-17 14:19:53 字数 524 浏览 2 评论 0原文

我想向 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

黑凤梨 2024-08-24 14:19:53

您可以从服务公开枚举,但枚举上的属性在通过线路发送时不会被序列化。这意味着该枚举的使用者只能看到枚举本身,而看不到您的任何属性。

您需要做的是使用 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 the EnumMember 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.

猫瑾少女 2024-08-24 14:19:53

如果打算公开枚举成员的显示文本,则有一个解决方法,可以在合约中以这种方式定义枚举:

public enum EPaymentCycle
{
    [EnumMember(Value = "Month by Month")]
    Monthly,

    [EnumMember(Value = "Week by Week")]
    Weekly,

    [EnumMember(Value = "Hour by Hour")]
    Hours
}

SvcUtils 序列化会产生一个有趣的结果:

public enum EPaymentCycle : int
{

    [System.Runtime.Serialization.EnumMemberAttribute(Value="Month by Month")]
    MonthByMonth= 0,

    [System.Runtime.Serialization.EnumMemberAttribute(Value="Week by Week")]
    WeekbyWeek= 1,

    [System.Runtime.Serialization.EnumMemberAttribute(Value="Hour by Hour")]
    HourbyHour = 2
}

您可以通过反射读取 EnumMemberAttribute 值,然后就得到了它。 svcutil 序列化生成的 xsd 元数据文件也符合预期:

<xs:simpleType name="EPaymentCycle">
<xs:restriction base="xs:string">
  <xs:enumeration value="Month by Month" />
  <xs:enumeration value="Week by Week" />
  <xs:enumeration value="Hour by Hour" />
</xs:restriction>

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:

public enum EPaymentCycle
{
    [EnumMember(Value = "Month by Month")]
    Monthly,

    [EnumMember(Value = "Week by Week")]
    Weekly,

    [EnumMember(Value = "Hour by Hour")]
    Hours
}

The SvcUtils serialization produces an interesting result:

public enum EPaymentCycle : int
{

    [System.Runtime.Serialization.EnumMemberAttribute(Value="Month by Month")]
    MonthByMonth= 0,

    [System.Runtime.Serialization.EnumMemberAttribute(Value="Week by Week")]
    WeekbyWeek= 1,

    [System.Runtime.Serialization.EnumMemberAttribute(Value="Hour by Hour")]
    HourbyHour = 2
}

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:

<xs:simpleType name="EPaymentCycle">
<xs:restriction base="xs:string">
  <xs:enumeration value="Month by Month" />
  <xs:enumeration value="Week by Week" />
  <xs:enumeration value="Hour by Hour" />
</xs:restriction>

一抹淡然 2024-08-24 14:19:53

我并不完全熟悉规范,但我怀疑这种元数据在 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...

z祗昰~ 2024-08-24 14:19:53

交通灯值的枚举示例...

[DataContract]
public enum TrafficLightType
{
    [EnumMember]
    Red,

    [EnumMember]
    Green,

    [EnumMember]
    Amber
}

Example enum for the values of a traffic light...

[DataContract]
public enum TrafficLightType
{
    [EnumMember]
    Red,

    [EnumMember]
    Green,

    [EnumMember]
    Amber
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文