vendredi 9 janvier 2015

Find generic type over reflection

I have a small snipped of code, which checks, wehter an class exists or not.


At first i load all available types:



List<Type> types = new List<Type>();
foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
{
try
{
types.AddRange(asm.GetTypes());
}
catch (ReflectionTypeLoadException e)
{
types.AddRange(e.Types.Where(t => t != null));
}
}


Than i concat namespace and class name (which should be checked):



string fullName = ns.Trim() + "." + classToProof.Trim();


And in the and, i check wether the class exists:



int found = types.Where(innerItem => innerItem.FullName == fullName).ToList().Count;


But i have the problem, that if i check generic classes, for example System.Collections.Generic.Dictionary, found is always 0 (should be 1).


Does anyone has an idea, why this happens?


Thank you






Aucun commentaire:

Enregistrer un commentaire