Can I get a simple text list of controls that are present on a web form using reflection? Basically, a colleague wants a list of controls to help determine a validation strategy, for example. in general, product numbers must be numeric, but on some screens they may be alphanumeric. I thought that using reflection would be easy to create a list of something like:
AddProduct.aspx txtProductNumber txtProductName etc.
I can get the names of the forms, but still did not contain the controls. 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;
Assembly.CreateInstance has a couple of overloads. For example, if I change the page’s destination string to
Page of page = (Page) assembly.CreateInstance (types [i] .FullName, true, BindingFlags.NonPublic, null, null, null, null);
then I get an error with a missing constructor.
So, am I completely off the wrong track or am I even trying to do this at all? Any help is greatly appreciated.
Edit: Sorry for the delay in answering this question. We got a bit more using Assembly.GetCallingAssembly () to create a list, but it still didn't quite fit our needs. In the end, we used the longer Find in Target approach.
source share