Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Reflection Indexed Properties

Tags:

I am writing a Clone method using reflection. How do I detect that a property is an indexed property using reflection? For example:

public string[] Items {    get;    set; } 

My method so far:

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; } 
like image 903
Greg Finzer Avatar asked Nov 14 '08 20:11

Greg Finzer


1 Answers

if (propertyInfo.GetIndexParameters().Length > 0) {     // Property is an indexer } 
like image 117
FlySwat Avatar answered Oct 26 '22 16:10

FlySwat