コード例 #1
0
ファイル: ReflectionHelpers.cs プロジェクト: sharwell/vs-mef
        internal static ImmutableArray <TypeRef> GetGenericTypeArguments(this MethodBase methodBase, Resolver resolver)
        {
            Requires.NotNull(methodBase, nameof(methodBase));

            return((methodBase as MethodInfo)?.GetGenericArguments()?.Select(t => TypeRef.Get(t, resolver)).ToImmutableArray()
                   ?? ImmutableArray <TypeRef> .Empty);
        }
コード例 #2
0
        private static RuntimeImport CreateRuntimeImport(ImportDefinitionBinding importDefinitionBinding, IReadOnlyList <ExportDefinitionBinding> satisfyingExports, Resolver resolver)
        {
            Requires.NotNull(importDefinitionBinding, nameof(importDefinitionBinding));
            Requires.NotNull(satisfyingExports, nameof(satisfyingExports));

            var runtimeExports = satisfyingExports.Select(export => CreateRuntimeExport(export, resolver)).ToImmutableArray();

            if (!importDefinitionBinding.ImportingMemberRef.IsEmpty)
            {
                return(new RuntimeImport(
                           importDefinitionBinding.ImportingMemberRef,
                           importDefinitionBinding.ImportingSiteTypeRef,
                           importDefinitionBinding.ImportDefinition.Cardinality,
                           runtimeExports,
                           PartCreationPolicyConstraint.IsNonSharedInstanceRequired(importDefinitionBinding.ImportDefinition),
                           importDefinitionBinding.IsExportFactory,
                           importDefinitionBinding.ImportDefinition.Metadata,
                           importDefinitionBinding.ImportDefinition.ExportFactorySharingBoundaries));
            }
            else
            {
                return(new RuntimeImport(
                           importDefinitionBinding.ImportingParameterRef,
                           importDefinitionBinding.ImportingSiteTypeRef,
                           importDefinitionBinding.ImportDefinition.Cardinality,
                           runtimeExports,
                           PartCreationPolicyConstraint.IsNonSharedInstanceRequired(importDefinitionBinding.ImportDefinition),
                           importDefinitionBinding.IsExportFactory,
                           importDefinitionBinding.ImportDefinition.Metadata,
                           importDefinitionBinding.ImportDefinition.ExportFactorySharingBoundaries));
            }
        }
コード例 #3
0
 internal SerializationContext(BinaryWriter writer, int estimatedObjectCount, Resolver resolver)
     : base(writer, estimatedObjectCount, resolver)
 {
 }
コード例 #4
0
ファイル: ReflectionHelpers.cs プロジェクト: sharwell/vs-mef
 internal static ImmutableArray <TypeRef> GetParameterTypes(this MethodBase method, Resolver resolver)
 {
     Requires.NotNull(method, nameof(method));
     return(method.GetParameters().Select(pi => TypeRef.Get(pi.ParameterType, resolver)).ToImmutableArray());
 }
コード例 #5
0
        private static ImmutableDictionary <string, MetadatumRequirement> GetRequiredMetadata(TypeRef metadataViewRef, Resolver resolver)
        {
            Requires.NotNull(metadataViewRef, nameof(metadataViewRef));
            Requires.NotNull(resolver, nameof(resolver));

            var  metadataView = metadataViewRef.Resolve();
            bool hasMetadataViewImplementation = MetadataViewImplProxy.HasMetadataViewImplementation(metadataView);

            if (metadataView.GetTypeInfo().IsInterface&& !metadataView.Equals(typeof(IDictionary <string, object>)) && !metadataView.Equals(typeof(IReadOnlyDictionary <string, object>)))
            {
                var requiredMetadata = ImmutableDictionary.CreateBuilder <string, MetadatumRequirement>();

                foreach (var property in metadataView.EnumProperties().WherePublicInstance())
                {
                    bool required = !property.IsAttributeDefined <DefaultValueAttribute>();

                    // Ignore properties that have a default value and have a metadataview implementation.
                    if (required || !hasMetadataViewImplementation)
                    {
                        requiredMetadata.Add(property.Name, new MetadatumRequirement(TypeRef.Get(ReflectionHelpers.GetMemberType(property), resolver), required));
                    }
                }

                return(requiredMetadata.ToImmutable());
            }

            return(ImmutableDictionary <string, MetadatumRequirement> .Empty);
        }
コード例 #6
0
 internal SerializationContext(BinaryReader reader, Resolver resolver)
     : base(reader, resolver)
 {
 }
