winforms - C# Get form names of project A from Project B -
i have 2 projects in 1 solution, project a
, project b
(using vs2010 ultimate , c# windows application
). project b
acts user management application project a
. in project b
have form contains chekcedlistbox
control list project a
forms names , texts (this form let system administrator grant users forms allowed view/edit based on security groups) code:
private void getformnames() { foreach (assembly in appdomain.currentdomain.getassemblies()) { foreach (type t in a.gettypes()) { if (t.basetype == typeof(form)) { var emptyctor = t.getconstructor(type.emptytypes); if (emptyctor != null) { var f = (form)emptyctor.invoke(new object[] { }); string formtext = f.text; string formname = f.name; checkedlistbox1.items.add("" + formtext + "//" + formname + ""); } } } } }
the result getting form names of current project (b) , empty lines(//) , select window//mdiwindowdialog
, printpreview
.
i'm going assume you've referenced projecta
correctly , forms you're interested in have public
parameterless constructor. problem caused projecta
not being loaded yet, can fix in multiple ways. direct use static assembly.load
(as long files in same directory, if not gets more complicated).
try { assembly projecta = assembly.load("projecta"); // replace actual projecta name // despite microsoft's dire warnings loading simple name, // should fine here long don't have multiple versions of projecta // floating around foreach (type t in projecta.gettypes()) { if (t.basetype == typeof(form)) { var emptyctor = t.getconstructor(type.emptytypes); if (emptyctor != null) { var f = (form)emptyctor.invoke(new object[] { }); // t.fullname distinguish unwanted entries , // possibly later ignore them string formitem = t.fullname + " // " + f.text + " // " + f.name; checkedlistbox1.items.add(formitem); } } } } catch(exception err) { // log exception }
another (probably cleaner solution) have forms you're interested in inherit single base form. load assembly known type
, check each enumerated type
inherits before adding list. more extensive change, however, , touches projecta
.
Comments
Post a Comment