mercredi 11 octobre 2017

C# how to tell apart "this[]" from other indexed properties?

I'm looping through the properties of a class and indexed "this[]" properties are causing the parameter count mismatch when I call the PropertyInfo.GetValue() method.

// - Example Class
public class MyObject
{
    // - I want this
    public int Name { get; set; }
    // - And I want this, this is an indexed property
    public IList<MyObject> OtherObjects { get; set; }
    // - But I don't want this!
    public MyObject this[int index] 
    {
        get
        {
            return OtherObjects[index];
        }
    }
}

// - Function
PropertyInfo[] properties = myObject.GetType().GetProperties();
for (int i = 0; i < properties.Length; i++)
{
    // - This skips both the IList and the this[], I want to be able to tell them apart.
    if (properties[i].GetIndexParameters().Length > 0) { continue; }
}

I can't (or rather I don't want to) set attributes to each class' properties that I'm going to call this function on, is there anyway I can distinguish these?





Aucun commentaire:

Enregistrer un commentaire