Solved

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

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

773 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