dimanche 25 février 2018

How to get type from different namespace than System in c#?

I would like to create types when being given only strings of their names. Here it's obvious:

Type t = System.Type.GetType("System.Double");

But when I try to get type from another namespace, like System.Drawing, the above method won't return the correct type. Working solution I've found:

Assembly foundAssembly = AppDomain.CurrentDomain.GetAssemblies().SingleOrDefault(assembly => assembly.GetName().Name == "System.Drawing");
Type t = foundAssembly.GetType("System.Drawing.Color");

However, it looks pure and I guess doing it costs some time (AppDomain.CurrentDomain has 22 assemblies in my case, but multiplied by 10000, it's something). So can we get it faster? I'm not searching for a solution like type = typeof(System.Drawing.Color);, because possibly I'll have to translate "System.Text.StringBuilder" to its type and so on...





Aucun commentaire:

Enregistrer un commentaire