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?
Put Unit tests in the same project as the code to achieve better encapsulation. You can easily test internal methods, which means you wont make methods public that should have been internal.
Unit testing is a software development process in which the smallest testable parts of an application, called units, are individually and independently scrutinized for proper operation. This testing methodology is done during the development process by the software developers and sometimes QA staff.
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:
Use the
Microsoft.VisualStudio.TestTools.UnitTesting.PrivateObject
class to assist in accessing internal and private APIs in your code. This is found in theMicrosoft.VisualStudio.QualityTools.UnitTestFramework.dll
assembly.Create a reflection framework that would be able to reflect off your code to access internal or private APIs.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With