2008-11-14 119 views
22

我正在使用反射寫一個克隆方法。如何使用反射檢測屬性是索引屬性?例如:C#反射索引屬性

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; 
} 
+13

這不是一個索引屬性,一個是返回數組的屬性。 – 2008-11-14 21:29:04

+3

這個問題需要由管理員進行更改。這是查找索引器屬性的最佳谷歌搜索結果,但這不是代碼示例所示的結果。下面的答案的一半回答了問題,一半是代碼示例。 – 2015-02-21 17:35:33

回答

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

你想要什麼GetIndexParameters()方法。如果它返回的數組超過0個項目,這意味着它是一個索引屬性。

請參閱the MSDN documentation瞭解更多詳情。

20

很抱歉,但

public string[] Items { get; set; } 

索引屬性,它只是一個數組類型的! 但是下面是:

public string this[int index] 
{ 
    get { ... } 
    set { ... } 
} 
+1

好的,但你如何通過反思找到它? – BrainSlugs83 2014-03-07 03:30:13

1

下面是一些代碼,爲我工作:

 
foreach (PropertyInfo property in obj.GetType().GetProperties()) 
{ 
    object value = property.GetValue(obj, null); 
    if (value is object[]) 
    { 
    .... 
    } 
} 

附: .GetIndexParameters().Length > 0)適用於本文中描述的案例:http://msdn.microsoft.com/en-us/library/b05d59ty.aspx 因此,如果您關心名稱爲Chars的屬性以獲取字符串類型的值,請使用該屬性,但對於我感興趣的大多數數組不適用,包括我很確定,來自原始問題的字符串數組。

2

如果您致電property.GetValue(obj,null),並且索引屬性IS,那麼您將得到一個參數計數不匹配異常。更好地檢查是否使用GetIndexParameters()索引該房產,然後決定要做什麼。

0

您可以將索引轉換爲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); 
    }