コード例 #7
0
        private RuntimeComposition(IEnumerable <RuntimePart> parts, IReadOnlyDictionary <TypeRef, RuntimeExport> metadataViewsAndProviders, Resolver resolver)
        {
            Requires.NotNull(parts, nameof(parts));
            Requires.NotNull(metadataViewsAndProviders, nameof(metadataViewsAndProviders));
            Requires.NotNull(resolver, nameof(resolver));

            var invalidArgExceptionMsg  = "Invalid arguments passed to generate runtime composition.";
            var invalidArgExceptionData = "InvalidCompositionException";

            if (!parts.Any())
            {
                var exception = new ArgumentException(invalidArgExceptionMsg, nameof(parts));
                exception.Data.Add(invalidArgExceptionData, true);

                throw exception;
            }

            if (!metadataViewsAndProviders.Any())
            {
                var exception = new ArgumentException(invalidArgExceptionMsg, nameof(metadataViewsAndProviders));
                exception.Data.Add(invalidArgExceptionData, true);

                throw exception;
            }

            this.parts = ImmutableHashSet.CreateRange(parts);
            this.metadataViewsAndProviders = metadataViewsAndProviders;
            this.Resolver = resolver;

            this.partsByType = this.parts.ToDictionary(p => p.TypeRef, this.parts.Count);

            var exports =
                from part in this.parts
                from export in part.Exports
                group export by export.ContractName into exportsByContract
                select exportsByContract;

            this.exportsByContractName = exports.ToDictionary(
                e => e.Key,
                e => (IReadOnlyCollection <RuntimeExport>)e.ToImmutableArray());
        }
コード例 #8
0
        private static RuntimeExport CreateRuntimeExport(ExportDefinitionBinding exportDefinitionBinding, Resolver resolver)
        {
            Requires.NotNull(exportDefinitionBinding, nameof(exportDefinitionBinding));
            Requires.NotNull(resolver, nameof(resolver));

            var partDefinitionType = exportDefinitionBinding.PartDefinition.TypeRef.Resolve();

            return(CreateRuntimeExport(
                       exportDefinitionBinding.ExportDefinition,
                       partDefinitionType,
                       exportDefinitionBinding.ExportingMemberRef,
                       resolver));
        }
コード例 #9
0
        internal LazyMetadataWrapper(ImmutableDictionary <string, object?> metadata, Direction direction, Resolver resolver)
        {
            Requires.NotNull(metadata, nameof(metadata));
            Requires.NotNull(resolver, nameof(resolver));

            this.direction          = direction;
            this.underlyingMetadata = metadata;
            this.resolver           = resolver;
        }
コード例 #10
0
        private static RuntimeExport CreateRuntimeExport(ExportDefinition exportDefinition, TypeRef partTypeRef, MemberRef?exportingMemberRef, Resolver resolver)
        {
            Requires.NotNull(exportDefinition, nameof(exportDefinition));

            return(new RuntimeExport(
                       exportDefinition.ContractName,
                       partTypeRef,
                       exportingMemberRef,
                       exportDefinition.Metadata));
        }
コード例 #11
0
        /// <summary>
        /// Attempts to get the types of metadata values without loading the associated assemblies.
        /// </summary>
        /// <param name="metadata">Medatadata dictionary to retrieve values from.</param>
        /// <param name="key">key for the metadata value.</param>
        /// <param name="resolver">Type resolver to use.</param>
        /// <param name="value">out param for the TypeRef associated with the given key.</param>
        /// <returns>Either a TypeRef representing the type of the underlying value or a TypeRef[] if the underlying value was an array.</returns>
        internal static bool TryGetLoadSafeValueTypeRef(IReadOnlyDictionary <string, object?> metadata, string key, Resolver resolver, out object?value)
        {
            Requires.NotNull(metadata, nameof(metadata));

            if (metadata is LazyMetadataWrapper lazyMetadata && lazyMetadata.direction == Direction.ToOriginalValue)
            {
                metadata = lazyMetadata.underlyingMetadata;
            }

            if (!metadata.TryGetValue(key, out object?innerValue))
            {
                value = null;
                return(false);
            }

            if (innerValue is ISubstitutedValue substitutedValue)
            {
                value = substitutedValue.SubstitutedValueTypeRef;
            }
            else if (innerValue != null && typeof(object[]).IsEquivalentTo(innerValue.GetType()))
            {
                value = ((object[])innerValue).Select(v => TypeRef.Get(v?.GetType(), resolver)).ToArray();
            }
            else
            {
                value = TypeRef.Get(innerValue?.GetType(), resolver);
            }

            return(true);
        }
コード例 #12
0
 public AttributedPartDiscoveryV1(Resolver resolver)
     : base(resolver)
 {
 }
