DataContract属性,并在.NET Serializable属性的区别 [英] difference between DataContract attribute and Serializable attribute in .net
问题描述
我想创建一个使用下面的方法的对象的深克隆
I am trying to create a deep clone of an object using the following method.
public static T DeepClone<T>(this T target)
{
using (MemoryStream stream = new MemoryStream())
{
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, target);
stream.Position = 0;
return (T)formatter.Deserialize(stream);
}
}
这个方法需要一个对象,它是序列化,即一个对象一类谁是有一个属性就可以了序列化。我有是有属性DataContract就可以了,但方法不具有这种属性的工作一类。我认为DataContract也是一种串行的,但也许比的序列化的不同。
This method requires an object which is Serialized i.e. an object of a class who is having an attribute "Serializable" on it. I have a class which is having attribute "DataContract" on it but the method is not working with this attribute. I think "DataContract" is also a type of serializer but maybe different than that of "Serializable".
任何人都可以请给我两者之间的区别?也请让我知道,如果它可以创建一个对象的deepclone只有1属性,它确实既DataContract和可序列化的属性或创建deepclone的也许不同的方式工作?
Can anyone please give me the difference between the two? Also please let me know if it is possible to create a deepclone of an object with just 1 attribute which does the work of both "DataContract" and "Serializable" attribute or maybe a different way of creating a deepclone?
请帮帮忙!
推荐答案
的 序列化
是所需的 的BinaryFormatter
工作。
DataContract
和 数据成员
属性都与 的DataContractSerializer
。
您可以用装饰为串行属性的类。
You can decorate a class with attributes for both serializers.
这篇关于DataContract属性,并在.NET Serializable属性的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!