How to Do .Net Binary Serialization of an Object When You Don't Have the Source Code of the Class

Binary deserialization without object definition

Why are the values are in different order in the two files?

That is because member order is not based on the declaration ordering. http://msdn.microsoft.com/en-us/library/424c79hc.aspx

The GetMembers method does not return members in a particular order, such as alphabetical or declaration order. Your code must not depend on the order in which members are returned, because that order varies.

.

Why is there extra 3 bytes compared the 2 serialized objects?

First the TypeFormat 'TypesWhenNeeded' should actually be 'TypesAlways'. That is why there are so many differences. For example the 05 after '=null' becoming 03 is due to that.

Second you don't have the correct types. Looking at BinaryFormatter in ILSpy and the hex dump reveals that the members you marked as 'int' are actually 'string'.

public class SomeDataFormat // 16 field
{
public string Name { get; set; }
public string Country { get; set; }
public string UserEmail{ get; set; }
public bool IsCaptchaDisplayed{ get; set; }
public bool IsForgotPasswordCaptchaDisplayed{ get; set; }
public bool IsSaveChecked{ get; set; }
public string SessionId{ get; set; }
public string SelectedLanguage{ get; set; }
public string SelectedUiCulture{ get; set; }
public string SecurityImageRefId{ get; set; }
public string LogOnId{ get; set; }
public bool BetaLogOn{ get; set; }
public string Amount{ get; set; }
public string CurrencyTo{ get; set; }
public string Delivery{ get; set; }
public bool displaySSN{ get; set; }
}

What am I missing? How could I do this?

I don't see a way to do it with the given BinaryFormatter. You could decompile/reverse the way BinaryFormatter works.

How do I deserialize an object using BinaryFormatter that contains a field that no longer exists?

No, this is impossible. What you have to do is deserialize it to a class with the same structure as before, then manually migrate your old object to your new object, and save the new object for the future.

.NET Binary serialization metadata

The reason that no tool exists is because it's often not enough to create a type that only contains the data. The methods are often just as important as the data, especially with properties that don't just set their private variables. No one knows what those methods are.

With that said, it may be useful to have a tool that is at least able to generate a type to hold the data. Maybe you'll be the first one to create such a tool?

.NET Binary Serialize object with references to other objects . . . what happens?

All of the references to other objects will be serialized as well. If you deserialize the data, you will end up with a complete, working set of its data, including objects A, B, and C. That's probably the primary benefit of binary serialization, as opposed to XML serialization.

If any of the other classes your object holds a reference to are not marked with the [Serializable] attribute, you'll get a SerializationException at run-time (the image of which was shamelessly stolen from the web; run-time errors don't even look like this anymore in the current versions of VS):

    Example of an unhandled SerializationException

Further than that, I'm not really sure what "internal things" you were hoping to understand. Serialization uses reflection to walk through the public and private fields of objects, converting them to a stream of bytes, which are ultimately written out to a data stream. During deserialization, the inverse happens: a stream of bytes is read in from the data stream, which is used to synthesize an exact replicate of the object, along with type information. All of the fields in the object have the same values that they held before; the constructor is not called when an object is deserialized. The easiest way to think about it is that you're simply taking a snapshot-in-place of the object, that you can restore to its original state at will.

The class that is responsible for the actual serialization and deserialization is called a formatter (it always inherits from the IFormatter interface). It's job is to generate an "object graph", which is a generalized tree containing the object that is being serialized/deserialized as its root. As mentioned above, the formatter uses reflection to walk through this object graph, serializing/deserializing all object references contained by that object. The formatter is also intelligent enough to know not to serialize any object in the graph more than once. If two object references actually point to the same object, this will be detected and that object will only be serialized once. This and other logic prevents entering an infinite loop.

Of course, it's easy to have a good general understanding of how this process works. It's much harder to actually write the code that implements it yourself. Fortunately, that's already been done for you. Part of the point of the .NET Framework is that all this complicated serialization logic is built in, leaving you free from worrying about it. I don't claim to understand all of it myself, and you certainly don't need to either to take full advantage of the functionality it offers. Years of writing all that code by hand are finally over. You should be rejoicing, rather than worrying about implementation details. :-)

C# image binary serialization

As you suggested in the comments I made an answer out of my comment:

You can also save it as a base64 string and then later return it to byte data/image

Do the owned classes need to be Serializable if they're saved & loaded within the main class?

Yea, it seems they do.

It's much easier to conform to using JsonUtility.ToJson(myObject); in Unity than doing it myself.

https://docs.unity3d.com/Manual/JSONSerialization.html



Related Topics



Leave a reply



Submit