给定泛型类定义BaseClass<T>,是否可以测试这种情况?
if (myvar is BaseClass<>)
我想知道myvar是否来自泛型类,但我不知道(或关心)T可能是什么.
我认为答案是否定的......我的备份计划是通过这个洞穴myvar.GetType().ToString().Contains("BaseClass<")或其他东西.
像这样:
if (myVar.GetType().IsGenericType
&& myVar.GetType().GetGenericTypeDefinition() == typeof(BaseClass<>))
Run Code Online (Sandbox Code Playgroud)