How to list all properties of a Control or Class using reflection?

endrec
endrec used Ask the Experts™
on
I wanted to populate a drop down list withall  the properties of an object and wanted to know how to do this using a function that returns a collection, it seems reflection is the way to go.

Example:  Imagine you want to list the properties of a custom control onto a page or populate a dropdown menu or collection with all the properties of the control.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Are you looking for something like this example -http://www.vcskicks.com/angle_user_control.php

Author

Commented:
No, a function that is passed in a control/class/object and returns a collection of that object's properties.
Get Property Names using Reflection

using System.Reflection;  // reflection namespace

// get all public static properties of MyClass type
PropertyInfo[] propertyInfos;
propertyInfos = typeof(MyClass).GetProperties();
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

ignore commnts
// get all public static properties of MyClass type

propertyInfos = typeof(MyClass).GetProperties();

will return all the properties of MyClass
Retired
Distinguished Expert 2017
Commented:
Hi endrec;

Here is some sample code showing how to get the properties of a TextBox.

Fernando
Type t = textBox1.GetType();
PropertyInfo[] props = t.GetProperties(BindingFlags.FlattenHierarchy | BindingFlags.Instance | 
                                 BindingFlags.NonPublic | BindingFlags.Public);

foreach (PropertyInfo propInfo in props)
{
    Console.WriteLine("Type = {0}  Name = {1}  Value = {2}", propInfo.PropertyType.Name, propInfo.Name, propInfo.GetValue(textBox1, null));
}

Open in new window

x77
Commented:
Try TypeDescriptor.GetProperties(SomeObject) or GetProperties(Type)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
   Dim lista = TypeDescriptor.GetProperties(Me)
   For Each p As PropertyDescriptor In lista
       Debug.Print(p.Name)
   Next
   lista = TypeDescriptor.GetProperties(GetType(DataTable))
   For Each p As PropertyDescriptor In lista
       Debug.Print(p.Name)
   Next
End Sub

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial