c# - 检查我的对象是否在 C# 中实现通用接口(interface)

标签 c#

我有那个界面:

public interface IEntityWithTypedId<T>{}

并且有两个类:

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

所以我有这样的实体:

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

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

谢谢

最佳答案

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

关于c# - 检查我的对象是否在 C# 中实现通用接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22963018/

相关文章:

c# - 将 Array 与多个读取器线程 + 写入器一起使用

c# - 程序未按预期运行

c# - C#如何获取图片框上的绘图图形

c# - 如何在运行时将方法附加到动态创建的 C# 类型?

c# - 从字符串创建一致的 UUID 或唯一标识符

c# - Windows Server 2008R2 IIS Appcrash - System.StackOverflowException

序列化为 XML 时 C# 控制元素名称

c# - UWP - 将标题行添加到我的 gridview

c# - 我应该为我的代码使用什么类型的数据库?

c# - 使用 RhinoMocks 测试私有(private)方法