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 | | | | re: Using reflection to get the type of a class from a string contaning the name of the class
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:[color=blue]
> 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
>
>
>[/color] | | | | re: Using reflection to get the type of a class from a string contaning the name of the class
Allan Ebdrup wrote:[color=blue]
> 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().[/color]
Have you tried:
Type myType = Type.GetType("OFiR.Recruitment.Department");
--
Tom Porterfield | | | | re: Using reflection to get the type of a class from a string contaning the name of the class
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 | | | | re: Using reflection to get the type of a class from a string contaning the name of the class
"Stefan L" <slueck@pp-software.REMOVEIfNoSpam.de> wrote in message
news:OialiZ6kGHA.1272@TK2MSFTNGP03.phx.gbl...[color=blue]
> 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.[/color]
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 | | | | re: Using reflection to get the type of a class from a string contaning the name of the class
"Allan Ebdrup" <ebdrup@noemail.noemail> wrote in message
news:OPULZt6kGHA.4660@TK2MSFTNGP05.phx.gbl...[color=blue]
> "Stefan L" <slueck@pp-software.REMOVEIfNoSpam.de> wrote in message
> news:OialiZ6kGHA.1272@TK2MSFTNGP03.phx.gbl...[color=green]
>> 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[/color][/color]
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.
[color=blue][color=green]
>> assemblies.[/color]
>
> 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
>[/color] | | | | re: Using reflection to get the type of a class from a string contaning the name of the class
"Marc Gravell" <marc.gravell@gmail.com> wrote in message
news:u$2cDi6kGHA.4596@TK2MSFTNGP02.phx.gbl...[color=blue]
> 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.[/color]
Thanks.
This was the approach we ended using. | | | | re: Using reflection to get the type of a class from a string contaning the name of the class
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. |  | Similar C# / C Sharp bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,510 network members.
|