Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

LuConvertersAllocator.cs 1.1KB

12345678910111213141516171819202122232425262728293031323334
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using Luticate2.Auth.Utils.Interfaces;
  5. namespace Luticate2.Auth.Utils.Business.Converters
  6. {
  7. public class LuConvertersAllocator : ILuConvertersAllocator
  8. {
  9. protected static IDictionary<Type, Type> listTypes = new Dictionary<Type, Type>
  10. {
  11. {typeof(IEnumerable<>), typeof(List<>)},
  12. {typeof(ICollection<>), typeof(Collection<>)},
  13. {typeof(Collection<>), typeof(Collection<>)},
  14. {typeof(IList<>), typeof(List<>)},
  15. {typeof(List<>), typeof(List<>)}
  16. };
  17. public object GetInstance(Type type)
  18. {
  19. if (type.IsGenericType)
  20. {
  21. var gtype = type.GetGenericTypeDefinition();
  22. if (listTypes.ContainsKey(gtype))
  23. {
  24. var glistType = listTypes[gtype].MakeGenericType(type.GenericTypeArguments[0]);
  25. return Activator.CreateInstance(glistType);
  26. }
  27. }
  28. return Activator.CreateInstance(type);
  29. }
  30. }
  31. }