12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- using System;
- using Luticate2.Auth.Utils.Dbo;
- using Luticate2.Auth.Utils.Dbo.Result;
- using Luticate2.Auth.Utils.Interfaces;
-
- namespace Luticate2.Auth.Utils.Business.Converters.ObjectConverter
- {
- public static class LuObjectConverterUtils
- {
- public static LuResult<ILuObjectConverter> GetObjectConverter(IServiceProvider serviceProvider, Type typeFrom, Type typeTo)
- {
- var type = typeof(ILuObjectConverter<,>);
- var gtype = type.MakeGenericType(typeFrom, typeTo);
- var converter = (ILuObjectConverter) serviceProvider.GetService(gtype);
-
- if (converter == null)
- {
- var baseTypeFrom = typeFrom;
- if (typeFrom.IsGenericType)
- {
- baseTypeFrom = typeFrom.GetGenericTypeDefinition();
- }
- var baseTypeTo = typeTo;
- if (typeTo.IsGenericType)
- {
- baseTypeTo = typeTo.GetGenericTypeDefinition();
- }
- gtype = type.MakeGenericType(baseTypeFrom, baseTypeTo);
- converter = (ILuObjectConverter) serviceProvider.GetService(gtype);
- }
-
- if (converter == null && typeFrom == typeTo)
- {
- converter = (ILuObjectConverter) serviceProvider.GetService(typeof(ILuObjectConverterIdentity));
- }
-
- if (converter != null)
- {
- return LuResult<ILuObjectConverter>.Ok(converter);
- }
-
- return LuResult<ILuObjectConverter>.Error(LuStatus.InternalError.ToInt(),
- $"Could not find converter for {typeFrom} => {typeTo}");
- }
- }
- }
|