Using reflection to get the type of a class from a string contaning the name of the class

A

Allan Ebdrup

How do I get hold of the type when I have a string that represents the type.
For example I have the string "OFiR.Recruitment.Department"
And I want to get the type called "OFiR.Recruitment.Department"
From what I've found on the web I could get the assembly:
reflection.Assembly = reflection.Assembly.LoadFrom ("OFiR.Recruitment.dll")
And iterate all the classes in the assembly using .GetTypes () and searching
untill I find my type.
But what if I don't know what assembly the class is located in?
Is there some way to iterate all the assemblies in your references?

There must be a more efficient way to do this than iterating the assemblies
ant their GetTypes().

Kind Regards,
Allan Ebdrup, OFiR
 
S

Stefan L

Hi Allan,

maybe you should look at the method
public static Type.GetType (String typename)

Works well for System types like Int32 and Double. Should also work for
other types, that are located in your statically referenced assemblies
(online help says "all loaded assemblies"), but it will fail with
dynamic assemblies.

HTH,
Stefan
 
T

Tom Porterfield

Allan said:
How do I get hold of the type when I have a string that represents the
type. For example I have the string "OFiR.Recruitment.Department"
And I want to get the type called "OFiR.Recruitment.Department"
From what I've found on the web I could get the assembly:
reflection.Assembly = reflection.Assembly.LoadFrom
("OFiR.Recruitment.dll") And iterate all the classes in the assembly
using .GetTypes () and searching untill I find my type.
But what if I don't know what assembly the class is located in?
Is there some way to iterate all the assemblies in your references?

There must be a more efficient way to do this than iterating the
assemblies ant their GetTypes().

Have you tried:

Type myType = Type.GetType("OFiR.Recruitment.Department");
 
M

Marc Gravell

Note that this often needs the assembly-qualified name (such as
"System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089" for Int32)... it may be easier to just
enumerate the assemblies, and then use Assembly.GetType(string) to search
for the class (returns null if not found). You can use
AppDomain.CurrentDomain.GetAssemblies() to get the loaded assemblies.

As an illustration of Type.GetType() returning null:
System.Windows.Forms.Form f = new System.Windows.Forms.Form();
Type t = Type.GetType("System.Windows.Forms.Form"); // return null even
though loaded

Marc
 
A

Allan Ebdrup

Stefan L said:
Hi Allan,

maybe you should look at the method
public static Type.GetType (String typename)

Works well for System types like Int32 and Double. Should also work for
other types, that are located in your statically referenced assemblies
(online help says "all loaded assemblies"), but it will fail with dynamic
assemblies.

What are statically referenced assemblies? I have OFiR.Recruitment.dll in my
References on the website, but can't load "OFiR.Recruitment.Department"
that's in that assembly, using Type.GetType("OFiR.Recruitment.Department").

Kind Regards,
Allan Ebdrup, OFiR
 
B

Ben Voigt

In .NET, statically referenced assemblies aren't automatically loaded at
startup as with native DLLs. Types in assemblies that have been used, will
be found, regardless of whether loaded via references or Assembly.Load.
 
A

Allan Ebdrup

Marc Gravell said:
Note that this often needs the assembly-qualified name (such as
"System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089" for Int32)... it may be easier to just
enumerate the assemblies, and then use Assembly.GetType(string) to search
for the class (returns null if not found). You can use
AppDomain.CurrentDomain.GetAssemblies() to get the loaded assemblies.

Thanks.
This was the approach we ended using.
 
W

Walter Wang [MSFT]

Hi Allan,

Thank you for your post.

I also think the approach is reasonable. Please feel free to post here if
you need more help on this.


Regards,
Walter Wang
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

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