Home > Enterprise >  How can I check if a generic type implements an interface
How can I check if a generic type implements an interface

Time:11-05

Just to give you some context I'm writing some test classes to test the CRUD operations of my repository using .NET 6.0.

I'm working with two interfaces:

1- IEntity is an empty base interface for all my objects.

public interface IEntity { }

2- IIdentifiableEntity<T> that implements IEntity and lets me know that the object has an Id property.

public interface IIdentifiableEntity<T> : IEntity
{
    T Id { get; set; }
}

I have created two test classes, one for each type of interface:

1- TestIEntity to test objects implementing the IEntity interface.

[TestClass]
public class TestIEntity<T>
    where T : IEntity
{
    [TestMethod]
    public void Test() {
        // Test objects of type IEntity
        // Assert.IsNotNull(repository.GetAll());
    }
}

2- TestIIdentifiableEntity to test objects implementing the IIdentifiableEntity. (in my case I'm only interested in testing objects that have an Id of type long)

[TestClass]
public class TestIIdentifiableEntity<T>
    where T : IIdentifiableEntity<long>
{
    [TestMethod]
    public void Test() {
        // Test instances of type IIdentifiableEntity<long>
        // Assert.IsTrue(repository.GetId(1).Id > 0);
    }
}

I have created two base abstract test classes that create the corresponding test class and invoke the test methods. It looks something like this:

[TestClass]
public abstract class TestBase<T>
    where T : IEntity
{
    [TestMethod]
    public void TestIEntity()
    {
        TestIEntity() testClass = new TestIEntity();
        testClass.Test();        
    }
}

[TestClass]
public abstract class TestBaseId<T>
    where T : IIdentifiableEntity<long>
{
    [TestMethod]
    public void TestIIdentifiableEntity()
    {
        TestIIdentifiableEntity() testClass = new TestIIdentifiableEntity();
        testClass.Test();        
    }
}

These two test classes are already up and running, however, I would like to find a way to mix them together so that I can call the test methods from the same test class, after checking the type of the generic type. My idea is to get something like:

[TestClass]
public abstract class TestBase<T>
    where T : IEntity
{
    [TestMethod]
    public void TestClass()
    {
        if (typeof(T) is IIdentifiableEntity<long>)
        {
            TestIIdentifiableEntity() testClass = new TestIIdentifiableEntity();
            testClass.Test();  
        }
        else
        {
            TestIEntity() testClass = new TestIEntity();
            testClass.Test();   
        }
    }
}

I tried the above solution but the program doesn't execute what is inside the if statement.

My question is: is what I'm trying to do possible? If so how do I check that a generic type implements IIdentifiableEntity<long>?

I tried browsing some questions but none of them apply to my case.

Thank you in advance, any advice is appreciated.

CodePudding user response:

You can use Type.IsAssignableTo(Type)

typeof( T ).IsAssignableTo( typeof( IIdentifiableEntity<long> ) )
  • Related