This project is read-only.


Create mechanism to recognize nested types from symbols


If you have a type as a symbol, I can find no reasonable way to determine if the type is nested.

I anticipate that "ContainingType" would have the parent-of-nested and be null in all other cases. This is not how that property is used - specifically, type parameters return the containing symbol in this property.

The only workaround I have discovered relies on the name of the type symbol to exclude type parameters. For example
    if (!symbol.GetType().Name.Contains("TypeParameter"))
        if (symbol.ContainingType != null)
        { nestedType = symbol.ContainingType; }
There are many serious problems with this approach, a few are
  • Depends on an implementation detail that is not otherwise exposed (type name)
  • Because nesting is not the intent of this field, there may be other scenarios that must be excluded
  • It's slow and it's a hack
Please provide some mechanism for determining if a type is nested. If there's a better workaround, that may be sufficient.



erikkallen wrote Oct 5, 2014 at 6:52 PM

Type parameters are actually owned by a type (or method) so IMO there's nothing strange with their owner type being returned in the ContainingType property.

If you only want nested types of class, struct, enum or interface types, use the TypeKind property of INamedTypeSymbol.

KathleenDollard wrote Oct 6, 2014 at 5:54 PM

Actually, it's the reverse I need.

Given a symbol, is it contained in another type? If so, what type.

I still don't have a way to do this, except the hack of excluding other options.


nmgafter wrote Oct 27, 2014 at 7:19 PM

type.ContainingType != null works.

Type parameters (of a generic type) are contained by another type. If those are not of interest to you, exclude them from your result with another test.