Solved

Efficient way to load user control from dll during runtime in c# csharp .net

Posted on 2009-03-31
3
1,551 Views
Last Modified: 2013-12-17
I have created a user control library of composite user controls, each with a few simple buttons.

During runtime, I am able to load and unload these controls. I expect to have hundreds of these controls, and depending on user selections a different user control will be loaded.

The thing is, how do I do this without creating a giant IF-ELSE statement? I thought about storing some info in a database, but I'm not sure what info to put in the DB that I can use to actually instantiate the user control in the dll.

In other words, I know I can: myCustomUserControl X = new myCustomUserControl(), and add via to MyPanel.Controls.Add(X) ..... but, how can I do the same thing using a database that will have some kind of reference to the myCustomUserControl in the DLL?

This way, I could do a DB lookup, and then a load, instead of a huge Switch or If-Else...

can anyone help??



0
Comment
Question by:yaronusa
  • 2
3 Comments
 
LVL 3

Accepted Solution

by:
WeiXi earned 500 total points
ID: 24030073
You can dynamically construct your user-controls during runtime by help of the System.Type and System.Reflection.ConstructorInfo classes. You have to know the assembly-qualified name of the control you want to load, though:
http://msdn.microsoft.com/en-us/library/system.type.assemblyqualifiedname.aspx

You can determine the fully-qualified name of the control via (using your control X from the question):
X.GetType().AssemblyQualifiedName

The first part of the fully-qualified name is the control itself, the second part the assembly. As an example, for the built-in Label it looks something like this:
"System.Windows.Forms.Label, System.Windows.Forms, Version=2.0.0.0, Culture=neutral"

So once, you figured out the full assembly name, you just have to change the Control-name in the first part for getting different Controls. All you need to store in your database is the name of the controls and the fully-qualified assembly names. With these you can construct a string to pass to Type.GetType().

The attached code snippet constructs a built.in Label object in my environment. I hope this helps.
System.Type t = Type.GetType("System.Windows.Forms.Label, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");

System.Reflection.ConstructorInfo ci = t.GetConstructor(Type.EmptyTypes);

object o = ci.Invoke(null);

Control ctrl = o as Control;

if (ctrl != null)

    MyPanel.Controls.Add(ctrl);

Open in new window

0
 

Author Comment

by:yaronusa
ID: 24031148
Holy Crap It Worked!

I must say your answer was the *very* comprehensive. Thanks-a-million!
0
 

Author Closing Comment

by:yaronusa
ID: 31564832
Holy Crap It Worked!

I must say your answer was the *very* comprehensive. Thanks-a-million!
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now