StringToIPEndpointTypeConverter.cs 1021 B

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