简体   繁体   中英

Checking if my object implements a generic interface in C#

I have that interface :

public interface IEntityWithTypedId<T>{}

And have both classes :

public abstract class EntityWithTypedId<TId> : IEntityWithTypedId<TId>{...}
public abstract class Entity : EntityWithTypedId<int>{...}    

So I have entities like those:

public class TestA : Entity
public class TestB : EntityWithTypedId<string>
public class TestC : EntityWithTypedId<byte>

How can I check if my entities implements IEntityWithTypedId ?

Thanks

typeof(TestA).GetInterfaces()
    .Any(i => i.GetGenericTypeDefinition() == typeof(IEntityWithTypedId<>))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM