开发者

Selecting and building a Control from a control library dynamically, using the control name from a table

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 wi开发者_运维技巧thout 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.


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); 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