0

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...

YogoWafel
  • 101
  • 2
  • 9
  • 2
    *"but multiplied by 10000, it's something"* Why would you load 10.000 assemblies? What are you trying to do? – Manfred Radlwimmer Feb 25 '18 at 20:14
  • @ManfredRadlwimmer not **loading** that many assemblies, **searching** for that many times. In my project I'll have to do such parse from string to type really often. Possibly, the number of assemblies could grow. I don't understand downvoting my post btw – YogoWafel Feb 25 '18 at 20:21
  • Quoting from [MSDN](https://msdn.microsoft.com/en-us/library/w3f99sx1(v=vs.110).aspx), for the `typeName` parameter of `Type.GetType(string typeName)`: _The assembly-qualified name of the type to get. See AssemblyQualifiedName. If the type is in the currently executing assembly or in Mscorlib`.dll, it is sufficient to supply the type name qualified by its namespace._ – Gian Paolo Feb 25 '18 at 20:23
  • @YogoWafel *"I don't understand downvoting my post btw"* Me neither, I think it's a valid question - maybe because the solution wasn't hard to google. – Manfred Radlwimmer Feb 25 '18 at 20:24

2 Answers2

2

If you want to make this work, you'll have to use the fully qualified type name (including the assembly). For System.Drawing.Color that would be (for .Net 4.0):

System.Drawing.Color, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
Type t = Type.GetType("System.Drawing.Color, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");

to get the fully qualified name of an already loaded type, use

t.AssemblyQualifiedName
Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62
  • [Note that assemblies and namespaces are not the same](https://stackoverflow.com/questions/21530460/namespace-or-assembly). In the asker's example, it so happens that `System.Drawing` is both the assembly and namespace of `Color`, but this may not always be the case. – Joe Sewell Feb 25 '18 at 20:29
  • @JoeSewell True - it would be best to save then fully qualified to begin with (however they are stored or processed). – Manfred Radlwimmer Feb 25 '18 at 20:30
2

While the accepted solution properly answers the question, I would also recommend caching any of these conversions into a <string, Type> dictionary, where the string is the input you are parsing, and the Type is the result of the lookup. This should significantly improve your performance since reflection is slow.

Null511
  • 418
  • 6
  • 7