开发者

Is it necessary to declare attribute [DataMember(Order=n)] on public method?

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 ex开发者_StackOverflowposing 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.


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


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