views:

24

answers:

1

I have a custom DDL that lives in my Server control library.

Currently I add this control to a table by strongly typing the name of the control, the customary way.

Control_Library.Report_NumberDDL newDDL = new Control_Library.Report_NumberDDL();

What I want to be able to do is dynamically create that control by pulling the control name from a table.

So I would have the name of the control in my code, in this case "Report_NumberDDL", and I would like to then create the control without having to strongly type it.

Something like this, though I know this doesn't work:

string controlName = "Report_NumberDDL";

Control_Library."controlName" controlNum1 = new Control_Library."controlName"();

SO since that obviously doesn't work can somebody help me with what would work?

Thanks

Edit:

I tried to do this:

    Type type = Type.GetType("Control_Library.Report_NumberDDL");
    object control = Activator.CreateInstance(type);

But on the CreateInstance(type) I get a null value exception. So the Type isn't getting created correctly.

A: 

You'll need to use reflection to dynamically instantiate objects. Import the System.Reflection namespace, then do something like this:

Type type = Type.GetType(ControlName); 
object control = Activator.CreateInstance(type); 
Don
Ok I'm trying to work through this but having an issue here is what I did: Type type = Type.GetType("One_Eva_Control_Library.Emu_AIR_Report_NumberDDL"); object control = Activator.CreateInstance(type);Now I get a null type exception so I'm not sure what I'm doing wrong there.
Collin Estes
Make sure that your One_Eva_Control_Library assembly is loaded and available. Then try Type type = Type.GetType("Emu_AIR_Report_NumberDDL");
Don
When you say loaded and available does that simply mean adding the 'using One_Eva_Control_Library;' to my aspx.cs file? Or is there something else need to make sure the assembly is loaded and available.
Collin Estes
I found this, too: http://blogs.msdn.com/haibo_luo/archive/2005/08/21/454213.aspx If you use typeof(One_Eva_Control_Library.Emu_AIR_Report_NumberDDL).AssemblyQualifiedName, you should get the string to use for GetType().
Don
I just mean that the assembly is referenced in your project and that you have a using statement for it at the top of your class file. I'm looking at code I use to load types for a plug-in system, and I load the assemblies through reflection as well, using Assembly.LoadFile(). I think getting that AssemblyQualifiedName is going to be your best bet.
Don