Well, I don't have time to write and test the program for you, but I
can point you toward the right classes and methods to use.
If you look at the Assembly class, you'll see that you can use the
GetTypes() method to get all of the types defined in the assembly. If
you need to look in assemblies that are called by your main program,
you can use GetExecutingAssembly to get the assembly for the executing
program, then GetReferencedAssemblies to follow the tree of referenced
assemblies from there.
For any particular assembly, as I mentioned you can use GetTypes() to
get the types defined in the assembly. For each type, you can do this:
foreach (assemblyType in assembly.GetTypes())
{
if (assemblyType.IsSubclassOf(typeof(System.Windows.F orms.Form)))
{
// You may need to use assemblyType.FullName here... I don't
know for sure.
System.Windows.Forms.Form f =
(System.Windows.Forms.Form)assembly.CreateInstance (assemblyType.Name,
false, System.Reflection.BindingFlags.Public |
System.Reflection.BindingFlags.NonPublic, null, null, null, null);
foreach (Control c in f.Controls)
{
... do something with control c of form f ...
}
f.Dispose();
}
}
Toze wrote:
Give me a sample to do that,
list of classes defined by an assembly using
Reflection,to instantiate each one using "new" and look at its Controls
collection
"Bruce Wood" wrote:
Toze wrote:
I'm using a assembly to load my apllication (ex: Mobi.exe), and now I need to
list all forms in my apllication and list all controls (ex: txtname;btnname)
inside of each form.
Technically, controls don't exist inside your form until after the
InitializeComponent() method runs; before that, it's just a bunch of
code that _will_ add controls when run.
If you mean, "all instantiated forms" as in "all the ones showing on
the screen" then you just have to query each form's Controls
collection. If you mean all of the Form classes defined / used by your
application, then you have to instantiate each one and then look at its
Controls collection.
You can get the list of classes defined by an assembly using
Reflection, and then from amongst those find the ones that are derived
(directly or indirectly) from System.Windows.Forms.Form. Then you have
to instantiate each one using "new" and look at its Controls collection.