Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.Serializable attribute gone in Windows 10 UWP apps?

Tags:

.net

windows

uwp

While trying to port an open source library (Aforge.net) to UWP, I discovered the System.Serializable attribute does not seem to exist. References for UWP work a little differently and I'm still trying to wrap my head around the changes so I hope I'm just missing something simple.

My question is, can someone please confirm whether the System.Serializable attribute works/should work in a UWP app? I've tried looking through MSDN and various other google sources but cannot find any evidence one way or another.

Any help is greatly appreciated.

Update
It looks like I may need to use DataContract / DataMember attributes instead of Serializable like was mentioned here for portable libraries: Portable class library: recommended replacement for [Serializable]

Thoughts?

like image 814
John Avatar asked Sep 07 '15 22:09

John


2 Answers

You need to use the following Attributes:

Mark the class with

[DataContract]

and mark properties with

[DataMember]

or

[IgnoreDataMember]

For example:

[DataContract]
public class Foo
{
    [DataMember]
    public string Bar { get; set; }

    [IgnoreDataMember]
    public string FizzBuzz { get; set; }
}
like image 192
Lance McCarthy Avatar answered Nov 08 '22 00:11

Lance McCarthy


As code above from Lance McCarthy:

[DataContract]
public class Foo
{
    [DataMember]
    public string SomeText { get; set; }

    // ....

    [IgnoreDataMember]
    public string FizzBuzz { get; set; }
}

In addition you can use my own extension (!!! Change MemoryStream to FileStream if you need to save it to file instead of to string):

public static class Extensions
{
    public static string Serialize<T>(this T obj)
    {
        var ms = new MemoryStream();
        // Write an object to the Stream and leave it opened
        using (var writer = XmlDictionaryWriter.CreateTextWriter(ms, Encoding.UTF8, ownsStream: false))
        {
            var ser = new DataContractSerializer(typeof(T));
            ser.WriteObject(writer, obj);
        }
        // Read serialized string from Stream and close it
        using (var reader = new StreamReader(ms, Encoding.UTF8))
        {
            ms.Position = 0;
            return reader.ReadToEnd();
        }
    }

    public static T Deserialize<T>(this string xml)
    {
        var ms = new MemoryStream();
        // Write xml content to the Stream and leave it opened
        using (var writer = new StreamWriter(ms, Encoding.UTF8, 512, leaveOpen: true))
        {
            writer.Write(xml);
            writer.Flush();
            ms.Position = 0;
        }
        // Read Stream to the Serializer and Deserialize and close it
        using (var reader = XmlDictionaryReader.CreateTextReader(ms, Encoding.UTF8, new XmlDictionaryReaderQuotas(), null))
        {
            var ser = new DataContractSerializer(typeof(T));
            return (T)ser.ReadObject(reader);
        }
    }
}

and then just use those extentions:

[TestClass]
public class UnitTest1
{
    [TestMethod]
    public void TestSerializer()
    {
        var obj = new Foo()
        {
            SomeText = "Sample String",
            SomeNumber = 135,
            SomeDate = DateTime.Now,
            SomeBool = true,
        };

        // Try to serialize to string
        string xml = obj.Serialize();

        // Try to deserialize from string
        var newObj = xml.Deserialize<Foo>();

        Assert.AreEqual(obj.SomeText, newObj.SomeText);
        Assert.AreEqual(obj.SomeNumber, newObj.SomeNumber);
        Assert.AreEqual(obj.SomeDate, newObj.SomeDate);
        Assert.AreEqual(obj.SomeBool, newObj.SomeBool);
    }
}

Good luck mate.

like image 4
ADM-IT Avatar answered Nov 07 '22 22:11

ADM-IT