Advertisement
Guest User

Untitled

a guest
May 24th, 2015
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Program
  4. {
  5. public class Program
  6. {
  7. public static void Main()
  8. {
  9. var lion = new Lion { Name = "Simba" };
  10. var tiger = lion.CreateMap<Tiger, Lion>(Cast);
  11.  
  12. Console.WriteLine(tiger.FullName);
  13. }
  14.  
  15. public static void Cast(Tiger dest, Lion src)
  16. {
  17. dest.FullName = src.Name;
  18. }
  19. }
  20.  
  21. public class Lion
  22. {
  23. public string Name { get; set; }
  24. }
  25.  
  26. public class Tiger
  27. {
  28. public string FullName { get; set; }
  29. }
  30. }
  31.  
  32. namespace System {
  33. public static class ObjectExtensions {
  34.  
  35. public static TDestination CreateMap<TDestination, TSource>(this TSource src, Action<TDestination, TSource> action) where TDestination : new()
  36. {
  37. var result = new TDestination();
  38. action(result, src);
  39. return result;
  40. }
  41. }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement