get Type from string

  • Thread starter Thread starter Ruben
  • Start date Start date
R

Ruben

Hi!

I am looking for an easy way to get a Type from a arbitrary string.
What I found looks like:

----
string TypeString = "System.String"; // only as example...
Type TypeIAmLookingFor = null;

Assembly[] appAssemblies =
System.AppDomain.CurrentDomain.GetAssemblies();

foreach (Assembly assembly in appAssemblies)
{
foreach (Type type in assembly.GetTypes())
{
if (t.ToString().Equals(TypeString))
TypeIAmLookingFor = type;
}
}
---

This is kind of overloaded; is there are more easy way to do this?

Thank you!
Ruben
 
Ruben said:
Hi!

I am looking for an easy way to get a Type from a arbitrary string.
What I found looks like:

----
string TypeString = "System.String"; // only as example...
Type TypeIAmLookingFor = null;

Assembly[] appAssemblies =
System.AppDomain.CurrentDomain.GetAssemblies();

foreach (Assembly assembly in appAssemblies)
{
foreach (Type type in assembly.GetTypes())
{
if (t.ToString().Equals(TypeString))
TypeIAmLookingFor = type;
}
}

See System.Type.GetType(string typeName)

-cd
 
Ruben said:
Thank you!

Sometimes it is hard to find the right place in the documentation... :-)

Note, however, that Type.GetType with just the "plain" type name (i.e.
without full assembly information) will only search in mscorlib and the
currently executing assembly. If you need to look in other assemblies,
either call GetType on the appropriate Assembly reference, or specify
the assembly details in the argument to Type.GetType.
 
Back
Top