How can I parse C#-style generic type names of the format List<int>
or Dictionary<string,int>
or even more complex Dictionary<string,Dictionary<System.String,int[]>>
. Assume that these names are strings and may not actually represent existing types. It should just as easily be be able to parse BogusClass<A,B,Vector<C>>
. To be clear, I am NOT interested in parsing .NET internal type names of the format List`1[[System.Int32]]
, but actual C# type names as they would appear in the source code, with or without namespace qualifiers using dot notation.
Regular expressions are out because these are nested structures. I thought perhaps the System.CodeDom.CodeTypeReference constructor would parse it for me since it has string BaseType
and CodeTypeReferenceCollection TypeArguments
members, but those apparently need to be set manually.
CodeTypeReference is the kind of structure I need:
class TypeNameStructure
{
public string Name;
public TypeNameStructure[] GenericTypeArguments;
public bool IsGenericType{get;}
public bool IsArray{get;} //would be nice to detect this as well
public TypeNameStructure( string friendlyCSharpName )
{
//Parse friendlyCSharpName into name and generic type arguments recursively
}
}
Are there any existing classes in the framework to achieve this kind of type name parsing? If not, how would I go about parsing this?
Answering own question. I wrote the following class achieve the results I need; give it a spin.
If I run the following:
It correctly produces the following output, representing the TypeName as a string: