将 enum 序列化为 int

发布于 2024-08-17 10:37:31 字数 907 浏览 4 评论 0原文

我希望通过网络服务返回以下类,其中包括一个枚举类型作为其成员之一。

[Serializable, XmlRoot("GeoCoordinate")]
public class GeoCoordinate
{
    public enum AccuracyLevel
    {
        Unknown = 0,
        Country = 1,
        Region = 2,
        SubRegion = 3,
        Town = 4,
        PostalCode = 5,
        Street = 6,
        Intersection = 7,
        Address = 8,
        Premise = 9
    }

    private AccuracyLevel _accuracy;

    // ... more members


    public AccuracyLevel Accuracy
    {
        get { return _accuracy; } 
        set { _accuracy = value;}
    }
}

这可以正常工作,但会返回以下形式的结果:

<!-- ... -->
<Accuracy>Unknown or Country or Region or SubRegion or Town or 
  PostalCode or Street or Intersection or Address or Premise</Accuracy>
<!-- ... -->

我希望它简单地返回一个整数,而不是表示枚举的字符串。可以在不更改 GeoCooperative.Accuracy 类型的情况下完成此操作吗?

I am looking to return the following class through a web service, which includes an enum type as one of its members.

[Serializable, XmlRoot("GeoCoordinate")]
public class GeoCoordinate
{
    public enum AccuracyLevel
    {
        Unknown = 0,
        Country = 1,
        Region = 2,
        SubRegion = 3,
        Town = 4,
        PostalCode = 5,
        Street = 6,
        Intersection = 7,
        Address = 8,
        Premise = 9
    }

    private AccuracyLevel _accuracy;

    // ... more members


    public AccuracyLevel Accuracy
    {
        get { return _accuracy; } 
        set { _accuracy = value;}
    }
}

This works correctly, but will return a result in the form of:

<!-- ... -->
<Accuracy>Unknown or Country or Region or SubRegion or Town or 
  PostalCode or Street or Intersection or Address or Premise</Accuracy>
<!-- ... -->

Instead of a string that represents the enum, I would like it to simply return an integer. Can this be done without changing the type of GeoCoordinate.Accuracy?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

走野 2024-08-24 10:37:31

虽然这是一个黑客,但我认为使用 在这种情况下,每个枚举成员上的 XmlEnumAttribute 是最容易接受的。如果此枚举更大,则最好在 Accuracy 属性上使用 XmlIgnore ,并向该类添加一个额外的 int 属性,如该问题的另一个答案中所述。

Usng XmlEnumAttribute 意味着只需要修改 enum 本身,并且无论在哪里使用它,xml 都会像 int 一样序列化。

    public enum AccuracyLevel
    {
        [XmlEnum("0")] Unknown = 0,
        [XmlEnum("1")] Country = 1,
        [XmlEnum("2")] Region = 2,
        [XmlEnum("3")] SubRegion = 3,
        [XmlEnum("4")] Town = 4,
        [XmlEnum("5")] PostalCode = 5,
        [XmlEnum("6")] Street = 6,
        [XmlEnum("7")] Intersection = 7,
        [XmlEnum("8")] Address = 8,
        [XmlEnum("9")] Premise = 9
    }

Although it is a hack, I deemed using XmlEnumAttribute on each of the enum members to be most pallatable in this case. If this enum were much larger, it would probably be better to use XmlIgnore on the Accuracy property, and add an additional int property to the class as described in another answer to this question.

Usng XmlEnumAttribute means that only the enum itself needs to be modified, and will xml serialize like an int wherever it is used.

    public enum AccuracyLevel
    {
        [XmlEnum("0")] Unknown = 0,
        [XmlEnum("1")] Country = 1,
        [XmlEnum("2")] Region = 2,
        [XmlEnum("3")] SubRegion = 3,
        [XmlEnum("4")] Town = 4,
        [XmlEnum("5")] PostalCode = 5,
        [XmlEnum("6")] Street = 6,
        [XmlEnum("7")] Intersection = 7,
        [XmlEnum("8")] Address = 8,
        [XmlEnum("9")] Premise = 9
    }
兮颜 2024-08-24 10:37:31

我相信您需要在枚举上使用 [XmlIgnore] ,并创建第二个返回整数值的属性:

[XmlRoot("GeoCoordinate")]
public class GeoCoordinate
{
    public enum AccuracyLevel
    {
        Unknown = 0,
        Country = 1,
        Region = 2,
        SubRegion = 3,
        Town = 4,
        PostalCode = 5,
        Street = 6,
        Intersection = 7,
        Address = 8,
        Premise = 9
    }

    private AccuracyLevel _accuracy;

    // ... more members


    [XmlIgnore]
    public AccuracyLevel Accuracy
    {
        get { return _accuracy; } 
        set { _accuracy = value;}
    }

    [XmlElement("AccuracyLevel")]
    public int AccuracyLevelInt
    {
        get {return (int) AccuracyLevel;}
        set {AccuracyLevel = (AccuracyLevel) value;}
    }
}

请注意, XML 序列化器。

另请注意,AccuracyLevelInt 属性可能未正确实现。我现在正在调查此事。

I believe you'll need to use [XmlIgnore] on the enum, and create a second property which returns the integer value:

[XmlRoot("GeoCoordinate")]
public class GeoCoordinate
{
    public enum AccuracyLevel
    {
        Unknown = 0,
        Country = 1,
        Region = 2,
        SubRegion = 3,
        Town = 4,
        PostalCode = 5,
        Street = 6,
        Intersection = 7,
        Address = 8,
        Premise = 9
    }

    private AccuracyLevel _accuracy;

    // ... more members


    [XmlIgnore]
    public AccuracyLevel Accuracy
    {
        get { return _accuracy; } 
        set { _accuracy = value;}
    }

    [XmlElement("AccuracyLevel")]
    public int AccuracyLevelInt
    {
        get {return (int) AccuracyLevel;}
        set {AccuracyLevel = (AccuracyLevel) value;}
    }
}

Note that [Serializable] is not used by the XML Serializer.

Also, note that the AccuracyLevelInt property is probably implemented incorrectly. I'm looking into that now.

我不在是我 2024-08-24 10:37:31

使用 [Serialized][DataContract] 修饰枚举。这两个属性之间存在一些差异,请务必检查一下(这篇博文 可能会对此有所帮助)。并使用 [EnumMember] 标记各个枚举项。我从未检查过枚举在传输过程中是什么样子,但这样做将确保它到达另一端,并且如果您生成客户端代理,还将确保它被拾取。

Decorate the enum with [Serializable] or [DataContract]. There are some differences between the two attributes, make sure you check it out (this blog post may help with that). And mark the individual enum items with [EnumMember]. I have never checked what the enum looks like in transit, but doing this will ensure that it arrives at the other end, and will also ensure that it gets picked up if you generate a client side proxy.

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