Advertisement
Guest User

ZenjectToMsDiNotWorking

a guest
Jan 30th, 2020
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.81 KB | None | 0 0
  1.         private void RegisterFromServiceCollection(IServiceCollection services)
  2.         {
  3.             var serviceProvider = services.BuildServiceProvider();
  4.  
  5.             var delegateType = typeof(Delegate);
  6.             foreach (var service in services)
  7.             {
  8.                 if (delegateType.IsAssignableFrom(service.ServiceType))
  9.                 {
  10.                     Debug.Log($"Skipping type registration, because of delegate registration: {service.ServiceType}. Register this type manually");
  11.  
  12.                     continue;
  13.                 }
  14.  
  15.                 ScopeConcreteIdArgConditionCopyNonLazyBinder binding;
  16.                 if (!(service.ImplementationType is null))
  17.                 {
  18.                     binding = Container.Bind(service.ServiceType).To(service.ImplementationType);
  19.                 }
  20.                 else if (!(service.ImplementationFactory is null))
  21.                 {
  22.                     binding = Container.Bind(service.ServiceType).FromMethod(_ => service.ImplementationFactory(serviceProvider));
  23.                 }
  24.                 else
  25.                 {
  26.                     binding = Container.Bind(service.ServiceType).To(service.ServiceType);
  27.                 }
  28.  
  29.                 switch (service.Lifetime)
  30.                 {
  31.                     case ServiceLifetime.Singleton:
  32.                         binding.AsSingle();
  33.                         break;
  34.                     case ServiceLifetime.Scoped:
  35.                         binding.AsCached();
  36.                         break;
  37.                     case ServiceLifetime.Transient:
  38.                         binding.AsTransient();
  39.                         break;
  40.                     default:
  41.                         throw new ArgumentOutOfRangeException($"Unknown service lifetime{service}");
  42.                 }
  43.             }
  44.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement