get Type from string

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
 
C

Carl Daniel [VC++ MVP]

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
 
J

Jon Skeet [C# MVP]

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top