Chcę oddzielić zmienne IEnumerable
według ich typów. Mój kod jest tak:jak uzyskać tylko typ liczby?
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
}
}
Gdy używam type.GetType().GetGenericTypeDefinition().Name
The listGenericType
jest jak List`1
lub HashSet`1
ale chcę go jak List
lub HashSet
. W związku z tym użyłem Substring
do rozwiązania tego problemu!
Czy istnieje sposób rozwiązania tego problemu bez postprocessingu string
? Mam na myśli coś w rodzaju poniższego kodu:
if (type is IEnumerable)
{
var listGenericType = type.GetType().GetGenericTypeDefinitionWithoutAnyNeedForPostProcessing();
if (listGenericType == "List") {
//do something
}
else if (listGenericType == "HashSet") {
//do something
}
}
https://msdn.microsoft.com/en-us/library/system.type.getgenerictypedefinition.aspx –