检查我的对象是否在C#中实现了通用接口

Pau*_*aul 3 c#

我有那个界面:

public interface IEntityWithTypedId<T>{}
Run Code Online (Sandbox Code Playgroud)

并有两个类:

public abstract class EntityWithTypedId<TId> : IEntityWithTypedId<TId>{...}
public abstract class Entity : EntityWithTypedId<int>{...}    
Run Code Online (Sandbox Code Playgroud)

所以我有这样的实体:

public class TestA : Entity
public class TestB : EntityWithTypedId<string>
public class TestC : EntityWithTypedId<byte>
Run Code Online (Sandbox Code Playgroud)

如何检查我的实体是否实现了IEntityWithTypedId?

谢谢

Jul*_*lia 5

typeof(TestA).GetInterfaces()
    .Any(i => i.GetGenericTypeDefinition() == typeof(IEntityWithTypedId<>))
Run Code Online (Sandbox Code Playgroud)