public static CompositionBatch AddValue <TContract>(this CompositionBatch batch, TContract value)
        {
            string contractName = AttributedModelServices.GetContractName(typeof(TContract));

            return(batch.AddValue(contractName, value));
        }
Exemple #2
0
        public void CreatePart_ObjectInstance_ShouldProduceSharedPart()
        {
            var part = AttributedModelServices.CreatePart(typeof(MyExport));

            Assert.Equal(CreationPolicy.Shared, part.Metadata.GetValue <CreationPolicy>(CompositionConstants.PartCreationPolicyMetadataName));
        }
 /// <summary>
 /// GetServices
 /// </summary>
 /// <param name="serviceType"></param>
 /// <returns></returns>
 public IEnumerable <object> GetServices(Type serviceType)
 {
     return(Container.GetExportedValues <object>(AttributedModelServices.GetContractName(serviceType)));
 }
Exemple #4
0
        protected override IEnumerable <object> DoGetAllInstances(Type serviceType)
        {
            var exports = provider.GetExportedValues <object>(AttributedModelServices.GetContractName(serviceType));

            return(exports);
        }
 private static string[] GetDisplayNames(IEnumerable <Type> types)
 {
     return(GetDisplayNames(types.Select(t => AttributedModelServices.CreatePartDefinition(t, null))));
 }
 /// <summary>
 /// Registers a type with the export provider.
 /// </summary>
 /// <param name="type">The type to register.</param>
 public void RegisterType(Type type)
 {
     contractMapping.Add(AttributedModelServices.GetContractName(type), type);
 }
 /// <summary>
 /// Export the component under typed contract <typeparamref name="TContract"/>.
 /// </summary>
 /// <typeparam name="TContract">Contract type.</typeparam>
 /// <returns>Builder for additional configuration.</returns>
 public ExportConfigurationBuilder As <TContract>()
 {
     WithMetadata(CompositionConstants.ExportTypeIdentityMetadataName, AttributedModelServices.GetTypeIdentity(typeof(TContract)));
     ContractName = AttributedModelServices.GetContractName(typeof(TContract));
     return(this);
 }
 /// <summary>
 /// Locate all of the MEF exports registered as supplying contract type T.
 /// </summary>
 /// <typeparam name="T">The contract type.</typeparam>
 /// <param name="context">The context to resolve exports from.</param>
 /// <returns>A list of exports.</returns>
 public static IEnumerable <Export> ResolveExports <T>(this IComponentContext context)
 {
     return(context.ResolveExports <T>(AttributedModelServices.GetContractName(typeof(T))));
 }
Exemple #9
0
        public static IEnumerable <Lazy <T, TMetadata> > GetAllInstances <T, TMetadata>(string key = null)
        {
            var contract = string.IsNullOrEmpty(key) ? AttributedModelServices.GetContractName(typeof(T)) : key;

            return(container.GetExports <T, TMetadata>(contract));
        }
 public ExportToSettingsAttribute(Type contractType)
     : base(AttributedModelServices.GetContractName(contractType), typeof(ISettings))
 {
 }
Exemple #11
0
        public static void RegisterType(this CompositionContainer container, Type interfaceType, Type implementingType)
        {
            string key = AttributedModelServices.GetContractName(interfaceType);

            container.ComposeExportedValue(key, Activator.CreateInstance(implementingType));
        }
 /// <summary>
 /// Exports the class to the SettingsRoot.
 /// </summary>
 public ExportToSettingsAttribute()
     : base(AttributedModelServices.GetContractName(typeof(SettingsRoot)), typeof(ISettings))
 {
 }
Exemple #13
0
        public object GetType(Type type)
        {
            var contractName = AttributedModelServices.GetContractName(type);

            return(_container.GetExportedValueOrDefault <object>(contractName));
        }
Exemple #14
0
        protected virtual IEnumerable <object> GetInstances(System.Type type)
        {
            var contractName = AttributedModelServices.GetContractName(type);

            return(this.container.GetExportedValues <object>(contractName));
        }
Exemple #15
0
 public MefSubstituteBuilder AddValueFactory <T1, T2, T3, T4, TValue>(string contractName, Func <T1, T2, T3, T4, TValue> valueFactory)
 {
     return(AddValueFactory(contractName, AttributedModelServices.CreatePartDefinition(typeof(ImportStub <T1, T2, T3, T4>), null), typeof(TValue), valueFactory));
 }
Exemple #16
0
        // Protected Methods (2) 

        /// <summary>
        ///
        /// </summary>
        /// <see cref="ServiceLocatorBase.OnGetAllInstances(Type)" />
        protected override IEnumerable <object> OnGetAllInstances(Type serviceType)
        {
            return(this._PROVIDER
                   .GetExportedValues <object>(AttributedModelServices.GetContractName(serviceType)));
        }
