471,616 Members | 1,743 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,616 software developers and data experts.

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 21049
"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
1 post views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
1 post views Thread by ZEDKYRIE | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.