Limitations of Private Accessors and Generic Methods (JIT Compiler Encountered an Internal Limitation)

Private accessors are really helpful for extensive unit testing. But they do have their limitations.

I wrote a generic protected method having a declaration like this:

protected ReturnType MyMethod<T>(OneClass, DateTime, DateTime, Func<OtherClass, T>, out List<T>);

Calling this method via an accessor in my unit test, I ran into an exception complaining “JIT Compiler encountered an internal limitation”.

First, I thought it was a LINQ issue, because I used LINQ inside to retrieve some data from a list, using the delegate passed. But I was wrong. Debugging into the test, the exception was thrown at the moment the method was called.

When I changed the declaration to

protected ReturnType MyMethod<T>(OneClass, DateTime, DateTime, Func<OtherClass, T>, out List<SomeType>);

the test finished successfully. But I had not implemented a generic method to be limited to the type of the resulting list.

Soon, the accessor got into suspicion. When I declared the generic version of the method as public, and used an instance of the implementing class, the test was successful too.

The solution in my case was to keep the method protected, derive a mock class from the implementing class in my test project, and use an instance of that derived class by the test.