StringToIPEndpointTypeConverter.cs 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using JetBrains.Annotations;
  2. using ReactiveUI;
  3. using System;
  4. using System.Net;
  5. using Volo.Abp.DependencyInjection;
  6. namespace NatTypeTester.ViewModels.ValueConverters
  7. {
  8. [ExposeServices(typeof(IBindingTypeConverter))]
  9. [UsedImplicitly]
  10. public class StringToIPEndpointTypeConverter : IBindingTypeConverter, ISingletonDependency
  11. {
  12. public int GetAffinityForObjects(Type fromType, Type toType)
  13. {
  14. if (fromType == typeof(string) && toType == typeof(IPEndPoint))
  15. {
  16. return 11;
  17. }
  18. if (fromType == typeof(IPEndPoint) && toType == typeof(string))
  19. {
  20. return 11;
  21. }
  22. return 0;
  23. }
  24. public bool TryConvert(object? from, Type toType, object? conversionHint, out object? result)
  25. {
  26. if (toType == typeof(IPEndPoint) && from is string str)
  27. {
  28. if (IPEndPoint.TryParse(str, out var ipe))
  29. {
  30. result = ipe;
  31. return true;
  32. }
  33. result = null;
  34. return false;
  35. }
  36. if (from is IPEndPoint fromIPEndPoint)
  37. {
  38. result = fromIPEndPoint.ToString();
  39. }
  40. else
  41. {
  42. result = string.Empty;
  43. }
  44. return true;
  45. }
  46. }
  47. }