Click here to Skip to main content
15,890,282 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I need a way to pass a type to a generic method based on a string type name.

For example, say I have a method with the following signature:

public Save<T>(Model) where T : BaseConfig, new(){}

The client will need to call this method dynamically based on the selection in a current dropdown.

For example, "Client Account" might be user-selected dropdown text with a dropdown value of "ClientAccount".

Based on this selection ClientAccountConfig should be the type passed as T into the save method.

So I need to be able to implement something like this:

Type configType = typeof(ddlModule.SelectedValue.ToString() + "Config")

Obviously the typeof above will return a string but I am trying to get a Type object described by the dynamic string that I'm building, in this case a Type object of ClientAccountConfig.

Do you know how I can accomplish this?

[edit]Code blocks added, < and > converted to HTML-Safe versions.
Please ensure "escape HTML characters" if set when pasting, or use the "<" and ">" characters from the effects list above the text entry box. Otherwise they get swallowed by the HTML when output to the browser - this can reduce or remove the sense of your post! - OriginalGriff[/edit]
Posted
Updated 4-Feb-11 9:16am
v2

1 solution

Are you looking for something like this:

If the Type you are trying to get is in the currently executing assembly or Mscorlib.dll, then all you need is the dynamically created type name, qualified by the namespace...
C#
Type configType = Type.GetType("Namespace.typeName")

If not, then you will need to qualify the type with the assembly it is in...
C#
Type configType = Type.GetType("Namespace.typeName, ExampleAssembly, Version=1.0.0.0, Culture=en, PublicKeyToken=a5d015c7d5a0b012");
 
Share this answer
 
v4
Comments
Sergey Alexandrovich Kryukov 4-Feb-11 17:44pm    
Must be assembly-qualified type name of course.
See http://msdn.microsoft.com/en-us/library/w3f99sx1.aspx.

--SA
JOAT-MON 4-Feb-11 17:52pm    
Good point, if it is not in the currently executing assembly. I will modify my answer to address this.
Sergey Alexandrovich Kryukov 4-Feb-11 18:08pm    
Thanks for understanding. I up-voted to 5.
--SA
Sergey Alexandrovich Kryukov 4-Feb-11 18:12pm    
Oh, no, sorry, still not correct.

Assembly name and name space are absolutely different things.
What you need is name space.
If you have a Type, you can see it through Type.FullName.
Assembly name is a complex thing which include signature hash and version, etc.
Third thing is the name of the assembly module(s) -- also very different from the two above.

Seriously, fix that, otherwise it's too confusing...
--SA
JOAT-MON 4-Feb-11 18:42pm    
Fixed.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900