Solved

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

Posted on 2009-03-31
3
1,565 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
c# ftp code 3 56
C# DateTime Help 6 47
C# LINQ query question 11 33
XML XDocument extract - Keeping a node list like a menu path 8 34
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled

751 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