Migrating serialization for a specific type of .NET

Please consider this class example:

[Serializable]
public class SomeClass
{
    private DateTime _SomeDateTime;

    public DateTime SomeDateTime
    {
        get { return _SomeDateTime; }
        set { _SomeDateTime = value; }
    }
}

I would like to serialize any DateTime declared in a class according to my own rules. Members of this class will change frequently, and I do not want to maintain my own serializer for each change. I would also like this behavior to be inherited by subclasses, rather than encode a custom serializer for each. Serialization is displayed by the web service. Thanks for any help!

+3
source share
3 answers

OnSerializing OnDeserializing, , . .

+3

Nullable date

public DateTime? SomeDateTime {get; set;}

, . , , . , ​​ ISerializable, , .

, ISerializable ? , System.Exception ISerializable, ( ) , - , AppDomains.

+3

, bool ShouldSerializeSomeDateTime() / , , , . , :

public string SomeDateTimeFormatted {   get {return theField == DateTime.MinValue? ": theField.ToString(" R");}//   set {... ...} }

It is better to stick with built-in serialization if you can, although partially, reduce the amount of code you need to write. Josh's assumption for null values ​​DateTime (DateTime?) Is good, although it may not be completely empty-string vs formatted-string - I expect it to use xsi: nil markup.

0
source

Source: https://habr.com/ru/post/1697924/


All Articles