便携式类库:建议更换为[Serializable接口] [英] Portable class library: recommended replacement for [Serializable]

查看:230
本文介绍了便携式类库:建议更换为[Serializable接口]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我移植一个.NET框架C#类库便携式类库。一个经常出现的问题是如何处理与装饰的 [Serializable接口] 属性类,因为这个属性不是便携式类库子集的一部分。在便携式类库集序列化的功能,而不是似乎是由<一个覆盖href=\"http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractattribute.aspx\">DataContractAttribute.


  • 要preserve尽可能多的便携式类库中的功能可能是它足以取代 [Serializable接口] [ DataContract] 属性(其含义是各个领域和受序列化属性需要与装饰[数据成员] 为好)?

  • 什么(如果有的话)我会 不可以 的能够使用这种方法,我的 的事要做与 [Serializable接口] 应用?

  • 是否有侵扰性较低的方法吗?

由于 [DataContract] [数据成员] 的使用,我正在考虑改变$ C $大意如下℃。是否有使用这种方法的任何明显的缺陷?有什么办法制定同样的事情更简洁?

 #如果PORTABLE
    [DataContract]
#其他
    [Serializable接口]
#万一
    公共类SerializableClass:SerializableBaseClass
    {
       ...
#如果!PORTABLE
        保护SerializableClass(的SerializationInfo信息,的StreamingContext上下文)
             :基地(信息,上下文)
        {
        }
#万一
        ...
#如果P​​ORTABLE
        [数据成员]
#万一
        私人类型1 _serializableField;#如果P​​ORTABLE
        [数据成员]
#万一
        私人2型SerializableProperty {搞定;组; }        ...
    }


解决方案

便携式类库(PCL)现在可以在所有平台上[二○一三年十月一十四日]


  

之前今天的释放,有一个与PCL许可限制
  引用程序集,这意味着他们只能在Windows上使用。
  随着今天的发布,我们宣布的一个新的独立版本
  与许可,允许它是PCL引用程序集的上使用
  任何平台 - 包括非微软的人
这使得开发人员
  更大的灵活性,并与.NET做伟大的事情。


来源: Portable类库(PCL)现在可以在所有平台上

下载: 的Microsoft .NET便携库参考组件4.6 RC

只是为了参考允许一套组件是:


  

mscorlib.dll中


  
  

System.dll中


  
  

System.Core.dll


  
  

system.xml.dll的


  
  

System.ComponentModel.Composition.dll(MEF)


  
  

System.Net.dll


  
  

System.Runtime.Serialization.dll


  
  

System.ServiceModel.dll


  
  

System.Xml.Serialization.dll


  
  

System.Windows.dll中(Silverlight的)


据我知道你需要标记是数据成员属性的字段,并添加 DataContract 属性。

更新

是的。

您可以查看便携式类库的解决方案是如何 Json.NET 实施。你可以找到从这里的源的\\ src \\ Newtonsoft.Json.Portable 当你下载的项目的解决方案://json.$c$cplex.com/downloads/获得/ 501717> Json.NET 4.5版本10(来源+二进制)

他们基本上使用的是带有自定义属性提供一种方法

//不要使用序列化

 #如果!(SILVERLIGHT || WINDOWS_PHONE || NETFX_CORE ||便携式)
  [Serializable接口]
#万一

//使用自定义提供

 #如果NETFX_CORE || PORTABLE
使用ICustomAttributeProvider = Newtonsoft.Json.Utilities.CustomAttributeProvider;
#万一

如果项目的移动

 #如果掌上电脑和放大器;!&安培; !NET20
      DataContractAttribute dataContractAttribute = GetDataContractAttribute(的objectType);
      如果(dataContractAttribute!= NULL)
        返回MemberSerialization.OptIn;
#万一

其中的选择启用描述是:

  ///&LT;总结&gt;
    ///只有会员必须标以&lt;见CREF =JsonPropertyAttribute/&GT;或LT;见CREF =DataMemberAttribute/&GT;被序列化。
    ///该成员的序列化模式也可以通过以&lt标志着类别设置;见CREF =DataContractAttribute/取代。
    ///&LT; /总结&gt;
    选择参加,

希望它帮助。

更新2


  

我使用[DataContract],而不是失去任何能力
  [可序列化],否则我将仍然能够做到的一切,
  [Serializable接口]支持?


您可以做到这一点的一切序列化除了支持
在对象是如何设置的名称和顺序之外的序列控制。

使用<一个href=\"http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractserializer.aspx\">DataContractSerializer有几个好处:

连载任何装饰的 [数据成员] 即使它不是公共可见

除非您明确告诉它的不能序列任何东西(选择加入)

您可以定义哪些元素是用 [数据成员] [订购=] 属性序列化的顺序C>

不需要反序列化参数的构造函数

比XmlSerializer的速度快10%。

在这里阅读更多:<一href=\"http://www.danrigsby.com/blog/index.php/2008/03/07/xmlserializer-vs-datacontractserializer-serialization-in-wcf/\">XmlSerializer VS DataContractSerializer的

亦作参考:

DataContract 支持以下几种类型的默认模式类型的序列:
CLR内建类型


  

