Guest User

FillWithDataIEnumerable

a guest
Apr 28th, 2014
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.73 KB | None | 0 0
  1.     public static class FillWithDataEx
  2.     {
  3.         public static void FillWithDataIEnumerable<T>(this iGrid grid, IEnumerable<T> items)
  4.             where T : class
  5.         {
  6.             PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
  7.  
  8.             var propGetters = new List<Func<T, object>>();
  9.             foreach (var prop in props)
  10.             {
  11.                 var col = grid.Cols.Add();
  12.                 col.Text = prop.Name;
  13.  
  14.                 Func<T, object> func = (Func<T, object>)ReflectionUtility.GetGetter(prop);
  15.                 propGetters.Add(func);
  16.             }
  17.  
  18.             foreach (var item in items)
  19.             {
  20.                 var row = grid.Rows.Add();
  21.  
  22.                 var values = new object[props.Length];
  23.                 for (var i = 0; i < props.Length; i++)
  24.                 {
  25.                     row.Cells[i].Value = propGetters[i](item);
  26.                 }
  27.             }
  28.         }
  29.     }
  30.  
  31.     public class ReflectionUtility
  32.     {
  33.         internal static Func<object, object> GetGetter(PropertyInfo property)
  34.         {
  35.             // get the get method for the property
  36.             MethodInfo method = property.GetGetMethod(true);
  37.            
  38.             // get the generic get-method generator (ReflectionUtility.GetSetterHelper<TTarget, TValue>)
  39.             MethodInfo genericHelper = typeof(ReflectionUtility).GetMethod(
  40.                 "GetGetterHelper",
  41.                 BindingFlags.Static | BindingFlags.NonPublic);
  42.            
  43.             // reflection call to the generic get-method generator to generate the type arguments
  44.             MethodInfo constructedHelper = genericHelper.MakeGenericMethod(
  45.                 method.DeclaringType,
  46.                 method.ReturnType);
  47.            
  48.             // now call it. The null argument is because it's a static method.
  49.             object ret = constructedHelper.Invoke(null, new object[] { method });
  50.            
  51.             // cast the result to the action delegate and return it
  52.             return (Func<object, object>) ret;
  53.         }
  54.    
  55.         static Func<object, object> GetGetterHelper<TTarget, TResult>(MethodInfo method)
  56.             where TTarget : class // target must be a class as property sets on structs need a ref param
  57.         {
  58.             // Convert the slow MethodInfo into a fast, strongly typed, open delegate
  59.             Func<TTarget, TResult> func = (Func<TTarget, TResult>) Delegate.CreateDelegate(typeof(Func<TTarget, TResult>), method);
  60.            
  61.             // Now create a more weakly typed delegate which will call the strongly typed one
  62.             Func<object, object> ret = (object target) => (TResult) func((TTarget) target);
  63.             return ret;
  64.         }
  65.     }
Advertisement
Add Comment
Please, Sign In to add comment