/// <summary>
        /// Creates a new instance of a part that the <see cref="T:System.ComponentModel.Composition.Primitives.ComposablePartDefinition"/> describes.
        /// </summary>
        /// <returns>The created part.</returns>
        public override ComposablePart CreatePart()
        {
            ComposablePart part     = null;
            ComposablePart tempPart = null;

            try
            {
                tempPart = ReflectionModelServices.IsDisposalRequired(this._sourcePart)
                               ? new DisposableIsolatingComposablePart(this._sourcePart, this._isolationMetadata)
                               : new IsolatingComposablePart(this._sourcePart, this._isolationMetadata);

                part     = tempPart;
                tempPart = null;
            }
            finally
            {
                if (tempPart != null)
                {
                    var disposablePart = (tempPart as IDisposable);
                    if (disposablePart != null)
                    {
                        disposablePart.Dispose();
                    }
                }
            }

            return(part);
        }
Exemplo n.º 2
0
        public override ComposablePart CreatePart()
        {
            var interceptedPart = InterceptedPartDefinition.CreatePart();

            return(ReflectionModelServices.IsDisposalRequired(InterceptedPartDefinition)
                       ? new DisposableInterceptingComposablePart(interceptedPart, this.valueInterceptor)
                       : new InterceptingComposablePart(interceptedPart, this.valueInterceptor));
        }
Exemplo n.º 3
0
 public static SerializableComposablePartDefinition FromComposablePartDefinition(ComposablePartDefinition composablePartDefinition)
 {
     return(new SerializableComposablePartDefinition()
     {
         PartTypeAssemblyQualifiedName = ReflectionModelServices.GetPartType(composablePartDefinition).Value.AssemblyQualifiedName,
         IsDisposalRequired = ReflectionModelServices.IsDisposalRequired(composablePartDefinition),
         ImportDefinitions = composablePartDefinition.ImportDefinitions.Select(importDefinition => new SerializableImportDefinition(importDefinition)).ToList(),
         ExportDefinitions = composablePartDefinition.ExportDefinitions.Select(exportDefinition => new SerializableExportDefinition(exportDefinition)).ToList(),
         Metadata = new SerializableDictionary <string, object>(composablePartDefinition.Metadata),
     });
 }
        public override ComposablePart CreatePart()
        {
            var innerPart = _innerPartDefinition.CreatePart();

            if (ReflectionModelServices.IsDisposalRequired(_innerPartDefinition) &&
                _innerPartDefinition.Metadata.ContainsKey(CompositionConstants.PartCreationPolicyMetadataName) &&
                (CreationPolicy)_innerPartDefinition.Metadata[CompositionConstants.PartCreationPolicyMetadataName] == CreationPolicy.NonShared)
            {
                return(new NonDisposableComposablePart(innerPart));
            }
            return(innerPart);
        }
Exemplo n.º 5
0
            private ComposablePartDefinition CreateWrapped(ComposablePartDefinition partDefinition, Type type)
            {
                IEnumerable <ExportDefinition> exports = partDefinition.ExportDefinitions.Select(e => this.CreateWrapped(e, type)).ToArray();
                IEnumerable <ImportDefinition> imports = partDefinition.ImportDefinitions.Cast <ContractBasedImportDefinition>().Select(i => this.CreateWrapped(i, type)).ToArray();

                return(ReflectionModelServices.CreatePartDefinition(
                           this.CreateWrapped(ReflectionModelServices.GetPartType(partDefinition), type),
                           ReflectionModelServices.IsDisposalRequired(partDefinition),
                           imports.AsLazy(),
                           exports.AsLazy(),
                           partDefinition.Metadata.AsLazy(),
                           null));
            }
Exemplo n.º 6
0
        private void ComposeDecoration()
        {
            if (myParts.Any())
            {
                return;
            }

            var contracts = new List <string>();

            foreach (var type in myDecoratorChain)
            {
                var originalPart = AttributedModelServices.CreatePartDefinition(type, null);

                var importDefs = originalPart.ImportDefinitions.ToList();

                if (type != myDecoratorChain.First())
                {
                    RewriteContract(type, importDefs, contracts.Last());
                }

                var exportDefs = originalPart.ExportDefinitions.ToList();

                if (type != myDecoratorChain.Last())
                {
                    contracts.Add(Guid.NewGuid().ToString());
                    RewriteContract(type, exportDefs, type, contracts.Last());
                }

                // as we pass it to lazy below we have to copy it to local variable - otherwise we create a closure with the loop iterator variable
                // and this will cause the actual part type to be changed
                var partType = type;
                var part     = ReflectionModelServices.CreatePartDefinition(
                    new Lazy <Type>(() => partType),
                    ReflectionModelServices.IsDisposalRequired(originalPart),
                    new Lazy <IEnumerable <ImportDefinition> >(() => importDefs),
                    new Lazy <IEnumerable <ExportDefinition> >(() => exportDefs),
                    new Lazy <IDictionary <string, object> >(() => new Dictionary <string, object>()),
                    null);

                myParts.Add(part);
            }

            // no add possible any longer
            myDecoratorChain = null;
        }
Exemplo n.º 7
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);
        }
        public static IDictionary <string, object> WritePartDefinition(ComposablePartDefinition part, bool writeAssembly)
        {
            Assumes.NotNull(part);

            IDictionary <string, object> cache = new Dictionary <string, object>();
            Lazy <Type> partType = ReflectionModelServices.GetPartType(part);

            cache.WriteMetadata(part.Metadata);
            cache.WriteLazyTypeForPart(partType, writeAssembly);
            cache.WriteValue <bool>(AttributedCacheServices.CacheKeys.IsDisposalRequired, ReflectionModelServices.IsDisposalRequired(part), false);

            return(cache);
        }