binary-serialization

How does BinaryFormatter.Deserialize create new objects?

℡╲_俬逩灬. 提交于 2019-11-27 05:47:00
问题 When BinaryFormatter deserializes a stream into objects, it appears to create new objects without calling constructors. How is it doing this? And why? Is there anything else in .NET that does this? Here's a demo: [Serializable] public class Car { public static int constructionCount = 0; public Car() { constructionCount++; } } public class Test { public static void Main(string[] args) { // Construct a car Car car1 = new Car(); // Serialize and then deserialize to create a second, identical car

How to optimize class for viewstate

依然范特西╮ 提交于 2019-11-27 02:56:22
问题 If I have an object I need to store in viewstate, what kinds of things can I do to optimize the size it takes to store the object? Obviously storing the least amount of data will take less space, but aside from that, are there ways to architect the class, properties, attrbutes etc, that will effect how large the serialized output is? 回答1: My basic points. I use small names for class and variable, or I use the [ XmlAttribute ("ME")] command I try to not place default values especial if the are

What are the differences between the XmlSerializer and BinaryFormatter

爷,独闯天下 提交于 2019-11-27 02:38:20
I spent a good portion of time last week working on serialization. During that time I found many examples utilizing either the BinaryFormatter or XmlSerializer. Unfortunately, what I did not find were any examples comprehensively detailing the differences between the two. The genesis of my curiosity lies in why the BinaryFormatter is able to deserialize directly to an interface whilst the XmlSerializer is not. Jon Skeet in an answer to " casting to multiple (unknown types) at runtime " provides an example of direct binary serialization to an interface. Stan R. provided me with the means of

Is it possible to do .NET binary serialization of an object when you don't have the source code of the class?

不问归期 提交于 2019-11-26 20:21:38
I am using BinaryFormatter to do binary serialization of some objects in C#. However, some of the objects contain classes that I access via a DLL and do not have the source code for, so I can't mark them with the Serializable attribute. Is there a straightforward way to serialize them anyway? I have a workaround which involves taking class NoSource and making a new class SerializableNoSource for which the constructor takes a NoSource object and extracts all the information I need from it, but it's hacky. Are there any better alternatives? You could create a serialization surrogate . Imagine

How to analyse contents of binary serialization stream?

有些话、适合烂在心里 提交于 2019-11-26 19:08:48
问题 I'm using binary serialization (BinaryFormatter) as a temporary mechanism to store state information in a file for a relatively complex (game) object structure; the files are coming out much larger than I expect, and my data structure includes recursive references - so I'm wondering whether the BinaryFormatter is actually storing multiple copies of the same objects, or whether my basic "number of objects and values I should have" arithmentic is way off-base, or where else the excessive size

What are the differences between the XmlSerializer and BinaryFormatter

橙三吉。 提交于 2019-11-26 10:09:36
问题 I spent a good portion of time last week working on serialization. During that time I found many examples utilizing either the BinaryFormatter or XmlSerializer. Unfortunately, what I did not find were any examples comprehensively detailing the differences between the two. The genesis of my curiosity lies in why the BinaryFormatter is able to deserialize directly to an interface whilst the XmlSerializer is not. Jon Skeet in an answer to \"casting to multiple (unknown types) at runtime\"

Is it possible to do .NET binary serialization of an object when you don't have the source code of the class?

大兔子大兔子 提交于 2019-11-26 09:01:01
问题 I am using BinaryFormatter to do binary serialization of some objects in C#. However, some of the objects contain classes that I access via a DLL and do not have the source code for, so I can\'t mark them with the Serializable attribute. Is there a straightforward way to serialize them anyway? I have a workaround which involves taking class NoSource and making a new class SerializableNoSource for which the constructor takes a NoSource object and extracts all the information I need from it,