是否有必要在公共方法上声明属性 [DataMember(Order=n)] ?

发布于 2024-09-04 17:24:10 字数 841 浏览 4 评论 0原文

在我的解决方案中,我创建了公共类来存储值并已声明 [DataContract/DataMember] 属性。

例如,

[DataContract]
public class MeterSizeInfo
{
    string _meterSizeId;

    [DataMember(Order = 1)]
    public string MeterSizeId
    {
        get { return this._meterSizeId; }
        set { this._meterSizeId = value; }
    }

    string _meterSizeName;

    [DataMember(Order = 2)]
    public string MeterSizeName
    {
        get { return this._meterSizeName; }
        set { this._meterSizeName = value; }
    }

}

然后我需要添加另一个向整个项目公开的公共方法。 我想知道是否必须添加 [DataMember(Order = 3)] 。

    [DataMember(Order = 3)] //<--- must declare or not?
    public string DoSomething()
    {
        // do something...
    }

据我所知,如果我想在 protobuf-net 中使用序列化器来序列化存储在其中的数据,我必须声明这些属性。但我不确定方法。

请帮忙。 先感谢您。

In my solution, I have created public class to store value and already declare [DataContract/DataMember] attribute.

For example,

[DataContract]
public class MeterSizeInfo
{
    string _meterSizeId;

    [DataMember(Order = 1)]
    public string MeterSizeId
    {
        get { return this._meterSizeId; }
        set { this._meterSizeId = value; }
    }

    string _meterSizeName;

    [DataMember(Order = 2)]
    public string MeterSizeName
    {
        get { return this._meterSizeName; }
        set { this._meterSizeName = value; }
    }

}

Then I need to add another public method exposing to entire project.
I wonder I have to add [DataMember(Order = 3)] for it or not.

    [DataMember(Order = 3)] //<--- must declare or not?
    public string DoSomething()
    {
        // do something...
    }

I understand that if I want to use serializer in protobuf-net to serialize my data stored in, I have to declare those attribute. but I'm not sure about that on method.

please help.
Thank you in advance.

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

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

发布评论

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

评论(3

醉生梦死 2024-09-11 17:24:10

protobuf-net 是一个值序列化器;除了属性之外,它对方法一无所知。如果您使用相同(或兼容)的类型,则该方法将自动出现,但这与 protobuf-net (或任何其他序列化)无关。

重新讨论添加属性的话题;在当前版本中,它通常需要一些东西来知道要序列化哪些属性(更重要的是:使用什么标识符)。有一种隐式模式,但我不推荐它,除非您知道您不会再次更改类型。曾经。完全没有。

在“v2”中,您可以删除属性;您可以选择为此使用外部模型,因此您可能会:(

var model = TypeModel.Create();
model[typeof(MeterSizeInfo)].Add("MeterSizeId", "MeterSizeName");

不要在确切的 API 上引用我,而是类似的东西)

然后您可以使用 model.Serialize

protobuf-net is a value serializer; it doesn't know anything about methods, except for properties. If you use the same (or compatible) type, then the method will be present automatically, but this is nothing to do with protobuf-net (or any other serialization).

Re the topic of adding attributes; with the current release it generally needs something to know which properties to serialize (and more importantly: with what identifiers). There is an implicit mode, but I don't recommend it unless you know you aren't going to be ever changing the type again. Ever. At all.

In "v2", you can remove the attributes; you have the option of using an external model for this, so you might have:

var model = TypeModel.Create();
model[typeof(MeterSizeInfo)].Add("MeterSizeId", "MeterSizeName");

(don't quote me on the exact API, but something like that)

You can then use model.Serialize etc

蹲在坟头点根烟 2024-09-11 17:24:10

不——不应该在那里。你不能序列化一个方法!

No - shouldn't be there. You can't serialise a method!

留蓝 2024-09-11 17:24:10

不仅属性以这种方式可读可写......所以您不能为方法添加属性。

No only properties are readable and writable in that way... so you can't add the attribute for a method.

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