泛型类型的平等

问题描述 投票:2回答:1

我想比较两种类型。我有class Foo<Bar>,但我不知道我必须知道的通用参数是否是一个Foo对象。以下代码不起作用,我不知道为什么。

foreach (var type in types.Where(t => t.BaseType == typeof(Foo<>))
{
    use(type);
}
c# system.reflection
1个回答
2
投票

我找到了答案。有一个名为type.GetGenericTypeDefinition()的方法,我现在可以将我的对象与此方法返回类型进行比较。

© www.soinside.com 2019 - 2024. All rights reserved.