Exemple #17
0
 static ExportServiceHostProvider()
 {
     MatchContractName = AttributedModelServices.GetContractName(typeof(ExportServiceHost <T>));
 }
Exemple #18
0
 public static ComposablePart CreateAttributed(object instance)
 {
     return(AttributedModelServices.CreatePart(instance));
 }
Exemple #19
0
        /// <summary>
        /// Builds the given parameter.
        /// </summary>
        /// <param name="parameter"></param>
        /// <param name="builder"></param>
        void BuildParameter(ParameterInfo parameter, ImportBuilder builder)
        {
            Debug.Assert(parameter != null);
            Debug.Assert(builder != null);

            var name = parameter.ParameterType.FullName;

            {
                // decides whether the parameter is attempting to import many items

                Type importManyType = null;

                if (parameter.ParameterType.IsGenericType &&
                    parameter.ParameterType.GetGenericTypeDefinition() == typeof(IEnumerable <>))
                {
                    importManyType = parameter.ParameterType.GetGenericArguments()[0];
                }

                if (parameter.ParameterType.IsGenericType &&
                    parameter.ParameterType.GetGenericTypeDefinition() == typeof(ICollection <>))
                {
                    importManyType = parameter.ParameterType.GetGenericArguments()[0];
                }

                if (parameter.ParameterType.IsArray)
                {
                    importManyType = parameter.ParameterType.GetElementType();
                }

                if (importManyType != null)
                {
                    builder.AsContractType(importManyType);
                    builder.AsMany(true);
                    builder.AllowDefault();
                    return;
                }
            }

            {
                // decides whether the parameter is attempting to import a Func<T>; essentially an ExportFactory

                Type funcType = null;

                if (parameter.ParameterType.IsGenericType &&
                    parameter.ParameterType.GetGenericArguments().Length == 1 &&
                    parameter.ParameterType.GetGenericTypeDefinition() == typeof(Func <>))
                {
                    funcType = parameter.ParameterType.GetGenericArguments()[0];
                }

                if (funcType != null)
                {
                    // import a Func generated by our FuncFactory
                    var contractType = typeof(Func <>).MakeGenericType(funcType);
                    var contractName = "FuncFactory:" + AttributedModelServices.GetContractName(typeof(Func <>));
                    builder.AsContractType(contractType);
                    builder.AsContractName(contractName);
                    builder.AsMany(false);
                    builder.AllowDefault();
                    return;
                }
            }

            // fall back to normal method
            builder.AsContractType(parameter.ParameterType);
            builder.AsMany(false);
            return;
        }
Exemple #20
0
        public static ImportingComposablePart CreateImporter <T>()
        {
            string contractName = AttributedModelServices.GetContractName(typeof(T));

            return(CreateImporter(contractName));
        }
Exemple #21
0
            private ComposablePartDefinition CreatePartDefinition(Type type)
            {
                ComposablePartDefinition partDefinition = AttributedModelServices.CreatePartDefinition(type, null);

                return(this.CreateWrapped(partDefinition, type));
            }
        protected override object GetInstance(Type serviceType, string key)
        {
            string contract = string.IsNullOrEmpty(key) ? AttributedModelServices.GetContractName(serviceType) : key;

            return(_container.GetExportedValue <object>(contract));
        }
        /// <summary>
        /// Extension method to searches the composition container for an export that has a matching type name. This function
        /// will first try to match on Type.AssemblyQualifiedName, then Type.FullName, and finally on Type.Name
        ///
        /// This method will not throw if multiple types are found matching the name, it will just return the first one it finds.
        /// </summary>
        /// <typeparam name="T">The type of the export</typeparam>
        /// <param name="typeName">The name of the type to find. This can be an assembly qualified name, a full name, or just the type's name</param>
        /// <returns>The export instance</returns>
        public T GetExportedValueByTypeName <T>(string typeName)
            where T : class
        {
            try
            {
                lock (_exportedValuesLockObject)
                {
                    T           instance;
                    IEnumerable values;
                    var         type = typeof(T);
                    if (_exportedValues.TryGetValue(type, out values))
                    {
                        // if we've alread loaded this part, then just return the same one
                        instance = values.OfType <T>().FirstOrDefault(x => x.GetType().MatchesTypeName(typeName));
                        if (instance != null)
                        {
                            return(instance);
                        }
                    }

                    // we want to get the requested part without instantiating each one of that type
                    var selectedPart = _compositionContainer.Catalog.Parts
                                       .Select(x => new { part = x, Type = ReflectionModelServices.GetPartType(x).Value })
                                       .Where(x => type.IsAssignableFrom(x.Type))
                                       .Where(x => x.Type.MatchesTypeName(typeName))
                                       .Select(x => x.part)
                                       .FirstOrDefault();

                    if (selectedPart == null)
                    {
                        throw new ArgumentException(
                                  "Unable to locate any exports matching the requested typeName: " + typeName, "typeName");
                    }

                    var exportDefinition =
                        selectedPart.ExportDefinitions.First(
                            x => x.ContractName == AttributedModelServices.GetContractName(type));
                    instance = (T)selectedPart.CreatePart().GetExportedValue(exportDefinition);

                    var exportedParts = instance.GetType().GetInterfaces()
                                        .Where(interfaceType => interfaceType.GetCustomAttribute <InheritedExportAttribute>() != null);

                    foreach (var export in exportedParts)
                    {
                        var exportList = _exportedValues.SingleOrDefault(kvp => kvp.Key == export).Value;

                        // cache the new value for next time
                        if (exportList == null)
                        {
                            var list = (IList)Activator.CreateInstance(typeof(List <>).MakeGenericType(export));
                            list.Add(instance);
                            _exportedValues[export] = list;
                        }
                        else
                        {
                            ((IList)exportList).Add(instance);
                        }
                    }

                    return(instance);
                }
            }
            catch (ReflectionTypeLoadException err)
            {
                foreach (var exception in err.LoaderExceptions)
                {
                    Log.Error(exception);
                    Log.Error(exception.ToString());
                }

                if (err.InnerException != null)
                {
                    Log.Error(err.InnerException);
                }

                throw;
            }
        }
