I want to highlight IEnumerable variables by their types. My code is as follows:
if (type is IEnumerable) { var listGenericType = type.GetType().GetGenericTypeDefinition().Name; listGenericType = listGenericType.Substring(0, listGenericType.IndexOf('`')); if (listGenericType == "List") { //do something } else if (listGenericType == "HashSet") { //do something } }
When I use type.GetType().GetGenericTypeDefinition().Name , listGenericType is similar to List`1 or HashSet`1 , but I want it to be like List or HashSet . So I used Substring to solve this problem!
Is there a way to handle this problem without the postProcessing string type? I mean something like the code below:
if (type is IEnumerable) { var listGenericType = type.GetType().GetGenericTypeDefinitionWithoutAnyNeedForPostProcessing(); if (listGenericType == "List") { //do something } else if (listGenericType == "HashSet") { //do something } }
source share