private List <object> ApplyInitializationPoints(object originalComponentInstance) { var initializationPointResults = new List <object>(); foreach (var initializationPoint in _initializationPoints) { if (initializationPoint.Query == null) { throw new CompositionException( $"Query is null for initialization point '{initializationPoint.Name}' on component instance of type '{_targetType.FullName}'"); } var initializationPointResult = initializationPoint.Query.Query(_composer); // Check if the required initialization points get a value. if (initializationPoint.Required && initializationPointResult == null) { throw new CompositionException(string.Format("Could not fill initialization point '{0}' of type '{1}'.", initializationPoint.Name, _targetType.FullName)); } initializationPointResults.Add(initializationPointResult); ComponentContextUtils.ApplyInitializationPoint(originalComponentInstance, initializationPoint.Name, initializationPoint.MemberType, initializationPointResult); } return(initializationPointResults); }
public IEnumerable <object> GetServices(Type serviceType) { var baseResult = _baseResolver.GetServices(serviceType); baseResult.ToList().ForEach(o => Composer.InitializePlugs(o, o.GetType())); return(ComponentContextUtils.HasContractAttribute(serviceType) ? Composer.GetAllComponents(serviceType).Concat(baseResult) : baseResult); }
private static void ExtractAllComponents(Assembly assembly, IComponentContext context) { Type[] types; try { types = assembly.GetTypes(); } catch (ReflectionTypeLoadException rtle) { var message = "Could not load types of assembly '" + assembly.FullName + "', with the following messages: \n"; message = rtle.LoaderExceptions.Aggregate(message, (current, exception) => current + (exception.Message + "\n")); throw new CompositionException(message); } var candidateTypes = types .Where(ComponentContextUtils.HasComponentAttribute) .Where(componentType => !ComponentContextUtils.HasIgnoredOnAssemblyRegistrationAttribute(componentType)); foreach (var componentType in candidateTypes) { context.Register(componentType); } }
public void EmptyInterfaceWithAdditionalAttributes() { var attributeBuilder = new CustomAttributeBuilder( typeof(ComponentAttribute).GetConstructor(Type.EmptyTypes), new object[0]); var di = _classEmitter.EmitInterfaceInstance <IEmpty>(_dth, null, null, new[] { attributeBuilder }); Assert.IsNotNull(di); Assert.IsTrue(ComponentContextUtils.HasComponentAttribute(di.GetType())); }
private void ExtractContractTypes() { var boundGenericContracts = ComponentContextUtils.FindContracts(_targetType) .Where(t => t.IsOpenGenericType()); foreach (var boundGenericContract in boundGenericContracts) { var openContract = boundGenericContract.GetGenericTypeDefinition(); _contractTypes.Add(openContract, boundGenericContract); } }
private void ExtractContractTypes() { var openContracts = ComponentContextUtils.FindContracts(_targetType) .Where(t => t.ContainsGenericParameters && t.IsGenericType); foreach (var openContract in openContracts) { _contractTypes.Add(openContract.GetGenericTypeDefinition(), openContract); } if (_contractTypes.Count < 1) { throw new CompositionException("No open contracts found on the type " + _targetType.Name); } }
private void LoadInitializationPoints() { // Check two categories of members for being an initialization point: // 1. Public fields // 2. Public properties // Check and add them to the list of initialization points if they // are not already registered. foreach (var fieldInfo in _targetType.GetFields()) { ComponentContextUtils.CheckAndAddInitializationPoint(_composer, _initializationPoints, fieldInfo); } foreach (var fieldInfo in _targetType.GetProperties()) { ComponentContextUtils.CheckAndAddInitializationPoint(_composer, _initializationPoints, fieldInfo); } }
public object GetService(Type serviceType) { object result = null; if (ComponentContextUtils.HasContractAttribute(serviceType)) { result = Composer.GetComponent(serviceType); } if (result == null) { result = _baseResolver.GetService(serviceType); if (result != null) { Composer.InitializePlugs(result, result.GetType()); } } return(result); }
public void Initialize(IComposer composer) { if (_composer != null) { return; } if (_targetType == null) { throw new InvalidOperationException("TargetType is not specified."); } if (!composer.Configuration.DisableAttributeChecking && !ComponentContextUtils.HasComponentAttribute(_targetType)) { throw new CompositionException("The type '" + _targetType + "' is not a component, but it is being registered as one. Only classes marked with [Component] attribute can be registered."); } _composer = composer; CompleteConfiguration(); }
private void LoadComponentCache() { var attribute = ComponentContextUtils.GetComponentCacheAttribute(_targetType); if (attribute == null) { _componentCache = _composer.GetComponent <DefaultComponentCache>(); return; } if (attribute.ComponentCacheType == null) { _componentCache = null; return; } var result = _composer.GetComponent(attribute.ComponentCacheType, attribute.ComponentCacheName); if (result == null) { throw new CompositionException("Can not register component type " + _targetType.FullName + " because the specified ComponentCache contract (type=" + attribute.ComponentCacheType.FullName + ", name=" + (attribute.ComponentCacheName ?? "null") + ") could not be queried from Composer."); } if (!(result is IComponentCache)) { throw new CompositionException("Component cache type " + result.GetType().FullName + " that is specified as component cache handler on component " + _targetType.FullName + " does not implement IComponentCache interface."); } _componentCache = (IComponentCache)result; }
public IEnumerable <Type> GetContractTypes() { return(ComponentContextUtils.FindContracts(_componentInstance.GetType())); }
public IEnumerable <Type> GetContractTypes() { return(ComponentContextUtils.FindContracts(_targetType)); }
private void LoadCompositionNotificationMethods() { _compositionNotificationMethods = ComponentContextUtils.FindCompositionNotificationMethods(_targetType); }
private void LoadTargetConstructor() { // If the constructor arguments are specified by the creator of the factory, // ignore finding the constructor. The constructor to be used will be bound // when creating the component. if (_constructorArgs != null) { _targetConstructor = null; return; } // Ignore finding the constructor if the creator of the factory has specified one. // Just extract the constructor args. if (_targetConstructor == null) { // Find the appropriate constructor for instantiating the component. // Order of precedence: // 1. The one marked with [CompositionConstructor] // 2. If there's a single public constructor, use it. // 3. If there's the default constructor, use it. // And it is an exception if none of the above is found. var candidateConstructors = _targetType.GetConstructors(); _targetConstructor = FindMarkedConstructor(_targetType, candidateConstructors) ?? FindSingleConstructor(candidateConstructors) ?? FindDefaultConstructor(candidateConstructors); if (_targetConstructor == null) { throw new CompositionException( "There's no appropriate constructor identified as the composition constructor for type '" + _targetType.FullName + "'" + "You can fix this by using [CompositionConstructor] attribute on the constructor that you intend to be used by Composer."); } } _constructorArgs = new List <ConstructorArgSpecification>(); string[] queryNames = null; if (ComponentContextUtils.HasCompositionConstructorAttribute(_targetConstructor)) { queryNames = ComponentContextUtils.GetCompositionConstructorAttribute(_targetConstructor).Names; } foreach (var parameterInfo in _targetConstructor.GetParameters()) { if (!ComponentContextUtils.HasContractAttribute(parameterInfo.ParameterType)) { throw new CompositionException( $"Parameter '{parameterInfo.Name}' of the constructor of type '{_targetType.FullName}' is not of a Contract type. " + "All parameters of the composition constructor must be of Contract types, so that Composer can query for a component and pass it to them."); } if ((queryNames != null) && (queryNames.Length > parameterInfo.Position)) { _constructorArgs.Add(new ConstructorArgSpecification(true, new ComponentQuery(parameterInfo.ParameterType, queryNames[parameterInfo.Position]))); } else { _constructorArgs.Add(new ConstructorArgSpecification(true, new ComponentQuery(parameterInfo.ParameterType, null))); } } if ((queryNames != null) && (queryNames.Length > _constructorArgs.Count)) { throw new CompositionException("Extra names are specified for the constructor of type '" + _targetType.FullName + "'"); } }
public FluentLocalComponentConfig(ComponentContext context, Type componentType) { Context = context ?? throw new ArgumentNullException(nameof(context)); Factory = (LocalComponentFactory)ComponentContextUtils.CreateLocalFactory(componentType); }