Is there an easy way without writing a recursive method which will give a \'user friendly\' name for a generic type from the Type class?
E.g. For the fo
Reflection - Getting the generic parameters from a System.Type instance
You can also use reflection on generic types:
var dict = new Dictionary();
Type type = dict.GetType();
Console.WriteLine("Type arguments:");
foreach (Type arg in type.GetGenericArguments())
{
Console.WriteLine(" {0}", arg);
}
You can then put it into some extension method for object and use it anywhere you need. I would also like to add that every recursion can be written as imperative code.
So the whole code will look like:
static void GetGenericParametersNames(Type type)
{
Queue typeQueue = new Queue();
typeQueue.Enqueue(type);
while (typeQueue.Any())
{
var t = typeQueue.Dequeue();
Console.WriteLine(" {0}", arg);
foreach (Type arg in t.GetGenericArguments())
{
typeQueue.Enqueue(t);
}
}
}