Exemplo n.º 1
0
 private static bool TryGetExportName(ExportDefinition export, [NotNullWhen(returnValue: true)] out string?name)
 {
     if (!export.Metadata.TryGetValue("Name", out var nameObj) ||
         nameObj is not string { Length : > 0 })
Exemplo n.º 2
0
        internal static object?GetExportedValueFromComposedPart(ImportEngine?engine, ComposablePart part, ExportDefinition definition)
        {
            if (engine != null)
            {
                try
                {
                    engine.SatisfyImports(part);
                }
                catch (CompositionException ex)
                {
                    throw ExceptionBuilder.CreateCannotGetExportedValue(part, definition, ex);
                }
            }

            try
            {
                return(part.GetExportedValue(definition));
            }
            catch (ComposablePartException ex)
            {
                throw ExceptionBuilder.CreateCannotGetExportedValue(part, definition, ex);
            }
        }
            public override bool IsConstraintSatisfiedBy(ExportDefinition exportDefinition)
            {
                Requires.NotNull(exportDefinition, nameof(exportDefinition));

                return(_sourceDefinition.IsConstraintSatisfiedBy(exportDefinition));
            }
 public override object GetExportedValue(ExportDefinition definition)
 {
     throw new NotImplementedException();
 }
 public override object GetExportedValue(ExportDefinition definition)
 {
     Assert.Fail();
     return(null);
 }
Exemplo n.º 6
0
 public PartCreatorExportDefinition(ExportDefinition productDefinition)
     : base()
 {
     _productDefinition = productDefinition;
 }
 public NonSharedCatalogExport(CatalogExportProvider catalogExportProvider,
                               ComposablePartDefinition partDefinition, ExportDefinition definition)
     : base(catalogExportProvider, partDefinition, definition)
 {
 }
Exemplo n.º 8
0
        static PartCreator <T> CreatePartCreatorOfTWithMetadata <T, TMetadata>(ImportDefinition productImport, ExportProvider sourceProvider, ExportDefinition productDefinition)
        {
            Func <PartLifetimeContext <T> > creator = CreatePartLifetimeContextCreator <T>(productImport, sourceProvider, productDefinition);

            return(new PartCreator <T, TMetadata>(creator, AttributedModelServices.GetMetadataView <TMetadata>(productDefinition.Metadata)));
        }
Exemplo n.º 9
0
        static PartCreator <T> CreatePartCreatorOfT <T>(ImportDefinition productImport, ExportProvider sourceProvider, ExportDefinition productDefinition)
        {
            Func <PartLifetimeContext <T> > creator = CreatePartLifetimeContextCreator <T>(productImport, sourceProvider, productDefinition);

            return(new PartCreator <T>(creator));
        }
Exemplo n.º 10
0
 public Export CreateMatchingExport(ExportDefinition exportDefinition, ExportProvider sourceProvider)
 {
     return(new Export(
                CreateMatchingExportDefinition(exportDefinition),
                () => CreatePartCreator(ExportedValueType, MetadataViewType, ProductImport, sourceProvider, exportDefinition)));
 }
Exemplo n.º 11
0
 static object CreatePartCreator(Type createdType, Type metadataViewType, ContractBasedImportDefinition productImport, ExportProvider sourceProvider, ExportDefinition productDefinition)
 {
     if (metadataViewType == null)
     {
         return(CreatePartCreatorMethod.MakeGenericMethod(createdType).Invoke(null, new object[] { productImport, sourceProvider, productDefinition }));
     }
     else
     {
         return(CreatePartCreatorWithMetadataMethod.MakeGenericMethod(createdType, metadataViewType).Invoke(null, new object[] { productImport, sourceProvider, productDefinition }));
     }
 }
Exemplo n.º 12
0
        public void Constructor2_NullAsMetadataArgument_ShouldSetMetadataPropertyToEmptyDictionary()
        {
            var definition = new ExportDefinition("Contract", (IDictionary <string, object>)null);;

            EnumerableAssert.IsEmpty(definition.Metadata);
        }
Exemplo n.º 13
0
 protected virtual bool IsMatch(ExportDefinition exportDefinition)
 {
     return(true);
 }
Exemplo n.º 14
0
 public override object GetExportedValue(ExportDefinition definition)
 {
     return(new CustomComposablePartProxy(Definition.ExportedInterface).GetTransparentProxy());
 }
Exemplo n.º 15
0
        public static CompositionError CreateCannotGetExportedValue(ComposablePart part, ExportDefinition definition, Exception innerException)
        {
            if (part == null)
            {
                throw new ArgumentNullException(nameof(part));
            }

            if (definition == null)
            {
                throw new ArgumentNullException(nameof(definition));
            }

            if (innerException == null)
            {
                throw new ArgumentNullException(nameof(innerException));
            }

            ICompositionElement element = definition.ToElement();

            return(CompositionError.Create(
                       CompositionErrorId.ImportEngine_PartCannotGetExportedValue,
                       element,
                       innerException,
                       SR.ImportEngine_PartCannotGetExportedValue,
                       element.DisplayName,
                       part.ToElement().DisplayName));
        }
Exemplo n.º 16
0
        static Func <PartLifetimeContext <T> > CreatePartLifetimeContextCreator <T>(ImportDefinition productImport, ExportProvider sourceProvider, ExportDefinition productDefinition)
        {
            Func <PartLifetimeContext <T> > creator = () =>
            {
                var product = sourceProvider.GetExports(productImport).Single(e => e.Definition == productDefinition);
                return(new PartLifetimeContext <T>((T)(product.Value), () =>
                {
                    if (product is IDisposable)
                    {
                        ((IDisposable)product).Dispose();
                    }
                }));
            };

            return(creator);
        }
Exemplo n.º 17
0
        private static ComposablePartDefinition CreatePartDefinition(IEnumerable <ImportDefinition> ctorImports, ExportDefinition contractExport, Type type)
        {
            ComposablePartDefinition originalPartDefinition = AttributedModelServices.CreatePartDefinition(type, null);

            if (originalPartDefinition == null)
            {
                throw new InvalidOperationException();
            }

            IList <ImportDefinition> imports = originalPartDefinition.ImportDefinitions
                                               .Where(idef => !ReflectionModelServices.IsImportingParameter(idef))
                                               .Concat(ctorImports)
                                               .ToList();

            IList <ExportDefinition> exports = originalPartDefinition.ExportDefinitions
                                               .Append(contractExport)
                                               .ToList();

            IDictionary <string, object> metadata = originalPartDefinition.Metadata;

            return(CreatePartDefinition(type, imports, exports, metadata));
        }
Exemplo n.º 18
0
 public ExportDefinition CreateMatchingExportDefinition(ExportDefinition productExportDefinition)
 {
     return(new ExportDefinition(ContractName, GetPartCreatorExportMetadata(productExportDefinition.Metadata)));
 }
Exemplo n.º 19
0
        internal static bool IsProductConstraintSatisfiedBy(ImportDefinition productImportDefinition, ExportDefinition exportDefinition)
        {
            object productValue = null;

            if (exportDefinition.Metadata.TryGetValue(CompositionConstants.ProductDefinitionMetadataName, out productValue))
            {
                ExportDefinition productDefinition = productValue as ExportDefinition;

                if (productDefinition != null)
                {
                    return(productImportDefinition.IsConstraintSatisfiedBy(productDefinition));
                }
            }

            return(false);
        }
Exemplo n.º 20
0
 static bool IsSharedInstance(ExportDefinition export)
 {
     return(IsSharedInstance(export.Metadata));
 }
            public static CatalogExport CreateExport(CatalogExportProvider catalogExportProvider,
                                                     ComposablePartDefinition partDefinition, ExportDefinition definition, CreationPolicy importCreationPolicy)
            {
                CreationPolicy partPolicy   = partDefinition.Metadata.GetValue <CreationPolicy>(CompositionConstants.PartCreationPolicyMetadataName);
                bool           isSharedPart = ShouldUseSharedPart(partPolicy, importCreationPolicy);

                if (isSharedPart)
                {
                    return(new CatalogExport(catalogExportProvider, partDefinition, definition));
                }
                else
                {
                    return(new NonSharedCatalogExport(catalogExportProvider, partDefinition, definition));
                }
            }
Exemplo n.º 22
0
        private IQueryable <ComposablePartDefinition> GetPartDefinitions(ExportDefinition definition)
        {
            var partDefinition = PartDefinitionFactory.Create(null, () => null, Enumerable.Empty <ImportDefinition>(), new ExportDefinition[] { definition });

            return(new ComposablePartDefinition[] { partDefinition }.AsQueryable());
        }
 static IEnumerable <Service> DefaultExposedServicesMapper(ExportDefinition ed)
 {
     yield return(MapService(ed));
 }
 private bool MatchesContract(ExportDefinition e, Type serviceType)
 {
     return(e.Metadata["ExportTypeIdentity"].ToString() == serviceType.FullName);
 }
Exemplo n.º 25
0
        public static CompositionError CreateCannotGetExportedValue(ComposablePart part, ExportDefinition definition, Exception innerException)
        {
            Assumes.NotNull(part, definition, innerException);

            ICompositionElement element = definition.ToElement();

            return(CompositionError.Create(
                       CompositionErrorId.ImportEngine_PartCannotGetExportedValue,
                       element,
                       innerException,
                       SR.ImportEngine_PartCannotGetExportedValue,
                       element.DisplayName,
                       part.ToElement().DisplayName));
        }
 internal ScopeFactoryExport(ScopeManager scopeManager, CompositionScopeDefinition catalog, ComposablePartDefinition partDefinition, ExportDefinition exportDefinition) :
     base(partDefinition, exportDefinition)
 {
     _scopeManager = scopeManager;
     _catalog      = catalog;
 }
 private Export CreateScopeExport(CompositionScopeDefinition childCatalog, ComposablePartDefinition partDefinition, ExportDefinition exportDefinition)
 {
     return(new ScopeFactoryExport(this, childCatalog, partDefinition, exportDefinition));
 }
Exemplo n.º 28
0
 public static ICompositionElement ToElement(this ExportDefinition definition)
 {
     return(ToElementCore(definition));
 }
Exemplo n.º 29
0
 private Export CreateExport(ComposablePart part, ExportDefinition export)
 {
     return(new Export(export, () => GetExportedValue(part, export)));
 }
Exemplo n.º 30
0
        public static ComposableCatalog WithDocumentTextDifferencingService(this ComposableCatalog catalog)
        {
            var assemblyQualifiedServiceTypeName = "Microsoft.CodeAnalysis.IDocumentTextDifferencingService, " + typeof(Workspace).GetTypeInfo().Assembly.GetName().ToString();

            // Check to see if IDocumentTextDifferencingService is exported
            foreach (var part in catalog.Parts)
            {
                foreach (var pair in part.ExportDefinitions)
                {
                    var exportDefinition = pair.Value;
                    if (exportDefinition.ContractName != "Microsoft.CodeAnalysis.Host.IWorkspaceService")
                    {
                        continue;
                    }

                    if (!exportDefinition.Metadata.TryGetValue("ServiceType", out var value) ||
                        !(value is string serviceType))
                    {
                        continue;
                    }

                    if (serviceType != assemblyQualifiedServiceTypeName)
                    {
                        continue;
                    }

                    // The service is exported by default
                    return(catalog);
                }
            }

            // If IDocumentTextDifferencingService is not exported by default, export it manually
            var manualExportDefinition = new ExportDefinition(
                typeof(IWorkspaceService).FullName,
                metadata: new Dictionary <string, object?>
            {
                { "ExportTypeIdentity", typeof(IWorkspaceService).FullName },
                { nameof(ExportWorkspaceServiceAttribute.ServiceType), assemblyQualifiedServiceTypeName },
                { nameof(ExportWorkspaceServiceAttribute.Layer), ServiceLayer.Default },
                { typeof(CreationPolicy).FullName, CreationPolicy.Shared },
                { "ContractType", typeof(IWorkspaceService) },
                { "ContractName", null },
            });

            var serviceImplType = typeof(Workspace).GetTypeInfo().Assembly.GetType("Microsoft.CodeAnalysis.DefaultDocumentTextDifferencingService");

            return(catalog.AddPart(new ComposablePartDefinition(
                                       TypeRef.Get(serviceImplType, Resolver.DefaultInstance),
                                       new Dictionary <string, object?> {
                { "SharingBoundary", null }
            },
                                       new[] { manualExportDefinition },
                                       new Dictionary <MemberRef, IReadOnlyCollection <ExportDefinition> >(),
                                       Enumerable.Empty <ImportDefinitionBinding>(),
                                       sharingBoundary: string.Empty,
                                       default(MethodRef),
                                       MethodRef.Get(serviceImplType.GetConstructors(BindingFlags.Instance | BindingFlags.Public).First(), Resolver.DefaultInstance),
                                       new List <ImportDefinitionBinding>(),
                                       CreationPolicy.Shared,
                                       new[] { typeof(Workspace).GetTypeInfo().Assembly.GetName() },
                                       isSharingBoundaryInferred: false)));
        }