23

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

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;
}
4

6 回答 6

50
if (propertyInfo.GetIndexParameters().Length > 0)
{
    // Property is an indexer
}
于 2008-11-14T20:57:27.243 回答
21

对不起,可是

public string[] Items { get; set; }

不是索引属性,它只是数组类型!然而以下是:

public string this[int index]
{
    get { ... }
    set { ... }
}
于 2009-01-13T19:49:54.957 回答
9

你要的是GetIndexParameters()方法。如果它返回的数组有超过 0 个项目,这意味着它是一个索引属性。

有关详细信息,请参阅MSDN 文档。

于 2008-11-14T20:58:04.120 回答
2

如果你调用property.GetValue(obj,null),并且属性被索引,那么你会得到一个参数计数不匹配异常。最好检查属性是否被索引GetIndexParameters(),然后决定做什么。

于 2011-06-17T11:13:18.480 回答
1

这是一些对我有用的代码:

foreach(obj.GetType().GetProperties() 中的 PropertyInfo 属性)
{
  对象值 = property.GetValue(obj, null);
  如果(值是对象[])
  {
    ……
  }
}

PS .GetIndexParameters().Length > 0)适用于本文中描述的情况:http: //msdn.microsoft.com/en-us/library/b05d59ty.aspx 因此,如果您关心名为 Chars 的字符串类型值的属性,请使用它,但它不适用于我感兴趣的大多数数组,包括我很确定原始问题中的字符串数组。

于 2011-06-07T21:53:11.667 回答
1

您可以将索引器转换为 IEnumerable

    public static IEnumerable<T> AsEnumerable<T>(this object o) where T : class {
        var list = new List<T>();
        System.Reflection.PropertyInfo indexerProperty = null;
        foreach (System.Reflection.PropertyInfo pi in o.GetType().GetProperties()) {
            if (pi.GetIndexParameters().Length > 0) {
                indexerProperty = pi;
                break;
            }
        }

        if (indexerProperty.IsNotNull()) {
            var len = o.GetPropertyValue<int>("Length");
            for (int i = 0; i < len; i++) {
                var item = indexerProperty.GetValue(o, new object[]{i});
                if (item.IsNotNull()) {
                    var itemObject = item as T;
                    if (itemObject.IsNotNull()) {
                        list.Add(itemObject);
                    }
                }
            }
        }

        return list;
    }


    public static bool IsNotNull(this object o) {
        return o != null;
    }

    public static T GetPropertyValue<T>(this object source, string property) {
        if (source == null)
            throw new ArgumentNullException("source");

        var sourceType = source.GetType();
        var sourceProperties = sourceType.GetProperties();
        var properties = sourceProperties
            .Where(s => s.Name.Equals(property));
        if (properties.Count() == 0) {
            sourceProperties = sourceType.GetProperties(BindingFlags.Instance | BindingFlags.NonPublic);
            properties = sourceProperties.Where(s => s.Name.Equals(property));
        }

        if (properties.Count() > 0) {
            var propertyValue = properties
                .Select(s => s.GetValue(source, null))
                .FirstOrDefault();

            return propertyValue != null ? (T)propertyValue : default(T);
        }

        return default(T);
    }
于 2013-07-02T09:10:33.127 回答