字节数组,日期时间,时间跨度,GUID,乌里,XmlQualifiedName,
  的XmlElement和XmlNode的数组


  
  

枚举


  
  

标记DataContract或CollectionDataContract属性类型


  
  

这实现IXmlSerializable的类型


  
  

数组和集合类,包括列表,字典和
  哈希表


  
  

标记有Seri​​alizable属性包括那些类型
  实现了ISerializable


  
  

通过以​​上都不是属性(POCO),但与默认类型
  构造


I am porting a .NET Framework C# class library to a Portable Class Library. One recurring problem is how to deal with classes decorated with the [Serializable] attribute, since this attribute is not part of the Portable Class Library subset. Serialization functionality in the Portable Class Library subset instead appears to be covered by DataContractAttribute.

  • To preserve as much of functionality as possible in the Portable Class Library, is it sufficient to replace [Serializable] with the [DataContract] attribute (with the implication that all fields and properties subject to serialization would need to be decorated with [DataMember] as well)?
  • What (if anything) will I not be able to do with this approach that I can do with [Serializable] applied?
  • Is there a less intrusive approach?

Given that [DataContract] and [DataMember] are used, I am considering to change the code along the following lines. Are there any obvious flaws with this approach? Is there any way to formulate the same thing less verbose?

#if PORTABLE
    [DataContract]
#else
    [Serializable]
#endif
    public class SerializableClass : SerializableBaseClass
    {
       ...
#if !PORTABLE
        protected SerializableClass(SerializationInfo info, StreamingContext context)
             : base(info, context)
        {
        }
#endif
        ...
#if PORTABLE
        [DataMember]
#endif
        private Type1 _serializableField;

#if PORTABLE
        [DataMember]
#endif
        private Type2 SerializableProperty { get; set; }

        ...
    }

解决方案

Portable Class Library (PCL) now available on all platforms [14 Oct 2013]

Prior to today’s release, there was a license restriction with the PCL reference assemblies which meant they could only be used on Windows. With today’s release we are announcing a new standalone release of the PCL reference assemblies with a license that allows it to be used on any platform – including non-Microsoft ones. This enables developers even more flexibility and to do great things with .NET.

Source: Portable Class Library (PCL) now available on all platforms

Download: Microsoft .NET Portable Library Reference Assemblies 4.6 RC

Just for the reference the allowed set of assemblies are:

mscorlib.dll

System.dll

System.Core.dll

System.Xml.dll

System.ComponentModel.Composition.dll (MEF)

System.Net.dll

System.Runtime.Serialization.dll

System.ServiceModel.dll

System.Xml.Serialization.dll

System.Windows.dll (from Silverlight)

As far as I know you need to mark the fields with DataMember attribute, and add the DataContract attribute.

UPDATE

Yes.

You can look how Json.NET portable class library solution is implemented. You can find the solution in the Source\Src\Newtonsoft.Json.Portable when you download the project from here Json.NET 4.5 Release 10 (source + binary).

Basically they are using an approach with a custom attribute provider

//don't use Serializable

#if !(SILVERLIGHT || WINDOWS_PHONE || NETFX_CORE || PORTABLE)
  [Serializable]
#endif

//use custom provider

#if NETFX_CORE || PORTABLE
using ICustomAttributeProvider = Newtonsoft.Json.Utilities.CustomAttributeProvider;
#endif 

And if project is PORTABLE

#if !PocketPC && !NET20
      DataContractAttribute dataContractAttribute = GetDataContractAttribute(objectType);
      if (dataContractAttribute != null)
        return MemberSerialization.OptIn;
#endif

where OptIn description is:

 /// <summary>
    /// Only members must be marked with <see cref="JsonPropertyAttribute"/> or <see cref="DataMemberAttribute"/> are serialized.
    /// This member serialization mode can also be set by marking the class with <see cref="DataContractAttribute"/>.
    /// </summary>
    OptIn,

Hope it helps.

UPDATE 2

Am I losing any abilities using [DataContract] instead of [Serializable], or will I still be able to do everything that [Serializable] supports?

You can do everything that Serializable supports except control over how the object is serialized outside of setting the name and the order.

Using DataContractSerializer has several benefits:

serialize anything decorated with a [DataMember] even if it's not public visible

can't serialize anything unless you specifically tell it to ("opt-in")

you can define the order in which the elements are serialized using the [Order=] attribute on the [DataMember]

doesn't require a parameterless constructor for deserialization

is 10% faster than XmlSerializer.

Read more here: XmlSerializer vs DataContractSerializer

Also for the reference:

DataContract supports serialization of the following kinds of types in the default mode: CLR built-in types

Byte array, DateTime, TimeSpan, GUID, Uri, XmlQualifiedName, XmlElement and XmlNode array

Enums

Types marked with DataContract or CollectionDataContract attribute

Types that implement IXmlSerializable

Arrays and Collection classes including List, Dictionary and Hashtable

Types marked with Serializable attribute including those that implement ISerializable

Types with none of the above attributes (POCO) but with a default constructor

这篇关于便携式类库:建议更换为[Serializable接口]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