daily pastebin goal
24%
SHARE
TWEET

Untitled

a guest May 16th, 2018 104 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using System.Reflection.Emit;
  5.  
  6. public class Populator<T>
  7. {
  8.     private delegate T Load(Dictionary<string, object> properties);
  9.     private Load _handler;
  10.     private Populator() { }
  11.     public T Build(Dictionary<string, object> properties)
  12.     {
  13.         return _handler(properties);
  14.     }
  15.     public static Populator<T> CreateBuilder(Dictionary<string, object> properties)
  16.     {
  17.         //private static readonly MethodInfo getValueMethod = typeof(IDataRecord).GetMethod("get_Item", new [] { typeof(int) });
  18.         //private static readonly MethodInfo isDBNullMethod = typeof(IDataRecord).GetMethod("IsDBNull", new [] { typeof(int) });
  19.         Populator<T> dynamicBuilder = new Populator<T>();
  20.         DynamicMethod method = new DynamicMethod("Create", typeof(T), new[] { typeof(Dictionary<string, object>) }, typeof(T), true);
  21.         ILGenerator generator = method.GetILGenerator();
  22.         LocalBuilder result = generator.DeclareLocal(typeof(T));
  23.         generator.Emit(OpCodes.Newobj, typeof(T).GetConstructor(Type.EmptyTypes));
  24.         generator.Emit(OpCodes.Stloc, result);
  25.         int i = 0;
  26.         foreach (var property in properties)
  27.         {
  28.             PropertyInfo propertyInfo = typeof(T).GetProperty(property.Key, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase | BindingFlags.FlattenHierarchy | BindingFlags.Default);
  29.             Label endIfLabel = generator.DefineLabel();
  30.  
  31.             if (propertyInfo != null && propertyInfo.GetSetMethod() != null)
  32.             {
  33.                 generator.Emit(OpCodes.Ldarg_0);
  34.                 generator.Emit(OpCodes.Ldc_I4, i);
  35.                 //generator.Emit(OpCodes.Callvirt, isDBNullMethod);
  36.                 generator.Emit(OpCodes.Brtrue, endIfLabel);
  37.  
  38.                 generator.Emit(OpCodes.Ldloc, result);
  39.                 generator.Emit(OpCodes.Ldarg_0);
  40.                 generator.Emit(OpCodes.Ldc_I4, i);
  41.                 //generator.Emit(OpCodes.Callvirt, getValueMethod);
  42.  
  43.                 generator.Emit(OpCodes.Unbox_Any, property.Value.GetType());
  44.                 generator.Emit(OpCodes.Callvirt, propertyInfo.GetSetMethod());
  45.                 generator.MarkLabel(endIfLabel);
  46.             }
  47.             i++;
  48.         }
  49.  
  50.         generator.Emit(OpCodes.Ldloc, result);
  51.         generator.Emit(OpCodes.Ret);
  52.         dynamicBuilder._handler = (Load)method.CreateDelegate(typeof(Load));
  53.         return dynamicBuilder;
  54.     }
  55. }
  56.    
  57. using System;
  58. using System.Collections.Generic;
  59. using System.ComponentModel;
  60. using Hyper.ComponentModel;
  61.  
  62. namespace Test
  63. {
  64.     class Person
  65.     {
  66.         public int Id { get; set; }
  67.         public string Name { get; set; }
  68.     }
  69.  
  70.     class Program
  71.     {
  72.         static void Main()
  73.         {
  74.             HyperTypeDescriptionProvider.Add(typeof(Person));
  75.             var properties = new Dictionary<string, object> { { "Id", 10 }, { "Name", "Fred Flintstone" } };
  76.             Person person = new Person();
  77.             DynamicUpdate(person, properties);
  78.             Console.WriteLine("Id: {0}; Name: {1}", person.Id, person.Name);
  79.             Console.ReadKey();
  80.         }
  81.  
  82.         public static void DynamicUpdate<T>(T entity, Dictionary<string, object> properties)
  83.         {
  84.             foreach (PropertyDescriptor propertyDescriptor in TypeDescriptor.GetProperties(typeof(T)))
  85.                 if (properties.ContainsKey(propertyDescriptor.Name))
  86.                     propertyDescriptor.SetValue(entity, properties[propertyDescriptor.Name]);
  87.         }
  88.     }
  89. }
  90.    
  91. public static IEnumerable<T> Read<T>(IDataReader reader) where T : class, new()
  92. {
  93.     PropertyDescriptorCollection props =
  94.         TypeDescriptor.GetProperties(typeof(T));
  95.  
  96.     PropertyDescriptor[] propArray = new PropertyDescriptor[reader.FieldCount];
  97.     for (int i = 0; i < propArray.Length; i++)
  98.     {
  99.         propArray[i] = props[reader.GetName(i)];
  100.     }
  101.     while(reader.Read()) {
  102.         T item = new T();
  103.         for (int i = 0; i < propArray.Length; i++)
  104.         {
  105.             object value = reader.IsDBNull(i) ? null : reader[i];
  106.             propArray[i].SetValue(item, value);
  107.         }
  108.         yield return item;
  109.     }
  110. }
  111.    
  112. for (int i = 0; i < dataRecord.FieldCount; i++)
  113.                 {
  114.  
  115.                     PropertyInfo propertyInfo = t.GetProperty(dataRecord.GetName(i));
  116.                     LocalBuilder il_P = generator.DeclareLocal(typeof(PropertyInfo));
  117.  
  118.                     Label endIfLabel = generator.DefineLabel();
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top