Solved

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

Posted on 2009-03-31
3
1,561 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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

856 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