Mega Code Archive

 
Categories / C# / Reflection
 

Recursively gets all generic type params in any given type definition

using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; using System.Text; namespace RestCake.Util {     internal static class ReflectionHelper     {         /// <summary>         /// Recursively gets all generic type params in any given type definition.         /// So if you had a List{Dictionary{Dictionary{string, int}, Dictionary{string, int}}}, it would get all of the type params.         /// </summary>         /// <param name="type"></param>         /// <param name="list"></param>         /// <returns></returns>         public static Type[] GetAllTypeParams(Type type, List<Type> list)         {             if (list == null)                 list = new List<Type>();             if (type.IsGenericType)             {                 Type[] typeParams = type.GetGenericArguments();                 list.AddRange(typeParams);                 // Recursively process all types                 foreach (Type t in typeParams)                     GetAllTypeParams(t, list);             }             return list.ToArray();         }     } }