You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LuObjectConverterUtils.cs 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using System;
  2. using Luticate2.Auth.Utils.Dbo;
  3. using Luticate2.Auth.Utils.Dbo.Result;
  4. using Luticate2.Auth.Utils.Interfaces;
  5. namespace Luticate2.Auth.Utils.Business.Converters.ObjectConverter
  6. {
  7. public static class LuObjectConverterUtils
  8. {
  9. public static LuResult<ILuObjectConverter> GetObjectConverter(IServiceProvider serviceProvider, Type typeFrom, Type typeTo)
  10. {
  11. var type = typeof(ILuObjectConverter<,>);
  12. var gtype = type.MakeGenericType(typeFrom, typeTo);
  13. var converter = (ILuObjectConverter) serviceProvider.GetService(gtype);
  14. if (converter == null)
  15. {
  16. var baseTypeFrom = typeFrom;
  17. if (typeFrom.IsGenericType)
  18. {
  19. baseTypeFrom = typeFrom.GetGenericTypeDefinition();
  20. }
  21. var baseTypeTo = typeTo;
  22. if (typeTo.IsGenericType)
  23. {
  24. baseTypeTo = typeTo.GetGenericTypeDefinition();
  25. }
  26. gtype = type.MakeGenericType(baseTypeFrom, baseTypeTo);
  27. converter = (ILuObjectConverter) serviceProvider.GetService(gtype);
  28. }
  29. if (converter == null && typeFrom == typeTo)
  30. {
  31. converter = (ILuObjectConverter) serviceProvider.GetService(typeof(ILuObjectConverterIdentity));
  32. }
  33. if (converter != null)
  34. {
  35. return LuResult<ILuObjectConverter>.Ok(converter);
  36. }
  37. return LuResult<ILuObjectConverter>.Error(LuStatus.InternalError.ToInt(),
  38. $"Could not find converter for {typeFrom} => {typeTo}");
  39. }
  40. }
  41. }