Hi all,
I'm trying to access all public properties, along with their values, of an object using reflection. So far so good.
My problem comes when I want to get the values of a collection that is exposed through an indexer. I have found the following code online (source:
My issue is that the index to the indexer is not a number but a string - therefor I cannot access it by position but only by knowing the key.
Can anyone provide any guidance? More code available if required.
Thanks,
MadJock
"Just beacuse you're paranoid, don't mean they're not after you
I'm trying to access all public properties, along with their values, of an object using reflection. So far so good.
My problem comes when I want to get the values of a collection that is exposed through an indexer. I have found the following code online (source:
Code:
// retrieve the property descriptor for the specified property
PropertyInfo objProp = GetPropertyDescriptor(obj, strName);
// get a reference to the collection
object objCol = objProp.GetValue(obj, new object[0]);
// find the default property (the indexed one) of the collection
PropertyInfo objPropIndexer = null;
MemberInfo[] objaDefaultMembers = objCol.GetType().GetDefaultMembers();
for (int i = 0; i < objaDefaultMembers.Length; i++)
{
if (objaDefaultMembers[i].MemberType == MemberTypes.Property)
{
objPropIndexer = (PropertyInfo)objaDefaultMembers[i];
}
}
// get the element at the specified index
object[] objParam = new object[1];
objParam[0] = (object)intIndex;
object objValue = objPropIndexer.GetValue(objCol, objParam);
My issue is that the index to the indexer is not a number but a string - therefor I cannot access it by position but only by knowing the key.
Can anyone provide any guidance? More code available if required.
Thanks,
MadJock
"Just beacuse you're paranoid, don't mean they're not after you