Reputation: 4728
further to my previous question, I have a new problem now, I want to get a property which is an array :
string propertyName="DisplayLayout.Bands[0].Columns";
PropertyInfo pi = control.GetType().GetProperty(propertyName)
But actually, it returns null.
Best Regards,
Florian Edit : Sorry for the lack of precision :$ I access to Bands property thanks to the answers of my precent question. My real problem is to access at the 'Columns' property which is a property of type 'Band'. I hope it's more clear.
EDIT2 : here an example :
PropertyInfo t = control.GetType().GetProperty(entry.Value[i].Nom.Split(new char[] { '.' })[0]);
PropertyInfo property = control.GetType().GetProperty(entry.Value[i].Nom.Split(new char[] { '.' })[0]);
PropertyInfo nestedProperty = property.PropertyType.GetProperty("Bands");
in nestedProperty I have Bands (Infragistics.UltraWinGrid.BandsCollection Bands) but I don't manage to access Bands[0] and the 'Column' Property
Upvotes: 1
Views: 2585
Reputation: 59129
The syntax Bands[0]
is an indexer access. Indexers are a properties that take parameters. C# doesn't allow properties with parameters to be accessed by name, but allows indexer syntax for the property matching the name given in a DefaultMemberAttribute on type type. To get the PropertyInfo
for the indexer in your example, you could write:
PropertyInfo nestedProperty = property.PropertyType.GetProperty("Bands");
var defaultMember = (DefaultMemberAttribute)Attribute.GetCustomAttribute(nestedProperty.PropertyType, typeof(DefaultMemberAttribute));
var nestedIndexer = nestedProperty.PropertyType.GetProperty(defaultMember.MemberName);
In order to get a value from the indexer, you will need to supply a second parameter to PropertyInfo.GetValue with the values to pass in:
var value = nestedIndexer.GetValue(bands, new object[] { 0 });
Upvotes: 1
Reputation: 941267
You are going too fast. There a three types and three properties here, you'll need to use GetType and GetProperty three times.
Upvotes: 2
Reputation: 22829
When you refer to a Type of some instance, you will only get "reflective" access to the methods, properties, etc. of said type.
Hence, the dot notation is not supported as you are basically touching 3 types, some control, some collection of Bands and a Band instance.
In other words, you can ask the control for the property "Bands" or the type of "Band" for the property "Columns", but no dot-notation.
Upvotes: 3