Notifications
Clear all

[Closed] Assembly independent binary de-/serialization

Hi,

I know there are some resources on this on the web already but I’m wondering if any of you have done any assembly independent object serialization and deserialization in c#?
I have an object that needs some tedious setup and training before I can use it in Max that is easier done in VS compared to max. But de-/serialization breaks when you change assemblies – in my case from console application to class library, with the same code base. So I’m looking for a way to serialize objects in VS that I can deserialize in max.

Any suggestions?

9 Replies
1 Reply
(@denist)
Joined: 11 months ago

Posts: 0

i don’t understand the problem. create any serializable class in VS, serialize it with BinaryFormatter and deserialize it in max… but of course the max has to know somehow about any included classes.

I did, it throws an error because of assembly mismatch. I serialize it in max with the project running as console application. I create a class library of the same project and try to deserialize it in max.

I got it working using this http://spazzarama.com/2009/06/25/binary-deserialize-unable-to-find-assembly/ .

But then I did take a closer look and found out I actually was using an old version of the assembly in max. Using standard deserialisation in max works as you say, and now I feel silly for making a whole thread about it Time to go to bed I guess.

Make sure to use SOAP when De/serializing to/from binary or there will be differences between AMD/Intel machines afair
I allways use the XmlSerializer, the files are bigger but your machine independent and can format the serialization verry easily

Soap does not support serialization of generics collections so it is a deal breaker for me. I’ll also try to keep it as light as possible, there is no need for a readable format since it is a trained neural network that is serialized. Any important information like input/output count etc. I store in the filename.
For the amd/intel issue you mention I have not encountered any problems there.

But it is always interesting to see different approaches to serialization used with max.

I think I found the real issue why I could not deserialize the file in the first place. If I place the assembly in the root folder of max it deserializes the file just fine. If I put the assembly in a folder and try to deserialize it throws the “Unable to find assembly…” error. Loading the assembly and using the “showmethods” method displays the static methods in the queried class, but when I try to use it it throws the error.

This works (with the assembly in the root folder):

(
    dotNet.loadAssembly "ArtificialNeuralNetwork.dll"
    ann = (dotNetClass "ArtificialNeuralNetwork.SerializationUtils").Deserialize ((getdir #scripts) + @"\MouseGesture\ann.bin")
)

This throws an error on the last line (with the assembly in root\scripts\MouseGesture\ folder):

(
    dotNet.loadAssembly ((getdir #scripts) + @"\MouseGesture\MouseCapture.dll") 
    showmethods (dotnetclass "ArtificialNeuralNetwork.SerializationUtils") --displays the methods in this class 
    ann = (dotnetclass "ArtificialNeuralNetwork.SerializationUtils").deserialize ((getdir #scripts) + @"\MouseGesture\ann.bin") --throws the error
)

This is the deserialize method in the assembly, nothing special about it:

public static object Deserialize(string filename) 
{ 
    Stream stream = File.OpenRead(filename);
    BinaryFormatter deserializer = new BinaryFormatter();
    object obj = deserializer.Deserialize(stream);
    stream.Close(); 
    return obj; 
}

I’m using other methods from the same assembly and they are working just fine.
What am I doing wrong?

I assume the MXS file (.ms||.mse) and the class lib are in the same folder?


		fn getAssemblyPath = 
		(
			local p = getSourceFileName()
			if p != undefined then 
			(
				getFilenamePath(p)
			)
			else
			(
				undefined
			)
		)--END getAssemblyPath FN

			local p = getAssemblyPath()
			--this.remFromMenu()
			
			if p != undefined AND doesFileExist (p + @"\foo.dll") do 
			(
				dotnet.loadAssembly (p + @"\foo.dll")
				this.manager = dotNetObject "bar.baz"
...

and why use static methods?

It is a macroscript, but I think that is not the point since I can load all the assemblies and use their methods in other parts of the script. It is just the deserialize method that throws the error when that specific assembly is not in the root folder.
The method is static because it is object independent and I like to store such method in static utility classes.

may it be a refferenced assembly your missing?