Help
Help

Reputation: 39

C# PropertyInfo

I can get the PropertyValues like

Type mytype=typeof(TextBox);

foreach(PropertyInfo myinfo in mytype.GetProperties())
{
ListBox1.Items.Add(myinfo.Name);
}

But Some Properties have child How Can I Find child property of I property?

(Sample Devexpress LookUpEdit component DataSource,DisplayMember e.t.c properties under the Properties)Thanks

Upvotes: 1

Views: 1260

Answers (6)

msarchet
msarchet

Reputation: 15242

You can Call

 myInfo.PropertyType().GetProperties();

To Get All of the Properties

Upvotes: 0

tam
tam

Reputation: 1583

You want to grab the type of the property info, then Grab the properties assocciated with that type.

ex:

        PropertyInfo info = GetType().GetProperties()[0];
        Type inner = info.GetType();
        inner.GetProperties();

EDIT: I originally said info.GetType() without actually making sure that was right, I apologize. As long as you know what you are expecting then you shouldn't need recursion for anything

something more simple should work fine:

PropertyInfo[] infos = typeof(SomeClass).GetProperties();
//Find the Property you are looking for
PropertyInfo propertyWithMoreProperties = ....

PropertyInfo[] moreInfos = propertyWidthMoreProperties.PropertyType.GetProperties();

Upvotes: 1

Amir Ismail
Amir Ismail

Reputation: 3883

make a recursive call to a method that take the property and return propertyinfo

Upvotes: 1

Brian Dishaw
Brian Dishaw

Reputation: 5825

You would need to access the PropertyType property of the myinfo object and then get the child properties using GetProperties() from there.

foreach(PropertyInfo myinfo in mytype.GetProperties())
{ 
    ListBox1.Items.Add(myinfo.Name);
    foreach(PropertyInfo mychildren in myinfo.PropertyType.GetProperties())
    {
       //do whatever with them
    }
}

Upvotes: 1

Optillect Team
Optillect Team

Reputation: 1747

Do the same loop (recursively) for the PropertyInfo.PropertyType.GetProperties

Upvotes: 2

mdm
mdm

Reputation: 12630

You can use the PropertyType property to find the type of the property, then in the same way you examine the properties of the TextBox, you can examine these sub-properties.

Type mytype=typeof(TextBox);
foreach(PropertyInfo myinfo in mytype.GetProperties())
{
    ListBox1.Items.Add(myinfo.Name);
    if(myinfo.Name == "Parent")
    {    
        PropertyInfo subProperty = typeof(Control).GetProperty("Name")
        if(subProperty != null)
            // Do some more stuff here
    }
}

Upvotes: 1

Related Questions