Exemple #24
0
        private object GetServiceFromCompositionContainer(Type type)
        {
            var contract = AttributedModelServices.GetContractName(type);

            return(_compositionContainer.GetExportedValueOrDefault <object>(contract));
        }
 public static ImportDefinition Create(Type contractType, ImportCardinality cardinality)
 {
     return(Create(AttributedModelServices.GetContractName(contractType), cardinality));
 }
Exemple #26
0
        /// <summary>
        /// Extension method to searches the composition container for an export that has a matching type name. This function
        /// will first try to match on Type.AssemblyQualifiedName, then Type.FullName, and finally on Type.Name
        ///
        /// This method will not throw if multiple types are found matching the name, it will just return the first one it finds.
        /// </summary>
        /// <typeparam name="T">The type of the export</typeparam>
        /// <param name="typeName">The name of the type to find. This can be an assembly qualified name, a full name, or just the type's name</param>
        /// <returns>The export instance</returns>
        public T GetExportedValueByTypeName <T>(string typeName)
            where T : class
        {
            lock (_exportedValuesLockObject)
            {
                T           instance;
                IEnumerable values;
                var         type = typeof(T);
                if (_exportedValues.TryGetValue(type, out values))
                {
                    // if we've alread loaded this part, then just reserve the same one
                    instance = values.OfType <T>().FirstOrDefault(x => x.GetType().MatchesTypeName(typeName));
                    if (instance != null)
                    {
                        return(instance);
                    }
                }

                // we want to get the requested part without instantiating each one of that type
                var selectedPart = _compositionContainer.Catalog.Parts
                                   .Select(x => new { part = x, Type = ReflectionModelServices.GetPartType(x).Value })
                                   .Where(x => type.IsAssignableFrom(x.Type))
                                   .Where(x => x.Type.MatchesTypeName(typeName))
                                   .Select(x => x.part)
                                   .FirstOrDefault();

                if (selectedPart == null)
                {
                    throw new ArgumentException("Unable to locate any exports matching the requested typeName: " + typeName, "typeName");
                }

                var exportDefinition = selectedPart.ExportDefinitions.First(x => x.ContractName == AttributedModelServices.GetContractName(type));
                instance = (T)selectedPart.CreatePart().GetExportedValue(exportDefinition);

                // cache the new value for next time
                if (values == null)
                {
                    values = new List <T> {
                        instance
                    };
                    _exportedValues[type] = values;
                }
                else
                {
                    ((List <T>)values).Add(instance);
                }

                return(instance);
            }
        }
Exemple #27
0
        /// <summary>
        /// GetService
        /// </summary>
        /// <param name="serviceType"></param>
        /// <returns></returns>
        public object GetService(Type serviceType)
        {
            string contractName = AttributedModelServices.GetContractName(serviceType);

            return(Container.GetExportedValueOrDefault <object>(contractName));
        }
 public LooseMetadataLazy(Func <T> factory, IDictionary <string, object> looseMetadata)
     : base(factory, AttributedModelServices.GetMetadataView <TMetadata>(looseMetadata))
 {
     LooseMetadata = looseMetadata;
 }
 protected override IEnumerable <object> GetAllInstances(Type serviceType)
 {
     return(container.GetExportedValues <object>(AttributedModelServices.GetContractName(serviceType)));
 }
 /// <summary>
 /// Creates a catalog for the contract specified by the given type.
 /// </summary>
 public DecoratorChainCatalog(Type contract)
     : this(AttributedModelServices.GetContractName(contract))
 {
 }