Unit Test Comilation Error when using generics with constraints RRS feed

  • Question

  • Using Visual Studio 2010 Ultimate, I have a class which is something like this:

    public class MyGenericClass<T> : IList<T> where T : class, IMyInterface

    I have not even created a unit test for this class, yet, though I have created unit tests for other other non-generic classes in this project.

    When I try to compile the unit test, I get the following error:

    C:\Program Files\MSBuild\VisualStudio\v10.0\TeamTest\Microsoft.TeamTest.targets(14,5): error: GenericArguments[0], 'T' on 'MyNamespace.MyGenericClass`1[T]' violates the constraint of type parameter 'T'

    The project being tested does compile succesfully. It is the unit test project compilation which is failing.

    The unit test project had previously compiled and ran successfully until I added this generic class with constraints. I've tried cleaning, rebuilding, signing and making internals visible... nothing has worked.

    Does anyone know of a patch/workaround/known issue for this?

    The only one I found was for VS2008 and it was closed as "won't fix"

    Wednesday, February 16, 2011 3:00 PM

All replies

  • Hi Leonard,

    Thank you for posting in our MSDN forum.


    From your description, you want to create unit tests for non-generic classes, but there are some generic classes with them in the same namespace. When you try to run the unit test, you get an error.


    We will be working together to solve this question, so please feel free to let me know if I have misunderstood anything.


    From the error, I think you can have a try at the follow steps:


    1.      You can run the class. We can know whether the class can run rightly.


    2.      Because you have used a generic class to inherit another generic class and general class, when you achieve some methods in the no-generic class, it is connected to the generic class.

    3.      About the generic class, you have use class MyGenericClass<T>to inherit IList<T>,  the type parameter ‘T’ in class MyGenericClass<T> is restrained by the ‘T’ in class IList<T>. So when you use a method in a general class to initialize the generic class, it’s easy to create mistakes.


    I would like to provide an article below regarding unit tests with generic methods for more information:




    I hope it could help you. If anything is unclear, please free feel to let me know.


    Best Regards,


    When you help others, it's help yourself!
    Friday, March 4, 2011 5:58 AM
  • I found a workaround for this problem. Since this error appears only if you have the interface and the implementation defined in different projects of the solution, you can create a redundant abstract class which implements the interface (in the same project where the interface is defined) and your class will not implement directly the interface anymore but will inherit the abstract class.
    Thursday, May 19, 2011 12:59 PM
  • None of the comments here helps me in any way.

    I've removed the definition of the class and every reference to the class being complained about everywhere in the project. 

    To be clear, the dll project no longer defines the class in question and the test project contains no references to the class yet I get the exact same error the OP reported.

    Originally my class was defined as

    public abstract class ImplicitByteWrapper<T> : 
            IComparable<ImplicitByteWrapper<T>>         where T : ImplicitByteWrapper<T>

    I have many other classes defined in exactly this same way and have no problem with them.

    Wednesday, May 8, 2013 3:25 PM