Example #1
0
 private ExportDefinition CreateWrapped(ExportDefinition export, Type type)
 {
     return(ReflectionModelServices.CreateExportDefinition(
                this.CreateWrapped(ReflectionModelServices.GetExportingMember(export), type),
                export.ContractName,
                export.Metadata.AsLazy(),
                null));
 }
Example #2
0
 internal static ExportDefinition CreateExportDefinition(SerializableExportDefinition serializableExportDefinition)
 {
     return(ReflectionModelServices.CreateExportDefinition(
                CreateLazyMemberInfo(serializableExportDefinition.ExportingMember),
                serializableExportDefinition.ContractName,
                new Lazy <IDictionary <string, object> >(() => serializableExportDefinition.Metadata),
                null)); // TODO: Is it OK to have null for origin?
 }
Example #3
0
        public static T GetExport <T>(this object obj, string propertyName, string contractName)
        {
            LazyMemberInfo info = new LazyMemberInfo(obj.GetType().GetProperty(propertyName));
            var            ed   = ReflectionModelServices.CreateExportDefinition(info, contractName, null, null);
            var            ed1  = ReflectionModelServices.GetExportingMember(ed);
            T someService       = (T)(ed1.GetAccessors().GetValue(0) as System.Reflection.MethodInfo).Invoke(obj, null);

            return((T)someService);
        }
        /// <summary>
        /// Creates <see cref="ExportDefinition"/> instance from the provided <see cref="IExportConvention"/> instances and type.
        /// </summary>
        /// <param name="exportConventions">An <see cref="IEnumerable{T}"/> of <see cref="IExportConvention"/> instances that should be used to create the <see cref="ExportDefinition"/> instances.</param>
        /// <param name="type">The <see cref="Type"/> for which the <see cref="ExportDefinition"/> instances should be created.</param>
        /// <returns>An <see cref="IEnumerable{T}"/> of <see cref="ExportDefinition"/> instances.</returns>
        private IEnumerable <ExportDefinition> CreateExportDefinitions(IEnumerable <IExportConvention> exportConventions, Type type)
        {
            var exportDefinitionsFromConvention =
                from exportConvention in exportConventions
                from member in exportConvention.Members.Invoke(type)
                select ReflectionModelServices.CreateExportDefinition(
                    member.ToLazyMemberInfo(),
                    this.ContractService.GetExportContractName(exportConvention, member),
                    new Lazy <IDictionary <string, object> >(() => GetExportDefinitionMetadata(exportConvention, member)),
                    null);

            return(exportDefinitionsFromConvention.ToList());
        }
        private static ExportDefinition CreateExportDefinition(string contractName, Type type)
        {
            LazyMemberInfo memberInfo = new LazyMemberInfo(MemberTypes.TypeInfo, type);

            Lazy <IDictionary <string, object> > metadata = new Lazy <IDictionary <string, object> >(() => {
                string typeIdentity = AttributedModelServices.GetTypeIdentity(type);
                return(new Dictionary <string, object> {
                    { CompositionConstants.ExportTypeIdentityMetadataName, typeIdentity }
                });
            });

            ExportDefinition exportDefinition = ReflectionModelServices.CreateExportDefinition(memberInfo, contractName, metadata, null);

            return(exportDefinition);
        }
