How could I get a list of all user-defined controls that were added to current namespace? I am using C# 2010.
Asked
Active
Viewed 775 times
0
-
1Have you tried anything? Read about reflection? – Gilad Green Aug 06 '17 at 08:28
-
Yes I tried. Controls are add to namespace and the namespace is not an object. I could not extract list of user-defined controls from namespace. – Farzin Aug 06 '17 at 08:32
2 Answers
0
You can use reflection. Try this code:
public static class ControlsFinder
{
public static List<Type> FindControls(Assembly controlsLibrary,
string rootNamespace, bool includeNestedTypes = true)
{
var parent = typeof(UserControl);
return controlsLibrary.GetTypes()
.Where(t => (includeNestedTypes
? t.FullName.StartsWith(rootNamespace)
: t.FullName.Equals($"{rootNamespace}.{t.Name}"))
&& parent.IsAssignableFrom(t))
.ToList();
}
}
Usage example:
var controls = ControlsFinder.FindControls(Assembly.GetExecutingAssembly(), "WinFrm");
If you only want names you can select them from controls
:
var names = controls.Select(t => t.Name).ToArray();

Aleks Andreev
- 7,016
- 8
- 29
- 37
-
I don't know where is wrong. I made a user-defined control and added it's project to my main app project's reference. Now in no way I can get the name of my user-defined control that were added to my main app project. This code return controls with 0 member too. – Farzin Aug 06 '17 at 08:54
-
I've updated answer. Be sure to pass correct assembly into `FindControls`. I've used `GetExecutingAssembly`, but it is just an example – Aleks Andreev Aug 06 '17 at 09:14
-
Thanks for your help but it does not work. How could I upload my simplified project? – Farzin Aug 06 '17 at 12:17
-
@Farzin you can use [gihub](https://github.com/) or [bibucket](https://bitbucket.org/). Or any other source control systems – Aleks Andreev Aug 06 '17 at 13:59
-
-
0
With these method i can extract list of all user-defined controls in my project and create an instance of the User-defined Control by it's name on my form.
var assemblies = AppDomain.CurrentDomain.GetAssemblies(); // Get my CurrentDomain Object
Assembly myType = Assembly.GetExecutingAssembly(); // Extract list of all references in my project
foreach (var assembly in assemblies) // Search for the library that contains namespace that have needed controls
{
if (assembly.GetName().ToString().ToUpper().IndexOf("FIBACONTROLS") > -1)
{
myType = assembly; // Get All types in the library
List<Type> myTps = myType.GetTypes().ToList();
Type mT = null;
foreach (Type selType in myTps) // Find the type that refer to needed user-defined control
{
if (selType.Name.ToUpper() == "FIBACOLORPICKER")
{
mT = selType;
break;
}
}
if (mT == null)
return;
object myInstance = Activator.CreateInstance(mT); // Created an instance on the type
Control mFib = (Control)myInstance; // create the control's object
mFib.Name = "Hahah"; // add the control to my form
mFib.Left = 100;
mFib.Top = 200;
mFib.Visible = true;
this.Controls.Add(mFib);
break;
}
}
I try add some comment to code to describe it.
It work and sure there are some better way to do it, but I'm new one in C# and I am sure that the solution that I found is not the best.

Farzin
- 11
- 5