您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

LuConvertersExtensions.cs 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Linq;
  5. using Luticate2.Auth.Utils.Business.Converters.ObjectConverter;
  6. using Luticate2.Auth.Utils.Business.Converters.ObjectConverterDescriptor;
  7. using Luticate2.Auth.Utils.Business.Utils;
  8. using Luticate2.Auth.Utils.Interfaces;
  9. using Microsoft.CodeAnalysis.Scripting;
  10. using Microsoft.Extensions.DependencyInjection;
  11. using Microsoft.Extensions.DependencyInjection.Extensions;
  12. namespace Luticate2.Auth.Utils.Business.Converters
  13. {
  14. public static class LuConvertersExtensions
  15. {
  16. public class LuObjectConvertersOptions
  17. {
  18. public List<Type> ValueTypes { get; set; }
  19. public List<Type> ListTypes { get; set; }
  20. }
  21. public static void AddLuObjectConverterDescriptors(this IServiceCollection services)
  22. {
  23. var typeILuObjectConverterDescriptor = typeof(ILuObjectConverterDescriptor<,>);
  24. var typeILuObjectConverterDescriptorEnumerable =
  25. typeILuObjectConverterDescriptor.MakeGenericType(typeof(Enumerable), typeof(Enumerable));
  26. services.AddSingleton<ILuObjectConverterDescriptorIdentity, LuObjectConverterDescriptorIdentity>();
  27. services.AddSingleton(typeILuObjectConverterDescriptorEnumerable, typeof(LuObjectConverterDescriptorEnumerable));
  28. }
  29. public static void AddLuObjectConverters(this IServiceCollection services, Action<LuObjectConvertersOptions> optionsAction = null)
  30. {
  31. var typeILuObjectConverter = typeof(ILuObjectConverter<,>);
  32. var options = new LuObjectConvertersOptions
  33. {
  34. ValueTypes = new List<Type>
  35. {
  36. typeof(bool),
  37. typeof(decimal),
  38. typeof(double),
  39. typeof(float),
  40. typeof(sbyte),
  41. typeof(byte),
  42. typeof(char),
  43. typeof(short),
  44. typeof(ushort),
  45. typeof(int),
  46. typeof(uint),
  47. typeof(long),
  48. typeof(ulong),
  49. typeof(Guid),
  50. typeof(DateTime),
  51. typeof(DateTimeOffset),
  52. typeof(TimeSpan)
  53. },
  54. ListTypes = new List<Type>
  55. {
  56. typeof(IEnumerable<>),
  57. typeof(ICollection<>),
  58. typeof(Collection<>),
  59. typeof(IList<>),
  60. typeof(List<>)
  61. }
  62. };
  63. optionsAction?.Invoke(options);
  64. services.TryAddSingleton<ILuConvertersAllocator, LuConvertersAllocator>();
  65. services.AddSingleton<ILuObjectConverterIdentity, LuObjectConverterIdentity>();
  66. foreach (var valueType in options.ValueTypes)
  67. {
  68. var nullableValueType = typeof(Nullable<>).MakeGenericType(valueType);
  69. var converterType = typeILuObjectConverter.MakeGenericType(valueType, nullableValueType);
  70. services.AddSingleton(converterType, typeof(LuObjectConverterIdentity));
  71. }
  72. foreach (var typeFrom in options.ListTypes)
  73. {
  74. foreach (var typeTo in options.ListTypes)
  75. {
  76. services.AddSingleton(typeILuObjectConverter.MakeGenericType(typeFrom, typeTo), typeof(LuObjectConverterLists));
  77. }
  78. }
  79. }
  80. public static void AddLuObjectConverterPoco<TTypeFrom, TTypeTo>(this IServiceCollection services)
  81. {
  82. services.AddSingleton<ILuObjectConverter<TTypeFrom, TTypeTo>, LuObjectConverterPoco<TTypeFrom, TTypeTo>>();
  83. }
  84. public static void AddLuOptions(this IServiceCollection services)
  85. {
  86. services.AddOptions<LuExpressionUtils.LuExpressionParserOptions>().Configure(options =>
  87. {
  88. options.ScriptOptions = ScriptOptions.Default
  89. .AddReferences(typeof(LuExpressionUtils.LuExpressionParserOptions).Assembly)
  90. .AddImports("System.Linq.Enumerable");
  91. });
  92. }
  93. }
  94. }