コード例 #13
0
        /// <summary>
        /// Creates a constraint for the specified metadata type.
        /// </summary>
        /// <param name="metadataTypeRef">The metadata type.</param>
        /// <param name="resolver">The assembly loader.</param>
        /// <returns>A constraint to match the metadata type.</returns>
        public static ImportMetadataViewConstraint GetConstraint(TypeRef metadataTypeRef, Resolver resolver)
        {
            if (metadataTypeRef == null)
            {
                return(EmptyInstance);
            }

            var requirements = GetRequiredMetadata(metadataTypeRef, resolver);

            if (requirements.IsEmpty)
            {
                return(EmptyInstance);
            }

            return(new ImportMetadataViewConstraint(requirements, resolver));
        }
コード例 #14
0
        private static RuntimeExport CreateRuntimeExport(ExportDefinition exportDefinition, Type partType, MemberRef exportingMemberRef, Resolver resolver)
        {
            Requires.NotNull(exportDefinition, nameof(exportDefinition));

            var exportingMember = exportingMemberRef.Resolve();

            return(new RuntimeExport(
                       exportDefinition.ContractName,
                       TypeRef.Get(partType, resolver),
                       exportingMemberRef,
                       TypeRef.Get(ReflectionHelpers.GetExportedValueType(partType, exportingMember), resolver),
                       exportDefinition.Metadata));
        }
コード例 #15
0
        public Task <RuntimeComposition> LoadRuntimeCompositionAsync(Stream cacheStream, Resolver resolver, CancellationToken cancellationToken = default(CancellationToken))
        {
            Requires.NotNull(cacheStream, nameof(cacheStream));
            Requires.Argument(cacheStream.CanRead, "cacheStream", Strings.ReadableStreamRequired);
            Requires.NotNull(resolver, nameof(resolver));

            return(Task.Run(() =>
            {
                using (var reader = new BinaryReader(cacheStream, TextEncoding, leaveOpen: true))
                {
                    var context = new SerializationContext(reader, resolver);
                    var runtimeComposition = context.ReadRuntimeComposition();
                    return runtimeComposition;
                }
            }));
        }
コード例 #16
0
        private RuntimeComposition(IEnumerable <RuntimePart> parts, IReadOnlyDictionary <TypeRef, RuntimeExport> metadataViewsAndProviders, Resolver resolver)
        {
            Requires.NotNull(parts, nameof(parts));
            Requires.NotNull(metadataViewsAndProviders, nameof(metadataViewsAndProviders));
            Requires.NotNull(resolver, nameof(resolver));

            this.parts = ImmutableHashSet.CreateRange(parts);
            this.metadataViewsAndProviders = metadataViewsAndProviders;
            this.Resolver = resolver;

            this.partsByType = this.parts.ToDictionary(p => p.TypeRef, this.parts.Count);

            var exports =
                from part in this.parts
                from export in part.Exports
                group export by export.ContractName into exportsByContract
                select exportsByContract;

            this.exportsByContractName = exports.ToDictionary(
                e => e.Key,
                e => (IReadOnlyCollection <RuntimeExport>)e.ToImmutableArray());
        }
コード例 #17
0
        public async Task <IExportProviderFactory> LoadExportProviderFactoryAsync(Stream cacheStream, Resolver resolver, CancellationToken cancellationToken = default(CancellationToken))
        {
            var runtimeComposition = await this.LoadRuntimeCompositionAsync(cacheStream, resolver, cancellationToken);

            return(runtimeComposition.CreateExportProviderFactory());
        }
コード例 #18
0
 public static RuntimeComposition CreateRuntimeComposition(IEnumerable <RuntimePart> parts, IReadOnlyDictionary <TypeRef, RuntimeExport> metadataViewsAndProviders, Resolver resolver)
 {
     return(new RuntimeComposition(parts, metadataViewsAndProviders, resolver));
 }
コード例 #19
0
        internal SerializationContextBase(BinaryWriter writer, int estimatedObjectCount, Resolver resolver)
        {
            Requires.NotNull(writer, nameof(writer));
            Requires.NotNull(resolver, nameof(resolver));

            this.writer = writer;
            this.serializingObjectTable = new Dictionary <object, uint>(estimatedObjectCount, SmartInterningEqualityComparer.Default);
            this.Resolver = resolver;
#if TRACESTATS
            this.sizeStats = new Dictionary <string, int>();
#endif

            // Don't use compressed uint here. It must be a fixed size because we *may*
            // come back and rewrite this at the end of serialization if this stream is seekable.
            // Otherwise, we'll leave it at our best estimate given the size of the data being serialized.
            Stream writerStream = writer.BaseStream;
            this.objectTableCapacityStreamPosition = writerStream.CanSeek ? writer.BaseStream.Position : -1;
            this.writer.Write(estimatedObjectCount);
        }