How to find namespace of class by its name using reflection in .net core?
asp.net-core, c#, reflection
Solution
If you know that you'll never have multiple types with the same name residing in the different namespaces, you can just iterate over all types in the assembly and filter on type name. For example, this works:
var typenames = new[] { "String", "Object", "Int32" };
var types = typeof(object).GetTypeInfo().Assembly
.GetTypes()
.Where(type => typenames.Contains(type.Name))
.ToArray(); // A Type[] containing System.String, System.Object and System.Int32
This won't necessarily break if you have multiple types with the same name, but you'll get all of them in the list.
Problem
I have a list of string with only classes names. I need to create instances of them using Activator, but all of them can be in diffrent namespaces. Classes can be move into another namespace in the future so i can't hardcode it.