Advertisement
Guest User

Untitled

a guest
Apr 5th, 2022
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.57 KB | None | 0 0
  1.     internal class PropertyHelper
  2.     {
  3.         private readonly IReadOnlyDictionary<string, PropertyAccessor> _accessors;
  4.  
  5.         public PropertyHelper(Type objectType)
  6.         {
  7.             var accessors = new Dictionary<string, PropertyAccessor>();
  8.             foreach (var propertyInfo in objectType.GetProperties())
  9.             {
  10.                 if (propertyInfo.GetIndexParameters().Any())
  11.                     continue;
  12.  
  13.                 var propAccessorType =
  14.                     typeof(PropertyAccessor<,>).MakeGenericType(propertyInfo.DeclaringType!, propertyInfo.PropertyType);
  15.                 var accessor = (PropertyAccessor)Activator.CreateInstance(propAccessorType, propertyInfo)!;
  16.                 accessors.Add(propertyInfo.Name, accessor);
  17.             }
  18.  
  19.             _accessors = accessors;
  20.         }
  21.  
  22.         public object? GetPropertyValue(object instance, string propName) => _accessors[propName].Read(instance);
  23.  
  24.         public void SetPropertyValue(object instance, string propName, object value) =>
  25.             _accessors[propName].Write(instance, value);
  26.  
  27.         private abstract class PropertyAccessor
  28.         {
  29.             public abstract object? Read(object instance);
  30.  
  31.             public abstract void Write(object instance, object value);
  32.         }
  33.  
  34.         private class PropertyAccessor<T, TProp> : PropertyAccessor
  35.         {
  36.             private readonly Func<T, TProp> _getter;
  37.             private readonly Action<T, TProp> _setter;
  38.  
  39.             public PropertyAccessor(PropertyInfo propertyInfo)
  40.             {
  41.                 _getter = (Func<T, TProp>)Delegate.CreateDelegate(typeof(Func<T, TProp>), null,
  42.                     propertyInfo.GetGetMethod()!);
  43.                 _setter = (Action<T, TProp>)Delegate.CreateDelegate(typeof(Action<T, TProp>), null,
  44.                     propertyInfo.GetSetMethod()!);
  45.             }
  46.  
  47.             public override object? Read(object instance) => _getter((T)instance);
  48.  
  49.             public override void Write(object instance, object value) => _setter((T)instance, (TProp)value);
  50.         }
  51.     }
  52.  
  53.     internal class Foo
  54.     {
  55.         public string Prop1 { get; set; }
  56.         public int Prop2 { get; set; }
  57.     }
  58.  
  59.     internal class Program
  60.     {
  61.         private static void Main(string[] args)
  62.         {
  63.             var propertyHelper = new PropertyHelper(typeof(Foo));
  64.             var foo = new Foo();
  65.  
  66.             propertyHelper.SetPropertyValue(foo, "Prop1", "zaza");
  67.             propertyHelper.SetPropertyValue(foo, "Prop2", 11);
  68.  
  69.             Console.WriteLine("Hello World!");
  70.         }
  71.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement