c# - How to get all Forms and User Controls in WinForms project? -
i have winforms project, using visual studio 2013, dotnet framework 4.0. how can list of of created forms , user controls in project, preferably @ runtime?
edit: reply. example, want forms , user controls under namespace "myapp.forms" , "myapp.usercontrols", how can assemblies? here's how it:
ienumerable<assembly> currentdomainassemblies = appdomain.currentdomain.getassemblies().where(a => a.fullname.startswith("myappnamespace")); foreach (assembly in currentdomainassemblies) { ienumerable<type> assembliestypes = a.gettypes().where(t => (typeof(form).isassignablefrom(t) || typeof(usercontrol).isassignablefrom(t)) && t.isclass); foreach (type t in assembliestypes) { if (t.fullname.contains("usercontrol")) { listuc.beginupdate(); listuc.items.add(t.name); listuc.endupdate(); } if (t.fullname.contains("forms")) { listform.beginupdate(); listform.items.add(t.name); listform.endupdate(); }
edit: approach based on namespace of forms , user controls. posssible filename, not namespace?
if want see classes(forms or usercontrols) can use reflection. there no such thing project during runtime. can list based on assembly.
edit: try this
foreach (assembly in appdomain.currentdomain.getassemblies().where(a=>!a.fullname.startswith("system.") || !a.fullname.startswith("microsoft."))) { var types = a.gettypes().where(t => (typeof(form).isassignablefrom(t) || typeof(usercontrol).isassignablefrom(t) )&& t.isclass && t.fullname.startswith("yournamespace.")); }
Comments
Post a Comment