LinkedProperty.cs 996 B

123456789101112131415161718192021222324252627282930313233343536
  1. using System;
  2. using System.Linq.Expressions;
  3. using System.Reflection;
  4. namespace InABox.Core
  5. {
  6. public class LinkedProperty
  7. {
  8. private LinkedProperty(PropertyInfo source, PropertyInfo target)
  9. {
  10. Source = source;
  11. Target = target;
  12. }
  13. public PropertyInfo Source { get; }
  14. public PropertyInfo Target { get; }
  15. public static LinkedProperty Create<TEntityLink, TLinkedEntity>(Expression<Func<TEntityLink, object>> source,
  16. Expression<Func<TLinkedEntity, object>> target)
  17. {
  18. return new LinkedProperty(
  19. CoreUtils.GetPropertyFromExpression(source),
  20. CoreUtils.GetPropertyFromExpression(target)
  21. );
  22. }
  23. public void Update(object from, object to)
  24. {
  25. if (from != null && to != null)
  26. {
  27. var value = Source.GetValue(from);
  28. Target.SetValue(to, value);
  29. }
  30. }
  31. }
  32. }