System.Serializable属性在Windows 10 UWP应用程序?

在尝试将开源库(Aforge.net)移植到UWP时,我发现System.Serializable属性似乎不存在。 UWP的参考工作有点不同,我仍然试图围绕这些变化,所以我希望我只是缺less一些简单的东西。

我的问题是,有人可以确认是否System.Serializable属性工作/应该在UW​​P应用程序中工作? 我试图通过MSDN和其他各种谷歌资源,但无法find任何证据的方式。

任何帮助是极大的赞赏。

更新
它看起来像我可能需要使用DataContract / DataMember属性,而不是像这里提到的便携库一样,可序列化: 可移植类库:build议replace[Serializable]

思考?

您需要使用以下属性:

标记课程

[DataContract] 

并用标记属性

 [DataMember] 

要么

 [IgnoreDataMember] 

例如:

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

正如上面的代码来自Lance McCarthy:

 [DataContract] public class Foo { [DataMember] public string SomeText { get; set; } // .... [IgnoreDataMember] public string FizzBuzz { get; set; } } 

另外,你可以使用我自己的扩展名(!!!如果你需要将文件保存到文件而不是字符串,则将MemoryStream更改为FileStream):

 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); } } } 

然后只是使用这些扩展:

 [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); } } 

祝你好运队友。