tags:

views:

78

answers:

3

Hello, 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

+3  A: 

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.

flq
+2  A: 

You are going too fast. There a three types and three properties here, you'll need to use GetType and GetProperty three times.

Hans Passant
+1  A: 

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 });
Quartermeister
Thank you. Yet, the line nestedProperty.PropertyType.GetProperty(defaultMember.MemberName); returns 'null'. In the 'quickwatch' editor, the 'value' column indicates 'nestedProperty.PropertyType.GetProperty(defaultMember.MemberName)' threw an exception of type 'System.Reflection.AmbiguousMatchException' System.Reflection.PropertyInfo {System.Reflection.AmbiguousMatchException}'
Florian
@Florian: Use the overload of GetProperty that takes in a list of argument types for overload resolution: nestedProperty.PropertyType.GetProperty(defaultMember.MemberName, new Type[] { typeof(int) })
Quartermeister
It works very well. Thank you !!! just a last question. In your example, in the last line : var value = nestedIndexer.GetValue(bands, new object[] { 0 });What is the reference to bands ??
Florian