473,408 Members | 1,743 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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

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

Jun 19 '06 #1
7 2005
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 Ebdrup schrieb:
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

Jun 19 '06 #2
Allan Ebdrup wrote:
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");
--
Tom Porterfield
Jun 19 '06 #3
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
Jun 19 '06 #4
"Stefan L" <sl****@pp-software.REMOVEIfNoSpam.de> wrote in message
news:Oi**************@TK2MSFTNGP03.phx.gbl...
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
Jun 19 '06 #5

"Allan Ebdrup" <eb****@noemail.noemail> wrote in message
news:OP**************@TK2MSFTNGP05.phx.gbl...
"Stefan L" <sl****@pp-software.REMOVEIfNoSpam.de> wrote in message
news:Oi**************@TK2MSFTNGP03.phx.gbl...
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 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.
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

Jun 19 '06 #6
"Marc Gravell" <ma**********@gmail.com> wrote in message
news:u$**************@TK2MSFTNGP02.phx.gbl...
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.
Jun 20 '06 #7
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.

Jun 20 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: samlee | last post by:
Hi All, I'm learning how to write C# using reflection, but don't know how to code using reflection this.Controls.Add(this.label1); Could anyone help, Thank in advance. ...
3
by: J E E | last post by:
Hi! Is it possible to access fields in a derived class using reflection? Code below works fine when I access it as a private member in the Page class, but not when accessing base class member...
13
by: Don | last post by:
How do I get an Enum's type using only the Enum name? e.g. Dim enumType as System.Type Dim enumName as String = "MyEnum" enumType = ???(enumName)
5
by: Anders Borum | last post by:
Hello! Whilst refactoring an application, I was looking at optimizing a ModelFactory with generics. Unfortunately, the business objects created by the ModelFactory doesn't provide public...
2
by: Luis Arvayo | last post by:
Hi, In c#, I need to dynamically create types at runtime that will consist of the following: - inherits from a given interface - will have a constructor with an int argument
0
by: Rob | last post by:
Hello, I'm new to C# and have a question regarding factoring through reflection. I have a database filled with data represented by name and value pairs. Each name has a identically named...
2
by: diego | last post by:
hi everyone, i have a sub that opens a form given the form's name as string and opens it using System.Reflection. How can I set the form's properties at runtime. Here is my code. Public Sub...
13
by: jerryau | last post by:
Hi, I am trying to dynamically create object instances based on a string class name, and then I need to dynamically set values to these objects, but I have no idea how to do this in C#. Here is...
1
by: learning | last post by:
Hi how can I instaltiate a class and call its method. the class has non default constructor. all examples i see only with class of defatul constructor. I am trying to pull the unit test out from...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.