Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2009-03-31
3
Medium Priority
?
1,573 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 2000 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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

721 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