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?
Solution:
List<string> types = new List<string>(); foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies()) { try { types.AddRange(asm.GetTypes().Select(item => (!item.FullName.Contains("`") ? item.FullName : item.FullName.Substring(0, item.FullName.IndexOf("`"))))); } catch (ReflectionTypeLoadException e) { types.AddRange(e.Types.Where(t => t != null).Select(item => (!item.FullName.Contains("`") ? item.FullName : item.FullName.Substring(0, item.FullName.IndexOf("`"))))); } } I removed all ` from the full name, and fill a prepared list of strings.
Thank you