System.Serializable 属性消失在 Windows 10 个 UWP 应用程序中?
System.Serializable attribute gone in Windows 10 UWP apps?
在尝试将开源库 (Aforge.net) 移植到 UWP 时,我发现 System.Serializable 属性似乎不存在。 UWP 参考的工作方式略有不同,我仍在努力思考这些变化,所以我希望我只是遗漏了一些简单的东西。
我的问题是,有人可以确认 System.Serializable 属性 works/should 是否适用于 UWP 应用程序吗?我试过查看 MSDN 和其他各种 google 来源,但无法以任何方式找到任何证据。
非常感谢任何帮助。
更新
看起来我可能需要使用 DataContract / DataMember 属性而不是 Serializable 就像这里提到的可移植库:Portable class library: recommended replacement for [Serializable]
想法?
您需要使用以下属性:
用
标记class
[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);
}
}
祝你好运,伙计。
有个窍门,
您可以覆盖这两个缺失引用的 Class 定义:
System.SerializableAttribute
System.ComponentModel.DesignerCategoryAttribute
这里是代码:
namespace System
{
internal class SerializableAttribute : Attribute
{
}
}
namespace System.ComponentModel
{
internal class DesignerCategoryAttribute : Attribute
{
public DesignerCategoryAttribute(string _) { }
}
}
在尝试将开源库 (Aforge.net) 移植到 UWP 时,我发现 System.Serializable 属性似乎不存在。 UWP 参考的工作方式略有不同,我仍在努力思考这些变化,所以我希望我只是遗漏了一些简单的东西。
我的问题是,有人可以确认 System.Serializable 属性 works/should 是否适用于 UWP 应用程序吗?我试过查看 MSDN 和其他各种 google 来源,但无法以任何方式找到任何证据。
非常感谢任何帮助。
更新
看起来我可能需要使用 DataContract / DataMember 属性而不是 Serializable 就像这里提到的可移植库:Portable class library: recommended replacement for [Serializable]
想法?
您需要使用以下属性:
用
标记class[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);
}
}
祝你好运,伙计。
有个窍门,
您可以覆盖这两个缺失引用的 Class 定义:
System.SerializableAttribute
System.ComponentModel.DesignerCategoryAttribute
这里是代码:
namespace System
{
internal class SerializableAttribute : Attribute
{
}
}
namespace System.ComponentModel
{
internal class DesignerCategoryAttribute : Attribute
{
public DesignerCategoryAttribute(string _) { }
}
}