bool CanHandle(ObjectRequestedEventArgs args, out OpenGenericRegistrationData registrationData, out Type[] genericArgs) { genericArgs = null; registrationData = null; var contractType = args.ContractType; if (!contractType.IsGenericType) { return(false); } var openGenericContractType = contractType.GetGenericTypeDefinition(); if (openGenericContractType == null) { throw new ImpossibleException(); } if (!_openContract2OpenConcrete.TryGetValue(openGenericContractType, out registrationData)) { return(false); } // check the generic constraints??? genericArgs = contractType.GetGenericArguments(); return(true); }
static RegistrationProvider CreateRegistrationProvider(ObjectRequestedEventArgs args, OpenGenericRegistrationData registrationData, Type[] genericArgs) { var registrationProviderType = typeof(ReflectionOrEmitRegistrationProvider <>).MakeGenericType(args.ContractType); TypedRegistrationProvider provider; try { var objProvider = Activator.CreateInstance(registrationProviderType, args.Kernel); provider = objProvider as TypedRegistrationProvider; if (provider == null) { throw new ImpossibleException(); } } catch (TargetInvocationException ex) { throw ex.InnerException; } provider.ActivatorKind = registrationData.ActivatorKind; provider.ContractType = registrationData.OpenGenericContractType.MakeGenericType(genericArgs); provider.ConcreteType = registrationData.OpenGenericConcreteType.MakeGenericType(genericArgs); if (registrationData.ConstructorSelector != null) { provider.ConstructorSelector = registrationData.ConstructorSelector; } if (registrationData.ConfiguredParameters != null) { provider.ConfiguredParameters = registrationData.ConfiguredParameters; } if (registrationData.LifetimeProvider != null) { provider.LifetimeProvider = registrationData.LifetimeProvider; } return(provider); }
static void RegisterNewService(ObjectRequestedEventArgs args, OpenGenericRegistrationData registrationData, Type[] genericArgs) { var provider = CreateRegistrationProvider(args, registrationData, genericArgs); args.Register(provider); }
public void Add(OpenGenericRegistrationData registrationData) { // Throw when the OpenGenericContractType already exists _openContract2OpenConcrete.Add(registrationData.OpenGenericContractType, registrationData); }