Type.GetType case insensitive - WinRT
Do you know the assembly you're loading the types from? If so, you could just create a case-insensitive Dictionary<string, Type>
(using StringComparer.OrdinalIgnoreCase
) by calling Assembly.GetTypes()
once. Then you don't need to use Type.GetType()
at all - just consult the dictionary:
// You'd probably do this once and cache it, of course...
var typeMap = someAssembly.GetTypes()
.ToDictionary(t => t.FullName, t => t,
StringComparer.OrdinalIgnoreCase);
...
Type type;
if (typeMap.TryGetValue(name, out type))
{
...
}
else
{
// Type not found
}
EDIT: Having seen that these are all in the same namespace, you can easily filter that :
var typeMap = someAssembly.GetTypes()
.Where(t => t.Namespace == "Foo.Bar")
.ToDictionary(t => t.Name, t => t,
StringComparer.OrdinalIgnoreCase);
You can use GetTypes()
method, to fetch all possible types in the assembly where your type is in, after that check which type uppercase equals to your type upper case then use it in GetType
method.