Problem
I need to figure out the structure of a class that I do not have access to that was serialized using BinaryFormatter
, so that I can serialize more files in the same structure.
What I have tried
Using this answer as reference I was able to make the following code:
private static T TransformMSNRBFToObject<T>(byte[] data) where T : new()
{
var response = new T();
using (var stream = new MemoryStream(data))
Write((BinaryObject)NRBFReader.ReadStream(stream));
return response
}
private static void Write(BinaryObject bObj, string name = "", int depth = 0)
{
Trace.WriteLine($"{name} [{bObj.TypeName}] [{bObj.AssemblyName}]");
foreach (var key in bObj.Keys)
{
bObj.TryGetValue(key, out var value);
if (value is BinaryObject obj)
Write(obj, key, depth + 1);
else
Trace.WriteLine($"{value?.GetType().FullName} {key}");
}
}
This was able to determine the structures of multiple classes, including their namespaces & assemblies. This has got me most of the way there, but there are a few items left that I am unsure how to handle.
Properties
There are what appear to be properties that all have a forward slash /
within the variable name. They are all of primitive
types.
E.g. Item0/B
, Item1/B
, Setting/1/Index
, Setting/2/Value
, etc.
Methods
There is also something that I am unable to identify, but my best guess is that they are methods since the name always contains the word Method
.
When I output the key I get: DoSomethingMethod
When I output the namespace I get: NamespaceName+DoSomethingMethods
They always contain a key that gives this: System.Int32 value__
I am assuming that value__
is either an input parameter, or the return type.
However, when I add a method it does not serialize.
Questions
- Where is the variable supposed to be stored to result in a
/
within the variable name? - Alternatively, is there a way like with
XmlSerializer
's[XmlAttribute("attributeName")]
that I could decorate a property to force it to have a specific name? - What is the format of the items that appear to be methods?