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.3KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. using System;
  2. using Luticate2.Auth.Utils.Interfaces;
  3. namespace Luticate2.Auth.Utils.Business.ObjectConverter
  4. {
  5. public static class LuObjectConverterUtils
  6. {
  7. public static ILuObjectConverter GetObjectConverter(IServiceProvider serviceProvider, Type typeFrom, Type typeTo)
  8. {
  9. var type = typeof(ILuObjectConverter<,>);
  10. var gtype = type.MakeGenericType(typeFrom, typeTo);
  11. var converter = (ILuObjectConverter) serviceProvider.GetService(gtype);
  12. if (converter == null)
  13. {
  14. var baseTypeFrom = typeFrom;
  15. if (typeFrom.IsGenericType)
  16. {
  17. baseTypeFrom = typeFrom.GetGenericTypeDefinition();
  18. }
  19. var baseTypeTo = typeTo;
  20. if (typeTo.IsGenericType)
  21. {
  22. baseTypeTo = typeTo.GetGenericTypeDefinition();
  23. }
  24. gtype = type.MakeGenericType(baseTypeFrom, baseTypeTo);
  25. converter = (ILuObjectConverter) serviceProvider.GetService(gtype);
  26. }
  27. if (converter == null && typeFrom == typeTo)
  28. {
  29. converter = (ILuObjectConverter) serviceProvider.GetService(typeof(ILuObjectConverterIdentity));
  30. }
  31. return converter;
  32. }
  33. }
  34. }