The Mysterious +<>c__DisplayClass1

by jmorris 9. July 2011 01:23

While unit testing a a VirtualPathProvider today, I came upon an interesting exception:

System.Runtime.Serialization.SerializationException : Type 'Foo.Web.Core.UnitTests.Plugins.Modules.PluginRegistrarTests+<>c__DisplayClass1' in assembly 'Foo.Web.Core.UnitTests, Version=, Culture=neutral, PublicKeyToken=null' is not marked as serializable.

I was stumped by this…what, where and who  is +<>c__DisplayClass1'? Granted my scenario was somewhat complex in that I am testing in a by using a fake AppDomain that mimics the ASP.NET HostingEnvironment ala this post.

When I saw that error, I immediatly thought the problem was that I was missing the Serializable attribute, since the exception explicitly states: “[type] is not marked as serializable.” I added that attribute to class that I was loading into the faux AppDomain for the same result:


WTF? I was stumped! I googled around a bit and got side tracked by some discussions of MarshalByRefObject and finally stumbled upon something on Ode2Code. I wasn’t quite sure of what the problem was until I read some of comments. Most notable this one. A quick check of the IL with ILSpy confirmed my suspicions:


Fix was easy: simply move the declaration of the FakeHttpApplication class to within the scope of the delegate itself. Here is how I had it defined:


And after I moved into the scope of the delegate:


So, what was the problem? Basically it comes down to the scoping of Anonymous methods and how the compiler generates code to support them. Anonymous methods are simply compiler generated types. The generated type c__DisplayClass1 is not marked as serializable, so it fails when passed into the scope of the Anonymous delegate call…which is executing in another AppDomain (that does not contain the Anonymous type definition).

Tags: ,

Unit Testing

Jeff Morris

Tag cloud

Month List

Page List