How to get the type for a class by sending just the name of the class instead of the class itself as the parameter?

StackOverflow https://stackoverflow.com/questions/1411372

  •  05-07-2019
  •  | 
  •  

Question

Dim a as Type=GetType(className) would gimme the type. But I have only the name of the class as string. I want something like GetType("class1") which would return the type.

Was it helpful?

Solution

Type.GetType("class1")

OTHER TIPS

Both Type.GetType(...) and Assembly.GetType(...) expects a fully qualified type name. Thus, only passing in the class name without its namespace will not yield the Type.

If you make sure to include the namespace like this:

Type.GetType("Fully.Qualified.Namespace.class1")

will yield the same result as GetType(class1).

Update: if you don't know the namespace of your class, you could do a search (using Linq mind you) on types in the current assembly:

GetType().Assembly.GetTypes().First(type => type.Name == "AssemblyModuleTests")

I assume this is a slower operation than looking up types using fully qualified names since GetTypes() enumerates all types in the assembly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top