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

  • Thread starter Thread starter Allan Ebdrup
  • Start date Start date
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
 
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
 
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");
 
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
 
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
 
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.
 
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.
 
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.
 
Back
Top