在Silverlight 4 Serializable属性 [英] Serializable attribute in silverlight 4
问题描述
所以,做我们还是我们没有在Silverlight 4 Serializable属性?我有一些在互联网上混乱的回应。当我尝试在我的code使用它,我得到一个命名空间的错误。这些都是我的,包括
So do we or do we not have a Serializable attribute in silverlight 4? I have some confusing responses on the internet. When I try to use it in my code, i get a namespace error. These are my includes
using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Runtime.Serialization;
我有组件系统,System.Runtime.Serialization添加到我的项目。
I have the assemblies System ,System.Runtime.Serialization added to my project.
一个跟进的问题是,如果它不是用在Silverlight我怎么正确地序列单身?因为我正打算使用这里给出的示例的http://msdn.microsoft.com/en-us/library/system.runtime.serialization.iserializable.aspx
A follow up question is, if it is not available in Silverlight how do I correctly serialize a singleton? Since I was planning to use the example given here http://msdn.microsoft.com/en-us/library/system.runtime.serialization.iserializable.aspx
感谢
推荐答案
这是你不能在Silverlight使用.NET属性,但你可以用DataContract序列化。
That's a .NET attribute which you can't use in Silverlight, but you can use DataContract to serialize.
有关独立(非WCF)序列化/反序列化,有可以使用的三个组成部分:
For stand-alone (non-WCF) serialization/deserialization, there are three components which can be used:
System.Runtime.Serialization.DataContractSerializer(从System.Runtime.Serialization.dll)
System.Runtime.Serialization.Json.DataContractJsonSerializer(从System.ServiceModel.Web.dll)
System.Xml.Serialization.XmlSerializer(从System.Xml.Serialization.dll)
System.Runtime.Serialization.DataContractSerializer (from System.Runtime.Serialization.dll) System.Runtime.Serialization.Json.DataContractJsonSerializer (from System.ServiceModel.Web.dll) System.Xml.Serialization.XmlSerializer (from System.Xml.Serialization.dll)
使用DataContractSerializer的一个简单的例子:
A simple example using the DataContractSerializer:
string SerializeWithDCS(object obj)
{
if (obj == null) throw new ArgumentNullException("obj");
DataContractSerializer dcs = new DataContractSerializer(obj.GetType());
MemoryStream ms = new MemoryStream();
dcs.WriteObject(ms, obj);
return Encoding.UTF8.GetString(ms.GetBuffer(), 0, (int)ms.Position);
}
从这个线程示例:<一href=\"http://forums.silverlight.net/forums/p/23161/82135.aspx\">http://forums.silverlight.net/forums/p/23161/82135.aspx
这篇关于在Silverlight 4 Serializable属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!