System.NotSupportedException : Cannot dynamically create an instance of System.Void

I came across an interesting bug during a major .Net 1.1 – .Net 2.0 upgrade the other day which took a bit of searching and showed what appears to be a fairly obscure bug in the .Net 1.1 libraries.

Our .Net project made use of the EasyMock .Net libraries in order to perfrom mock-tests using NUnit both to check our own work, and on the continuous build server to ensure that builds we release for testing are all they can be.

Updating to .Net 2.0 I noticed that EasyMock .Net itself didn’t have a later version designed for the new system, and running the old one was coming up with some obscure errors in the tests – the NotSupportedException shown on the title of this post.

A bit of investigation showed that EasyMock, when acting as a proxy for a method call, creats an instance of the return type of that method call. In the case of Void methods it attempts to create an instance of System.Void using Activator.CreateInstance().

According to the MSDN libraries for .Net 1.1, it is -never- possible to create a dynamic instance of a System.Void type. This line should never have worked for values of void, but no matter where I looked I couldn’t see why we got to that position in .Net 2.0 and not in .Net 1.1 – there was no conditional for Voids that should have shunted us on to a different track, nor any special handling for same.

Finally I booted up an old copy of the source in Visual Studio 2003 and traced through in there and lo-and-behold! It successfully created a dynamic instance of a System.Void type. For some reason, despite documentation to the contrary, it was able to do this in .Net 1.1.

It can’t in 2.0 or later, so if your code tries to do this I would suggest finding a different way to accomplish your goals, otherwise remain version-stuck and unable to update.

Also, Easymock .Net appears to be defunct, and in any later environment completely useless without an overhaul and careful examination of the source for other such unsupported ‘tricks’ that the developers may have used, either knowingly or without realising. I ripped it out of our system and replaced it was nmock2. It also hasn’t been updated past .Net 2.0, but it’s a step up.

Did require a complete test rewrite however. It was a pain.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s