Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Runtime.CompilerServices;
  4. using System.Text;
  5.  
  6. namespace Placebo.Internal
  7. {
  8. internal static class ReflectionExtensions
  9. {
  10. public static string GetFriendlyName(this Type type)
  11. {
  12. if (type == null) return "<null>";
  13.  
  14. var typeName = type.Name;
  15. if (!type.IsGenericType)
  16. return typeName;
  17.  
  18. var length = typeName.IndexOf('`');
  19. if (length < 0) length = typeName.Length;
  20.  
  21. return new StringBuilder()
  22. .Append(typeName, 0, length)
  23. .Append('<')
  24. .Append(string.Join(",", type.GetGenericArguments().Select(GetFriendlyName)))
  25. .Append('>')
  26. .ToString();
  27. }
  28.  
  29. public static string GetObjectFriendlyName(this object subject)
  30. {
  31. if (subject == null) return "<null>";
  32. return string.Format(
  33. "{0}@{1:x}",
  34. subject.GetType().GetFriendlyName(),
  35. RuntimeHelpers.GetHashCode(subject));
  36. }
  37. }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement