Advertisement
ivandrofly

Building copy property (UNDONE)

Mar 22nd, 2019
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.34 KB | None | 0 0
  1.     internal static class PluginExtentionMethods
  2.     {
  3.         private static Action<object, Plugin, string> _copier;
  4.  
  5.         static PluginExtentionMethods()
  6.         {
  7.             ParameterExpression instanceParam = Expression.Parameter(typeof(object), "instance");
  8.             ParameterExpression destinationParam = Expression.Parameter(typeof(Plugin), "destination");
  9.  
  10.             ParameterExpression sourceParam = Expression.Parameter(typeof(string), "propName");
  11.  
  12.             BinaryExpression copyExp = Expression.Assign(Expression.Property(destinationParam, typeof(), Expression.Property(instanceParam, source.Name)));
  13.  
  14.             _copier = Expression.Lambda<Action<object, Plugin, string>>(copyExp, instanceParam, destinationParam, sourceParam).Compile();
  15.         }
  16.  
  17.         /// <summary>
  18.         /// Copy the value from the source property to the instance
  19.         /// </summary>
  20.         public static void Copy(this PropertyInfo source, Plugin destinationType, object instance)
  21.         {
  22.             _copier(instance, destinationType, source.Name);
  23.         }
  24.     }
  25.  
  26. // note: there is a issue 'cause we can't past the property name. (the idea was to allow passing property names at the run-time, but it looks like we are not allowed to do that in c#)
  27.  
  28. // this code was a snipped from a feature I was building to Subtitle Edit (Plugin system)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement