public virtual void RegisterServices(IServiceCollection services, IRegisterContext context)
 {
     foreach (var type in AppDomain.CurrentDomain.FindInstanceTypesByAttribute <KnifeAttribute>())
     {
         if (context.HasFiltered(type))
         {
             continue;
         }
         foreach (var injectAttribute in type.GetCustomAttributes(typeof(KnifeAttribute), true).Cast <KnifeAttribute>())
         {
             if (injectAttribute.ValidateFromType != null)
             {
                 if (injectAttribute.ValidateFromType.IsGenericTypeDefinition)
                 {
                     if (!IsAssignableFromGenericType(type, injectAttribute.ValidateFromType))
                     {
                         throw new InvalidOperationException($"The type '{type.FullName}' must be a child class from '{injectAttribute.ValidateFromType.FullName}'.");
                     }
                 }
                 else
                 {
                     if (!injectAttribute.ValidateFromType.IsAssignableFrom(type))
                     {
                         throw new InvalidOperationException($"The type '{type.FullName}' must be a child class from '{injectAttribute.ValidateFromType.FullName}'.");
                     }
                 }
             }
             injectAttribute.RegisterService(services, context, type);
         }
     }
 }
Ejemplo n.º 2
0
 public void RegisterServices(IServiceCollection services, IRegisterContext context)
 {
     if (context.HasFiltered(typeof(IDictionary <,>)))
     {
         return;
     }
     services.AddTransient(typeof(IDictionary <,>), typeof(KnifeInjectionDictionary <,>));
 }
Ejemplo n.º 3
0
        public void RegisterServices(IServiceCollection services, IRegisterContext context)
        {
            foreach (var type in AppDomain.CurrentDomain.FindInterfaceTypesByAttribute <DynamicProxyAttribute>())
            {
                if (context.HasFiltered(type))
                {
                    continue;
                }

                var dynamicImplmentAttributes = type.GetCustomAttributes <DynamicProxyAttribute>(true).ToList();
                if (dynamicImplmentAttributes.Count > 1)
                {
                    throw new NotSupportedException($"Only one DynamicProxyAttribute can be used for the interface '{type.FullName}'.");
                }
                dynamicImplmentAttributes.First().RegisterService(services, context, type);
            }
        }