是否可以查看对象是否继承了没有泛型类型参数的IDictionary?

Gue*_*lla 5 c#

我正在测试这样的对象:

if (item is IDictionary<object, object>)
Run Code Online (Sandbox Code Playgroud)

但这与所有其他类型的组合<sting, object>, <int, string>等都不匹配......

我只是想知道它是否已经实现了接口,无论它使用什么泛型类型.

我找到了一个例子说它有可能做类似的事情:

dictionary.GetType().GetInterfaces().Any(x => x.GetGenericTypeDefinition == typeof(IDictionary<>));
Run Code Online (Sandbox Code Playgroud)

但我仍然需要指定类型签名或它无效.

是否可以创建一个检查接口而不必指定类型的语句?

Ste*_*tty 7

你很接近,你真的需要修复语法:

dictionary.GetType().GetInterfaces().Any(x => x.GetGenericTypeDefinition() == typeof(IDictionary<,>))
Run Code Online (Sandbox Code Playgroud)

注意()后面的GetGenericTypeDefinition,以及里面的逗号<>.