469,282 Members | 2,078 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

get Type from string

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

Nov 13 '06 #1
3 20342
"Ruben" <Mr****@gmx.dewrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
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?
See System.Type.GetType(string typeName)

-cd
Nov 13 '06 #2
Carl Daniel [VC++ MVP] schrieb:
See System.Type.GetType(string typeName)
Thank you!

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

Nov 13 '06 #3
Ruben <Mr****@gmx.dewrote:
Carl Daniel [VC++ MVP] schrieb:
See System.Type.GetType(string typeName)

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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 13 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Jason Cartwright | last post: by
3 posts views Thread by Mike in Paradise | last post: by
reply views Thread by Chris Fink | last post: by
42 posts views Thread by blisspikle | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.