1

I have the ISymbol object for an enumerable collection and need to get the underlying type.

e.g.

List<int> intList;

I have the ISymbol for intList, and need to find the underlying type - in this case, it is int.

I tried using the code listed here, but seems like reflection does not work here.

Here's my code snippet:

private Type GetUnderlyingTypeFromEnumerable(ISymbol symbol)
{  
    Type eType = null;  
    Type[] interfaces = symbol.GetType().GetInterfaces();  
    foreach (Type i in interfaces)  
        if (i.IsGenericType && i.GetGenericTypeDefinition().Equals(typeof(IEnumerable<>)))  
        {  
            eType = i.GetGenericArguments()[0];  
            break;  
        }  

    return eType;  
}
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
user3009098
  • 93
  • 1
  • 6

1 Answers1

2

Thanks everyone. I pried around the INamedTypeSymbol interface and found an easy way to solve this:

    private ITypeSymbol GetUnderlyingTypeFromEnumerable(INamedTypeSymbol symbol)
    {
        return symbol.TypeArguments.First();
    }
user3009098
  • 93
  • 1
  • 6