CodeKicks.com
Focus on Microsoft Technologies - Tutorials, Articles, Code Samples.

Thursday, September 14, 2006

.NET code for Generic Pluck

public class PluckableList<T> : List
{  
public Array Pluck(string propertyName){    
Type type = typeof(T);    
PropertyInfo property = type.GetProperty(propertyName);    
Array result = Array.CreateInstance(property.PropertyType, this.Count);    
for(int i=0;i<this.Count;i++){      
result.SetValue(type.InvokeMember(propertyName, BindingFlags.GetProperty, null, this[i], null), i);    
}    
return result;  
}
}

Post a Comment