我正在使用反射编写克隆方法。如何使用反射检测属性是否为索引属性?例如:

public string[] Items
{
   get;
   set;
}

到目前为止,我的方法:
public static T Clone<T>(T from, List<string> propertiesToIgnore) where T : new()
{
    T to = new T();

    Type myType = from.GetType();

    PropertyInfo[] myProperties = myType.GetProperties();

    for (int i = 0; i < myProperties.Length; i++)
    {
        if (myProperties[i].CanWrite && !propertiesToIgnore.Contains(myProperties[i].Name))
        {
            myProperties[i].SetValue(to,myProperties[i].GetValue(from,null),null);
        }
    }

    return to;
}

最佳答案

if (propertyInfo.GetIndexParameters().Length > 0)
{
    // Property is an indexer
}

10-06 14:35