An interesting error “Operation could destabilize the runtime”

So I came across an interesting error today that I thought was worth mentioning. This very a very vague error and not at all useful. There is a helpful blog post by Matt Wrock  here. Although my issue was different, it did give me some ideas on what was going on. Apparently, to discover the issue, Matt used a tool called  PEVerify.  This apparently loads your code gives you more information than the normal debugger does. It is shipped with .NET.To learn more about it, there is a SO answer here.

So Matt’s issue turned out to be that the stack was expecting a string[] and was passed a List<string>. This is because of the string[] (which is of type IEnumerable, but in the namespace System.Collections.IEnumerable). Even though the List<string> is IEnumerable, it is actually of type IEnumerable<T>. The generic IEnumerable<T> is in namespace System.Generic.IEnumerable. Thus, the namespaces are a mismatch and therefore the runtime failure.

So the lesson learned here is to make sure you use the same exact object and namespace. If you are using a generic object or interface, make sure that what your are passing in supports generics. A string array does not in this case under the .NET 2+ SDK. The theory is that it thinks this might be a potential unsafe type causing the trust issue.

How to Fix this;

The easiest way to fix this issue is to force the array into a list by doing a .ToList() on the array. This will make the list a List<T> aka in this case List<string>. This will force the cast and you will be on your merry way. Basically, behind the scenes, .NET does a for loop that loops through the array in the normal convention (for ( int count =0; count < array.Count(); count++) ) and adds each item to a List<T> and returns that list to the caller. This will make sure you get the exact match you are looking for.

 

Hope this helps someone else with their issues!

Happy Coding Smile

Advertisements

About Gregg Coleman

I am Senior-level Software Engineer working primarily these days with .NET. I have a good working knowledge of ASP.NET MVC, Web Forms, WCF web services and Windows Services. I spend much of my time in the Web Services (SOAP and REST) world in my current job designing and implementing various SOA architectures. I have been in the software engineering industry for about 6 years now and will not now nor ever consider myself an "expert" in programming because there is always so much to learn. My favorite thing about designing software is there are always new emerging technologies and something to learn every day! My current job has me spending much of my job on the bleeding edge of technologies and changing gears all the time, so I'm never bored and always challenged. On my spare time I enjoy weight training, reading and venturing to new places near by. Of course programing and learning new technologies are another hobby of mine.
This entry was posted in .NET, .NET 4.5, ASP.NET, C#, Databases, Exception handling, Exceptions, Generics, LINQ, MVC, Programming, Validation and tagged , , , , , , . Bookmark the permalink.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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