Esempio n. 1
0
        public ComposedPart(ComposablePartDefinition definition, IReadOnlyDictionary <ImportDefinitionBinding, IReadOnlyList <ExportDefinitionBinding> > satisfyingExports, IImmutableSet <string> requiredSharingBoundaries)
        {
            Requires.NotNull(definition, nameof(definition));
            Requires.NotNull(satisfyingExports, nameof(satisfyingExports));
            Requires.NotNull(requiredSharingBoundaries, nameof(requiredSharingBoundaries));

#if DEBUG   // These checks are expensive. Don't do them in production.
            // Make sure we have entries for every import.
            Requires.Argument(satisfyingExports.Count == definition.Imports.Count() && definition.Imports.All(d => satisfyingExports.ContainsKey(d)), "satisfyingExports", Strings.ExactlyOneEntryForEveryImport);
            Requires.Argument(satisfyingExports.All(kv => kv.Value != null), "satisfyingExports", Strings.AllValuesMustBeNonNull);
#endif

            this.Definition                = definition;
            this.SatisfyingExports         = satisfyingExports;
            this.RequiredSharingBoundaries = requiredSharingBoundaries;
        }
Esempio n. 2
0
            private ComposablePartDefinition ReadComposablePartDefinition()
            {
                using (this.Trace(nameof(ComposablePartDefinition)))
                {
                    var  partType             = this.ReadTypeRef() !;
                    var  partMetadata         = this.ReadMetadata();
                    var  exportedTypes        = this.ReadList(this.readExportDefinitionDelegate);
                    var  exportingMembers     = ImmutableDictionary.CreateBuilder <MemberRef, IReadOnlyCollection <ExportDefinition> >();
                    uint exportedMembersCount = this.ReadCompressedUInt();
                    for (int i = 0; i < exportedMembersCount; i++)
                    {
                        var member  = this.ReadMemberRef() !;
                        var exports = this.ReadList(this.readExportDefinitionDelegate);
                        exportingMembers.Add(member, exports);
                    }

                    var importingMembers = this.ReadList(this.readImportDefinitionBindingDelegate);
                    var sharingBoundary  = this.ReadString();
                    IReadOnlyList <MethodRef> onImportsSatisfiedMethods = this.ReadList(this.ReadMethodRef) !;

                    MethodRef?importingConstructor = default(MethodRef);
                    IReadOnlyList <ImportDefinitionBinding>?importingConstructorImports = null;
                    if (this.reader !.ReadBoolean())
                    {
                        importingConstructor        = this.ReadMethodRef();
                        importingConstructorImports = this.ReadList(this.readImportDefinitionBindingDelegate);
                    }

                    var creationPolicy            = this.ReadCreationPolicy();
                    var isSharingBoundaryInferred = this.reader.ReadBoolean();

                    var part = new ComposablePartDefinition(
                        partType,
                        partMetadata,
                        exportedTypes,
                        exportingMembers,
                        importingMembers,
                        sharingBoundary,
                        onImportsSatisfiedMethods,
                        importingConstructor,
                        importingConstructorImports,
                        creationPolicy,
                        isSharingBoundaryInferred);
                    return(part);
                }
            }
Esempio n. 3
0
        public static ComposableCatalog CreateAssembliesCatalog(params string[] assemblyNames)
        {
            var appDomainAssemblies = AppDomain.CurrentDomain.GetAssemblies();
            var loadedAssemblies    = new Assembly[assemblyNames.Length];

            for (var i = 0; i < assemblyNames.Length; i++)
            {
                loadedAssemblies[i] = GetLoadedAssembly(assemblyNames[i], appDomainAssemblies);
            }

            var types = new List <Type>();

            foreach (var assembly in loadedAssemblies)
            {
                types.AddRange(assembly.GetTypes());
            }

            var parts = new ComposablePartDefinition[types.Count];

            Parallel.For(0, types.Count, i => parts[i] = Discovery.CreatePart(types[i]));
            return(ComposableCatalog.Create(StandardResolver).AddParts(parts.Where(p => p != null)));
        }
Esempio n. 4
0
        public ComposableCatalog AddPart(ComposablePartDefinition partDefinition)
        {
            Requires.NotNull(partDefinition, nameof(partDefinition));

            var parts = this.parts.Add(partDefinition);

            if (parts == this.parts)
            {
                // This part is already in the catalog.
                return(this);
            }

            var typesBackingParts = this.typesBackingParts.Add(partDefinition.TypeRef);

            if (typesBackingParts == this.typesBackingParts)
            {
                Requires.Argument(false, nameof(partDefinition), Strings.TypeAlreadyInCatalogAsAnotherPart, partDefinition.TypeRef.FullName);
            }

            var exportsByContract = this.exportsByContract;

            foreach (var exportDefinition in partDefinition.ExportedTypes)
            {
                var list = exportsByContract.GetValueOrDefault(exportDefinition.ContractName, ImmutableList.Create <ExportDefinitionBinding>());
                exportsByContract = exportsByContract.SetItem(exportDefinition.ContractName, list.Add(new ExportDefinitionBinding(exportDefinition, partDefinition, default(MemberRef))));
            }

            foreach (var exportPair in partDefinition.ExportingMembers)
            {
                var member = exportPair.Key;
                foreach (var export in exportPair.Value)
                {
                    var list = exportsByContract.GetValueOrDefault(export.ContractName, ImmutableList.Create <ExportDefinitionBinding>());
                    exportsByContract = exportsByContract.SetItem(export.ContractName, list.Add(new ExportDefinitionBinding(export, partDefinition, member)));
                }
            }

            return(new ComposableCatalog(parts, exportsByContract, typesBackingParts, this.DiscoveredParts, this.Resolver));
        }
Esempio n. 5
0
        static NetFxAdapters()
        {
            var discovery = new AttributedPartDiscoveryV1(Resolver.DefaultInstance);

            CompositionServicePart = discovery.CreatePart(typeof(CompositionService));
        }
Esempio n. 6
0
        static NetFxAdapters()
        {
            var discovery = new AttributedPartDiscoveryV1(Resolver.DefaultInstance);

            CompositionServicePart = discovery.CreatePart(typeof(CompositionService)) ?? throw Assumes.NotReachable();
        }