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=1.0.0.0, 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:

image

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:

image

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:

image

And after I moved into the scope of the delegate:

image

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

Comments

12/6/2011 8:58:23 PM #

Levrige Business Evolution Consultants

As a business owner you have many challenges and responsibilities.  Using Internet technology correctly can help you grow your business and increase sales quickly and easily without a huge investment.  Evolve your business and start working less while making more by leveraging Internet technology in your marketing.

Levrige Business Evolution Consultants | Reply

12/9/2011 3:01:54 AM #

free joomla templates

Hey  you actually make it seem really super easy beside with your presentation however I in finding this topic to be actually something that I feel I would by no means understand. It sort of feels too sophisticated and very vast for me. I am taking a look forward in your subsequent put up, I\'ll try to get the cling of it!

free joomla templates | Reply

1/3/2012 5:02:05 AM #

Robert

Hey  social media has come a long way from being a mere outlet for sharing thoughts and images among friends and families. It has become a popular and effective method for business owners…

Robert | Reply

1/26/2012 10:34:38 AM #

marketing.eserv2you.com

Be richer with this  Great program. It has changed my life now and forever
http://richersucess.com

Ella | Reply

1/26/2012 10:35:35 AM #

marketing.eserv2you.com

Be happier with this  amazing soft. It has changed my life now and forever
http://richersucess.com

Holmberg | Reply

1/27/2012 4:38:18 AM #

Atlanta SEO

What a timely post. I appreciate your meaningful and interesting feedback. I am surprised that more people do not debate this topic more often.

Atlanta SEO | Reply

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading



Who Am I?

My name is Jeff Morris a software engineer/developer in Southern California, USA. My platform du jour is dotnet, but I dabble on the darkside occasionally.

Tag cloud

Page List