views:

1624

answers:

5

I've got a program that picks up some code from script files and compiles it. And It works fine.

The problem is: in the scripts I declare a couple of classes and I want to serialize them. Obviously the C# serializer (xml and binary) doesn't like to serialize and the de-serialize object defined in a in-memory assembly.

I prefer to don't leave the in-memory assembly so i'm looking for another way of serializing, but in case, is possible to build assembly in memory and eventually write it on file ?

+1  A: 

You could always write your own ToXml function using reflection to write out your property data to a string. Then your object would deserialize itself.

Just a thought.

A: 

If you want to create assemblies dynamically look into IL emitting via reflection. Here is a good article to get you started.

Andrew Hare
A: 

So just to clarify, are you asking how you can serialize a type if it hasn't got the [Serializable] attribute applied?

One solution is to use the WCF Data Contract Serializer: http://msdn.microsoft.com/en-us/library/ms731923.aspx.

Obviously this will only work if you can target .Net 3.0 or higher.

Alternately you can implement an ISerializationSurrogate. Jeffrey Richter has a great introduction at http://msdn.microsoft.com/en-us/magazine/cc188950.aspx.

Antony Perkov
A: 

I would avoid all built-in serialization whenever possible, both are badly broken. For example, XML serialization doesn't support dictionaries and normal serialization/SOAP doesn't support generics. And both have versioning issues.

It is time consuming, but createing ToXML and FromXML methods is probably to most effective way to go.

Jonathan Allen
A: 

Hava a look at here for custom serialisers, which is a sample for dictionary XML serializing

Can Erten