Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to serialize a delegate that points to the base of an overridden virtual method?
    primarykey
    data
    text
    <p>While <a href="https://stackoverflow.com/questions/1334254">other</a> <a href="https://stackoverflow.com/questions/4357729">questions</a> about using reflection to bypass all safeties and directly call the base class's implementation of an overridden method have generally been met with derision and calls to redesign the offending code, I think I've stumbled upon a bizarre but legitimate use case: <a href="https://stackoverflow.com/questions/10267388">delegate serialization</a>. Since I've seen the other questions, <strong>please</strong> don't bombard me with advice to redesign my code and stop trying to bypass the type system -- I'm writing a <strong>serialization formatter</strong>, and those <a href="http://msdn.microsoft.com/en-us/library/system.runtime.serialization.formatterservices.getuninitializedobject.aspx" rel="nofollow noreferrer">already get a pass to ignore constructors</a>.</p> <p>Much to my dismay, even the v2.0 BCL's <a href="http://msdn.microsoft.com/en-us/library/system.runtime.serialization.formatters.binary.binaryformatter.aspx" rel="nofollow noreferrer"><code>BinaryFormatter</code></a> fails this simple NUnit test:</p> <pre><code>[TestFixture] public class DelegateSerializationTestFixture { [Test] public void DelegateSerializationTest() { var bigKitty = new Lion(); var kittyNoise = bigKitty.GetKittyNoiseFunc(); Assert.AreEqual("Meow", kittyNoise()); var stream = new MemoryStream(); var formatter = new BinaryFormatter(); formatter.Serialize(stream, kittyNoise); stream.Position = 0; formatter = new BinaryFormatter(); var kittyNoise2 = (Func&lt;string&gt;)formatter.Deserialize(stream); Assert.AreEqual("Meow", kittyNoise2()); // returns Grrr } } [Serializable] public class Lion : Cat { public override string GetNoise() { return "Grrr"; } public Func&lt;string&gt; GetKittyNoiseFunc() { return base.GetNoise; } } [Serializable] public class Cat { public virtual string GetNoise() { return "Meow"; } } </code></pre> <p>If <code>BinaryFormatter</code> itself can't get this right, I guess I shouldn't be surprised that my own Compact Framework 3.5 binary serialization implementation drops the ball as well.</p> <p>If it's truly impossible to reconstruct such a delegate using only the Compact Framework v3.5's limited reflection capabilities -- and it may well be -- is there a way to at least detect such a delegate so that my serializer can throw instead of leaving incorrect data in the stream?</p> <p>So far, the only way I know of to detect this condition at serialization time is to use full-trust reflection to compare the private method-pointer value in the original delegate with the value I get from reconstructing the delegate using its publicly visible properties.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload