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.

LuConvertersAllocator.cs 1.1KB

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