Guest User

Untitled

a guest
Sep 25th, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq.Expressions;
  4. using System.Reflection;
  5.  
  6. namespace TypeUtil {
  7. public static class TypeExtensions {
  8. public static FastPropertyInfo<T> GetProperty<T>(this Type type, string name) {
  9. return type.GetProperty<T>(name, BindingFlags.Default);
  10. }
  11.  
  12. public static FastPropertyInfo<T> GetProperty<T>(this Type type, string name, BindingFlags flags) {
  13. var property = type.GetProperty(name, flags);
  14.  
  15. if(property == null) {
  16. return null;
  17. }
  18.  
  19. return new FastPropertyInfo<T>(property);
  20. }
  21. }
  22.  
  23. public class FastPropertyInfo<T> {
  24. private PropertyInfo info;
  25. private Func<T, object[], object> getter;
  26. private Action<T, object, object[]> setter;
  27.  
  28. public bool CanRead { get { return info.CanRead; } }
  29. public bool CanWrite { get { return info.CanWrite; } }
  30.  
  31. public FastPropertyInfo(PropertyInfo info) {
  32. this.info = info;
  33. getter = CreateGetValue(info);
  34. setter = CreateSetValue(info);
  35. }
  36.  
  37. public object GetValue(T instance, object[] index) {
  38. if(getter == null) {
  39. throw new ArgumentException("The property's get accessor is not found.");
  40. }
  41.  
  42. return getter(instance, index);
  43. }
  44.  
  45. public void SetValue(T instance, object value, object[] index) {
  46. if(setter == null) {
  47. throw new ArgumentException("The property's set accessor is not found.");
  48. }
  49.  
  50. setter(instance, value, index);
  51. }
  52.  
  53. private static Func<T, object[], object> CreateGetValue(PropertyInfo info) {
  54. if(!info.CanRead) {
  55. return null;
  56. }
  57.  
  58. // info.GetValue(instance, null);
  59. var instanceExpression = Expression.Parameter(typeof(T), "instance");
  60. var indexExpression = Expression.Parameter(typeof(object[]), "index");
  61. var getExpression = Expression.Call(instanceExpression, info.GetGetMethod());
  62. var lambdaExpression = Expression.Lambda<Func<T, object[], object>>(Expression.Convert(getExpression, typeof(object)), instanceExpression, indexExpression);
  63.  
  64. return lambdaExpression.Compile();
  65. }
  66.  
  67. private static Action<T, object, object[]> CreateSetValue(PropertyInfo info) {
  68. if(!info.CanWrite) {
  69. return null;
  70. }
  71.  
  72. // info.SetValue(instance, value, null);
  73. var valueType = info.GetSetMethod().GetParameters()[0].ParameterType;
  74. var instanceExpression = Expression.Parameter(typeof(T), "instance");
  75. var valueExpression = Expression.Parameter(valueType, "value");
  76. var indexExpression = Expression.Parameter(typeof(object[]), "index");
  77. var setExpression = Expression.Call(instanceExpression, info.GetSetMethod(), valueExpression, indexExpression);
  78. var lambdaExpression = Expression.Lambda<Action<T, object, object[]>>(setExpression, instanceExpression, valueExpression, indexExpression);
  79.  
  80. return lambdaExpression.Compile();
  81. }
  82. }
  83. }
Add Comment
Please, Sign In to add comment