SharpCrafters Forum – Serialization in embedded resource (2.0.2.779)

SharpCrafters Support Forum

        


Serialization in embedded resource... Expand / Collapse
Author
Message
Posted 1/5/2010 10:16:14 PM


Community Member
Hello Gael,

in my own Plug-In I create an Object-Graph of serializable Types. Then I serialize this Graph with an BinaryFormatter. I wanted to add this as an embedded Resource to the Assembly. So it's very similar to the architecture that LAOS uses to maintain state between compile- and runtime.

Is the ability to add embedded resources somehow private to LAOS? I thought that it's in AssemblyManifestDeclaration.Resources, but that's a readonly collection. How can I add a resource in my plugin?

Another related question: The the following code:

var myType = new TypeDefDeclaration();
// construct type and members...

var systemType = myType.GetSystemType(null, null); // throws an TypeNotFoundException; because the Assembly doesn't contain this type before assembling?
var systemType = myType.GetReflectionWrapper(null, null); // is ok so far ... it returns an instance a Type named '^tV\\+aB3YGg4\\+6' which is derived (indirectly) from System.Type.

// now I set this Type to a property of an Object
myMetadata.AdapterType = systemType;

The "myMetadata" Instance is contained within the Object-Graph which shall be serialized into the embedded Resource. But then I get the following exception: "Type '^tV\\+aB3YGg4\\+6' in Assembly 'PostSharp.Sdk, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b13fd38b8f9c99d7' is not marked as serializable.".

I know why I get this, and I could implement an ISerializationSurrogate for this Type. The ISerializationSurrogate would serialize the Type like a System.RuntimeType (it could call System.UnitySerializationHolder.GetUnitySerializationInfo, but this is internal, so it would better reimplement the serialization protocol).

Do you consider this as a bug in PostSharp? May it be possible in the 2.0 final version to generally be able serialize Instances returned by .GetReflectionWrapper()?

Thanks so far!
Günter
Post #680
Posted 1/6/2010 5:14:56 AM


Gael Fraiteur

SharpCrafters
Hi Gunter,

The reflection wrapper (derived from System.Type) you receive is really not serializable. You should serialize something else, for instance the qualified type name.

AssemblyManifestDeclaration.Resources is not read-only; you can create a new ManifestResourceDeclaration and add it to the collection.
Post #681
Posted 1/7/2010 12:47:42 PM


Community Member
Hello Gael,

thank you for your response. I've just seen that the Resources Collection is *not* read-only. I don't know where I've looked instead.... sorry.

I've serialized the GetReflectionWrapper Type with a ISerializationSurrogate. But this code depends on the obfuscated name of the Wrapper-Type, and on the name of internal mscorlib classes and methods, so it's clearly not for production code. So I'm planing to replace the serialization mechanism completely with coded (injected) initialization (like the 'null' LAOS Serializer). Seems to be the better choice, since my types are not extensible and the hierarchy is static. This will also avoid versioning and security problems off-hand.

Günter
Post #682
Posted 1/7/2010 4:21:11 PM


Gael Fraiteur

SharpCrafters
Hi Gunter,

Thanks for feedback.
The current obfuscation algorithm always returns the same obfuscated string for the same class clear name (it's actually a MD5 hash <!-- s:) --> <!-- s:) -->), but of course there is no guarantee we don't change the algorithm or rename the class.

It is safer to serialize the class name.

-gael
Post #683
« Prev Topic | Next Topic »


All times are GMT +1:00, Time now is 11:32am

Powered By InstantForum.NET v4.1.4 © 2010
Execution: 0.206. 6 queries. Compression Disabled.