UrlExtensions.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. using System;
  2. namespace Emby.Server.Implementations.Services
  3. {
  4. /// <summary>
  5. /// Donated by Ivan Korneliuk from his post:
  6. /// http://korneliuk.blogspot.com/2012/08/servicestack-reusing-dtos.html
  7. ///
  8. /// Modified to only allow using routes matching the supplied HTTP Verb
  9. /// </summary>
  10. public static class UrlExtensions
  11. {
  12. public static string GetMethodName(this Type type)
  13. {
  14. var typeName = type.FullName != null // can be null, e.g. generic types
  15. ? LeftPart(type.FullName, "[[") // Generic Fullname
  16. .Replace(type.Namespace + ".", string.Empty) // Trim Namespaces
  17. .Replace("+", ".") // Convert nested into normal type
  18. : type.Name;
  19. return type.IsGenericParameter ? "'" + typeName : typeName;
  20. }
  21. private static string LeftPart(string strVal, string needle)
  22. {
  23. if (strVal == null)
  24. {
  25. return null;
  26. }
  27. var pos = strVal.IndexOf(needle, StringComparison.OrdinalIgnoreCase);
  28. return pos == -1
  29. ? strVal
  30. : strVal.Substring(0, pos);
  31. }
  32. }
  33. }