Partager via


versioning serialized delegates

Got an interesting question about how to deserialize delegates serialized with an older version of an assembly. While deserializing, if the older version isnt available the engine might throw a TypeLoadException. The Serialization binder doesnt help in this case since Delegate holders implement IObjectReference and are serialized as simple type and method names (the holder itself is loaded fine). During deserialization IObjectReference.GetRealObject creates and binds the appropriate delegate, which fails if the correct assembly couldnt be loaded. 

The solution is to use AssemblyResolve event and load the appropriate assembly and the delegate would be deserialized as expected.

Note: Serializing delegates could lead to security issues, since a method of the same signature can be invoked if the serialized stream is modified. Thus be careful while serializing/deserializing delegates when you are deserializing from untrusted sources.

Comments