Archive for May 2002

 
 

Delegates and Serialization in .NET

Delegates to non-public methods cannot be deserialized.

Not that the documentation mentions this anywhere, mind you. I had to find out the hard way.

If you attempt to deserialize a delegate that points to a public method, the .NET framework will throw a SerializationException with the following message:

“Additional information: Serialization will not deserialize delegates to non-public methods.”

This sucketh. I had to make a bunch of methods public thus violating a number of principles on good design.

Common Intermediate Language

I’m meta-programming in IL today. (I’m writing C# code that generates new IL code.)

Two notes:

  1. The Intermediate Language is an extremely simple version of a stack machine. You should be able to learn it in a few hours by writing a Release mode version of short functions in C#, compiling them, looking at the generated code using ILDASM, and then looking up opcodes in MSDN.
  2. Why do opcodes have cryptic names? I mean, who would prefer ldc.i4 to PushInt32? Just because it is assembly it doesn’t have to be cryptic.