2015-09-07 110 views
10

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

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

任何帮助,非常感谢。

更新
它看起来像我可能需要使用像在这里提到的便携式库DataContract /数据成员的属性,而不是Serializable接口:Portable class library: recommended replacement for [Serializable]

的思考?

+0

为什么反对票,无评论? – John

+0

我没有downvote,但这里没有问题,所以我可以明白为什么有人会。 –

+0

公平点。编辑包含一个问题。 – John

回答

13

您需要使用以下属性:与

[DataMember] 

[IgnoreDataMember] 

例如

马克与

[DataContract] 

类和标记属性:

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

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

由于从兰斯·麦卡锡上面的代码:

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

祝你好运队友。

0

有一个小窍门,

您可以覆盖这两名失踪引用的类定义:

System.SerializableAttribute 
System.ComponentModel.DesignerCategoryAttribute 

下面的代码:

namespace System 
{ 
    internal class SerializableAttribute : Attribute 
    { 

    } 
} 
namespace System.ComponentModel 
{ 
    internal class DesignerCategoryAttribute : Attribute 
    { 
     public DesignerCategoryAttribute(string _) { } 
    } 
} 
相关问题