开发者

Build failure in unit test project with accessors of a project containing covariant types

I added a covariant interface to our project:

interface IView
{
}

interface IPresenter<out TView> where TView : IView
{
    TView View { get; }
}

I created some classes, implementing these interfaces:

class TestView : IView
{
}

class TestPresenter : IPresenter<TestView>
{
  public TestView View
  {
    get { return something; }
  }

  private void DoSomething()
  {
  }
}

And I can use this without problems:

IPresenter<IView> presenter = new TestPresenter();

So everything seems right, so I assume my covariance usage is correct. Unfortunately our unit test projects contain private accessors from some types located in the same project like the covariant interface, which causes a build failure.

Could not load type 'GenericInheritanceTest.IPresenter_Impl`1' from assembly 'GenericInheritanceTest_Accessor, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' because it declares a covariant or contravariant type parameter and is not an interface or delegate.

What exactly is the problem here? Is there a failure in my implementation, resp. how to fix this? Can not be, that we have to avoid accessors 开发者_开发知识库as soon as we use covariant types??? Is it possible to prevent creating accessors for certain types to solve this problem?


This is a bug in Visual Studio 2010. It has been reported to Microsoft Connect but has been closed and will apparently not be fixed.

According to a blog entry by Bruce Taimana development of the private accessor feature has been stopped and may be removed in future versions of Visual Studio. Possible alternatives listed are:

  1. Use the Microsoft.VisualStudio.TestTools.UnitTesting.PrivateObject class to assist in accessing internal and private APIs in your code. This is found in the Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll assembly.

  2. Create a reflection framework that would be able to reflect off your code to access internal or private APIs.

  3. If the code you are trying to access is internal, you may be able to access your APIs using the InternalsVisibleToAttribute so your test code can have access to the internal APIs.


I was struggling with this error and also got an error: The "BuildShadowTask" failed unexpectedly. The only way to get rid of the error when I tried resolving it here is removing the out keyword from generics, i.e. a covariant interface.

Actually I got this error when Resharper suggested to me that type parameter could be covariant:

private delegate TResult Action<TResult>(); 

Changed to:

private delegate TResult Action<out TResult>();

Sadly, I had to change it back again and disabling the Resharper warning in a comment:

// ReSharper disable once TypeParameterCanBeVariant 
private delegate TResult Action<TResult>();

One strategy can therefore be to search for:

"<out"

in a project and remove the out keyword, just to get it to compile. Not very elegant, but not very elegant by Microsoft either, because this was rapported five years ago through Microsoft Connect and they have chosen to just close the problem. The problem is in Unit Test Projects. It does not help to change from Visual Studio Unit Testing Framework to NUnit Testing Framework.


If you are using Unity Interception, and your parameter is labeled as out, Unity Interception will cause this error. The reason for this is that Interception must be able to read the parameter list. So Similar to the case above, if Resharper is warning that the parameter can be covariant, this warning needs to be ignored.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