Example #6
0
        private IEnumerable <ExportDefinition> GetExportDefinitions(Type implementationType, Type contractType)
        {
            var lazyMember  = new LazyMemberInfo(implementationType);
            var contracName = AttributedModelServices.GetContractName(contractType);
            var metadata    = new Lazy <IDictionary <string, object> >(() =>
            {
                var md = new Dictionary <string, object>();
                md.Add(CompositionConstants.ExportTypeIdentityMetadataName, AttributedModelServices.GetTypeIdentity(contractType));
                return(md);
            });

            return(new []
            {
                ReflectionModelServices.CreateExportDefinition(lazyMember, contracName, metadata, null)
            });
        }
        private void RewriteContract(Type typeToDecorate, IList <ExportDefinition> exportDefs, Type exportingType, string newContract)
        {
            var exportToDecorate = exportDefs.Single(d => d.ContractName == myContractName);

            Contract.Requires(exportToDecorate != null, "No import found for contract {0} on type {1}", myContractName, typeToDecorate);

            exportDefs.Remove(exportToDecorate);

            var exportDef = ReflectionModelServices.CreateExportDefinition(
                new LazyMemberInfo(exportingType),
                newContract,
                new Lazy <IDictionary <string, object> >(() => exportToDecorate.Metadata),
                null);

            exportDefs.Add(exportDef);
        }
        public static ExportDefinition ReadExportDefinition(ComposablePartDefinition owner, IDictionary <string, object> cache)
        {
            Assumes.NotNull(owner);
            Assumes.NotNull(cache);

            LazyMemberInfo exportingMemberInfo = new LazyMemberInfo(
                cache.ReadValue <MemberTypes>(AttributedCacheServices.CacheKeys.MemberType, MemberTypes.TypeInfo),
                cache.ReadLazyAccessors(ReflectionModelServices.GetPartType(owner)));


            return(ReflectionModelServices.CreateExportDefinition(
                       exportingMemberInfo,
                       cache.ReadContractName(),
                       cache.ReadLazyMetadata(),
                       owner as ICompositionElement));
        }
        public AddInComposablePartDefinition(AddInCatalog catalog, ComposablePartDefinition composablePartDefinition)
        {
            this._composablePartDefinition = composablePartDefinition;

            List <KeyValuePair <string, object> > injectedMetadata = new List <KeyValuePair <string, object> >();

            injectedMetadata.Add(new KeyValuePair <string, object>(AddInCatalog.PackageIdMetadataName, catalog.PackageId));
            injectedMetadata.Add(new KeyValuePair <string, object>(AddInCatalog.PackageVersionMetadataName,
                                                                   catalog.PackageVersion));

            List <ExportDefinition> interceptedExports = new List <ExportDefinition>();

            foreach (ExportDefinition export in composablePartDefinition.ExportDefinitions)
            {
                ICompositionElement compositionElement = export as ICompositionElement;
                if (compositionElement == null)
                {
                    throw new InvalidOperationException("ExportDefinition doesn't implement ICompositionElement");
                }

                Dictionary <string, object> metadata = injectedMetadata.Concat(export.Metadata)
                                                       .ToDictionary(kvp => kvp.Key,
                                                                     kvp => kvp.Value);

                // TODO this will fail if export isn't a ReflectionMemberExportDefinition (Internal, so I can't check)
                LazyMemberInfo lazyMember = ReflectionModelServices.GetExportingMember(export);

                ExportDefinition interceptedExport =
                    ReflectionModelServices.CreateExportDefinition(lazyMember,
                                                                   export.ContractName,
                                                                   new Lazy <IDictionary <string, object> >(() => metadata),
                                                                   compositionElement.Origin);
                interceptedExports.Add(interceptedExport);
            }

            this._exportDefinitions = interceptedExports.ToArray();
        }
 private IEnumerable <ComposablePartDefinition> BuildParts(IQueryable <ComposablePartDefinition> parts)
 {
     return(parts.Select(def => ReflectionModelServices.CreatePartDefinition(
                             ReflectionModelServices.GetPartType(def),
                             true,
                             new Lazy <IEnumerable <ImportDefinition> >(() => def.ImportDefinitions),
                             new Lazy <IEnumerable <ExportDefinition> >(() => def.ExportDefinitions.Select(export =>
                                                                                                           ReflectionModelServices.CreateExportDefinition(
                                                                                                               ReflectionModelServices.GetExportingMember(export),
                                                                                                               export.ContractName,
                                                                                                               new Lazy <IDictionary <string, object> >(() => VisitExport(def, export)),
                                                                                                               this))),
                             new Lazy <IDictionary <string, object> >(() => VisitPart(def)),
                             this)));
 }
Example #11
0
        private ComposablePartDefinition RewritePart(ComposablePartDefinition definition)
        {
            var closedGenericContractType = ContractType.MakeGenericType(ExportingType.GetGenericArguments());
            var closedGenericTypeIdentity = AttributedModelServices.GetTypeIdentity(closedGenericContractType);
            var openGenericTypeIdentity   = AttributedModelServices.GetTypeIdentity(ContractType);
            var openGenericContractName   = AttributedModelServices.GetContractName(ContractType);
            var exports             = new List <ExportDefinition>();
            var hasRewrittenExports = false;

            foreach (var exportDefinition in definition.ExportDefinitions)
            {
                // Rewrite only exports having open-generics type identity of the contract type we do care about
                if (openGenericTypeIdentity == (string)exportDefinition.Metadata[CompositionConstants.ExportTypeIdentityMetadataName])
                {
                    // If both open-generic contract name and the present contract name are equal,
                    // contract name has to be rewritten to form a closed-generic contract
                    var contractName = openGenericContractName == exportDefinition.ContractName
                                           ? AttributedModelServices.GetContractName(closedGenericContractType)
                                           : exportDefinition.ContractName;

                    // Preserve all the metadata except the type identity as it has to be rewritten
                    var metadata = new Dictionary <string, object>
                    {
                        { CompositionConstants.ExportTypeIdentityMetadataName, closedGenericTypeIdentity }
                    };
                    foreach (var key in exportDefinition.Metadata.Keys.Where(key => key != CompositionConstants.ExportTypeIdentityMetadataName))
                    {
                        metadata.Add(key, exportDefinition.Metadata[key]);
                    }

                    // Rewrite the export
                    var rewrittenExport = ReflectionModelServices.CreateExportDefinition(
                        ReflectionModelServices.GetExportingMember(exportDefinition),
                        contractName,
                        new Lazy <IDictionary <string, object> >(() => metadata),
                        exportDefinition as ICompositionElement);

                    exports.Add(rewrittenExport);
                    hasRewrittenExports = true;
                }
                else
                {
                    // This export is ok, copy the original
                    exports.Add(exportDefinition);
                }
            }

            // If the part has any rewritten exports, we have to rewrite the part itself
            if (hasRewrittenExports)
            {
                return(ReflectionModelServices.CreatePartDefinition(
                           ReflectionModelServices.GetPartType(definition),
                           ReflectionModelServices.IsDisposalRequired(definition),
                           new Lazy <IEnumerable <ImportDefinition> >(() => definition.ImportDefinitions),
                           new Lazy <IEnumerable <ExportDefinition> >(() => exports),
                           new Lazy <IDictionary <string, object> >(() => definition.Metadata),
                           definition as ICompositionElement));
            }

            return(definition);
        }