Is it possible to get a plain text list of controls that are present on a webform using reflection? Basically a colleague is looking to get a list of controls to help define a validation strategy e.g. in general product numbers must be numeric but on certain screens they can be alphanumeric. I thought it would be straightforward using reflection to generate a list of something like:
AddProduct.aspx
txtProductNumber
txtProductName
etc.
I can get the form names but so far not the controls contained within. The current code looks like this:
Assembly assembly = Assembly.LoadFrom(@"Filename.dll");
Type[] types = assembly.GetExportedTypes();
for (int i = 0; i < types.Length; i++)
{
Page page = (Page)assembly.CreateInstance(types[i].FullName);
ControlCollection controls = page.Controls;
// At this point controls.Count = 0 presumably because the controls are defined as protected.
}
Assembly.CreateInstance has a couple of overloads. For example if I change the page assignment line to
Page page = (Page)assembly.CreateInstance(types[i].FullName, true, BindingFlags.NonPublic, null, null, null, null);
then I get an error about a missing constructor.
So have I gone completely down the wrong path or is what I'm attempting to do actually possible at all? Any help is much appreciated.
Edit: Apologies for the delayed response to this question. We got a bit further using Assembly.GetCallingAssembly() to generate a list but it still didn't quite meet our needs. We used a more long-winded Find in Entire solution approach in the end.