Reflection/Metadata on an assembly to get Windows form controls

R

Ron

I am building a utility that needs to extract all Windows forms control
names from a compiled assembly for documentation purposes.

I am currently using reflection, but not all of the controls are coming
back. What should I be doing differently?

Thanks,
Ron

private void Reflect(string AssemblyPath)
{
Assembly assem = Assembly.LoadFrom(AssemblyPath);

Type[] types = assem.GetTypes();

foreach (Type t in types)
{
try
{
richTextBox1.Text += ("\r\n" + t.FullName + "\r\n");
MemberInfo[] mbrInfoArray = t.GetMembers();
foreach (MemberInfo mbrInfo in mbrInfoArray)
{
richTextBox1.Text += string.Format("{0} is a
{1}\r\n", mbrInfo, mbrInfo.MemberType);
Application.DoEvents();
}

}
}
catch (System.NullReferenceException)
{
Console.WriteLine("Error msg");
}
}

}

}
 
R

Ron

Peter,

Thanks for the reply.

The assembly I want to run against is one of our Windows Forms projects.

It has roughly 80+ forms.

I am looking to extract each of the Windows controls on each of the forms
through reflection, which I will be using for security level permissions.

Ron
 
R

Ron

Peter,
Fair enough.

Its not returning any controls...only properties and methods and forms. I
dont see a single control in my output.

Ron
 
P

parez

Peter,
Fair enough.

Its not returning any controls...only properties and methods and forms. I
dont see a single control in my output.

Ron

Try using the following method for each of the form. that mite give
you what you are looking for

GetAllControls<Control>(form);

protected List<T> GetAllControls<T>(Control control) where T:Control
{

List<T> controls = new List<T>();

foreach (Control child in control.Controls)
{
//if (child is T)
T specificControl = child as T;

if (specificControl != null)
controls.Add(specificControl);

if (child.HasChildren)
controls.AddRange(GetAllControls<T>(child));
}
return controls;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top