如何确定属性是否为DNX Core 5.0中的通用类型?

Sta*_*ams 4 .net c# dnx asp.net-core

以前我可以使用它来获取类的泛型类型;

typeof(MyClass).GetTypeInfo().DeclaredProperties.Any(p => p.PropertyType.IsGenericType)
Run Code Online (Sandbox Code Playgroud)

但是,在DNX Core 5.0中,IsGenericType不受支持.我现在可以用什么?

小智 8

刚刚查看了一些源代码,确认框架中仍然存在IsGenericType属性.

https://github.com/aspnet/Common/blob/dev/src/Microsoft.Framework.ClosedGenericMatcher.Sources/ClosedGenericMatcher.cs#L44

以下工作如何?

typeof(MyClass).GetTypeInfo().DeclaredProperties.Any(p => p.PropertyType.GetTypeInfo().IsGenericType)
Run Code Online (Sandbox Code Playgroud)

  • 嘿欢呼,只是根据你的例子将`GetTypeInfo()`添加到`PropertyType`并编译. (2认同)