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

Thursday, September 14, 2006

.NET Code for Pluck Function

Description: Inspired by the javascript prototype library, this method grabs a value of property off of each element contained within a collection and returns an array containing elements of the same type as the property.

public Array Pluck(string propertyName){	
Type type = typeof(Person);	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