Exemplo n.º 1
0
            private RuntimeComposition.RuntimeImport ReadRuntimeImport()
            {
                using (this.Trace("RuntimeImport"))
                {
                    var flags       = (RuntimeImportFlags)this.reader.ReadByte();
                    var cardinality =
                        flags.HasFlag(RuntimeImportFlags.CardinalityOneOrZero) ? ImportCardinality.OneOrZero :
                        flags.HasFlag(RuntimeImportFlags.CardinalityExactlyOne) ? ImportCardinality.ExactlyOne :
                        ImportCardinality.ZeroOrMore;
                    bool isExportFactory = flags.HasFlag(RuntimeImportFlags.IsExportFactory);

                    MemberRef    importingMember    = default(MemberRef);
                    ParameterRef importingParameter = default(ParameterRef);
                    if (flags.HasFlag(RuntimeImportFlags.IsParameter))
                    {
                        importingParameter = this.ReadParameterRef();
                    }
                    else
                    {
                        importingMember = this.ReadMemberRef();
                    }

                    var importingSiteTypeRef = this.ReadTypeRef();
                    var satisfyingExports    = this.ReadList(this.reader, this.ReadRuntimeExport);
                    var metadata             = this.ReadMetadata();
                    IReadOnlyList <string> exportFactorySharingBoundaries = isExportFactory
                        ? this.ReadList(this.reader, this.ReadString)
                        : ImmutableList <string> .Empty;

                    return(importingMember.IsEmpty
                        ? new RuntimeComposition.RuntimeImport(
                               importingParameter,
                               importingSiteTypeRef,
                               cardinality,
                               satisfyingExports,
                               flags.HasFlag(RuntimeImportFlags.IsNonSharedInstanceRequired),
                               isExportFactory,
                               metadata,
                               exportFactorySharingBoundaries)
                        : new RuntimeComposition.RuntimeImport(
                               importingMember,
                               importingSiteTypeRef,
                               cardinality,
                               satisfyingExports,
                               flags.HasFlag(RuntimeImportFlags.IsNonSharedInstanceRequired),
                               isExportFactory,
                               metadata,
                               exportFactorySharingBoundaries));
                }
            }
Exemplo n.º 2
0
        protected ParameterRef ReadParameterRef()
        {
            using (this.Trace(nameof(ParameterRef)))
            {
                if (this.TryPrepareDeserializeReusableObject(out uint id, out ParameterRef value))
                {
                    var method         = this.ReadMethodRef();
                    var parameterIndex = this.reader.ReadByte();
                    value = new ParameterRef(method, parameterIndex);

                    this.OnDeserializedReusableObject(id, value);
                }

                return(value);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ImportDefinitionBinding"/> class
        /// to represent a parameter in an importing constructor.
        /// </summary>
        public ImportDefinitionBinding(
            ImportDefinition importDefinition,
            TypeRef composablePartType,
            ParameterRef importingConstructorParameter,
            TypeRef importingSiteTypeRef,
            TypeRef importingSiteTypeWithoutCollectionRef)
        {
            Requires.NotNull(importDefinition, nameof(importDefinition));
            Requires.NotNull(composablePartType, nameof(composablePartType));
            Requires.NotNull(importingSiteTypeRef, nameof(importingSiteTypeRef));
            Requires.NotNull(importingSiteTypeWithoutCollectionRef, nameof(importingSiteTypeWithoutCollectionRef));

            this.ImportDefinition = importDefinition;
            this.ComposablePartTypeRef = composablePartType;
            this.ImportingParameterRef = importingConstructorParameter;
            this.ImportingSiteTypeRef = importingSiteTypeRef;
            this.ImportingSiteTypeWithoutCollectionRef = importingSiteTypeWithoutCollectionRef;
        }
Exemplo n.º 4
0
        private ParameterRef Scramble(ParameterRef importingParameterRef)
        {
            if (importingParameterRef.IsEmpty)
            {
                return(default(ParameterRef));
            }

            if (importingParameterRef.Constructor.IsEmpty)
            {
                return(new ParameterRef(
                           this.Scramble(importingParameterRef.Method),
                           importingParameterRef.ParameterIndex));
            }
            else
            {
                return(new ParameterRef(
                           this.Scramble(importingParameterRef.Constructor),
                           importingParameterRef.ParameterIndex));
            }
        }
Exemplo n.º 5
0
        public void EnsureNoArrangentIsAppliedToNonMatchingParameterInvocationFunc()
        {
            // Given
            var type                = typeof(IFooFuncValueTypeParameterRef <int>);
            var methodName          = nameof(IFooFuncValueTypeParameterRef <int> .MethodWithOneParameter);
            var signature           = type.GetMethod(methodName) ?? throw new MethodInfoException(type, methodName);
            var refParameterFeature = new ParameterRef(signature, new object?[] { 13 });
            var invocation          = new Invocation(signature, refParameterFeature);
            var arrangment          = new RefParameterArrangement <int>(signature, "WrongParameterName", 42);

            // When
            arrangment.ApplyTo(invocation);

            // Then
            Assert.True(invocation.HasFeature <IParameterRef>());
            var feature = invocation.GetFeature <IParameterRef>();

            Assert.Single(feature.RefParameterCollection);
            var parameter = feature.RefParameterCollection.Single();

            Assert.Equal(13, parameter.Value);
        }
Exemplo n.º 6
0
        public void CanApplyWithMatchingSignatureReturnsTrueFunc()
        {
            // Given
            var type                = typeof(IFooFuncValueTypeParameterRef <int>);
            var methodName          = nameof(IFooFuncValueTypeParameterRef <int> .MethodWithOneParameter);
            var signature           = type.GetMethod(methodName) ?? throw new MethodInfoException(type, methodName);
            var refParameterFeature = new ParameterRef(signature, new object?[] { 13 });
            var invocation          = new Invocation(signature, refParameterFeature);
            var arrangment          = new RefParameterArrangement <int>(signature, "first", 42);

            // When
            var canApply = arrangment.CanApplyTo(invocation);

            // Then
            Assert.True(canApply);
            Assert.True(invocation.HasFeature <IParameterRef>());
            var feature = invocation.GetFeature <IParameterRef>();

            Assert.Single(feature.RefParameterCollection);
            var parameter = feature.RefParameterCollection.Single();

            Assert.Equal(13, parameter.Value);
        }
        public void ApplySequenceOfReferenceTypeRefParametersArrangement()
        {
            // Given
            var value1              = new object();
            var value2              = new object();
            var value3              = new object();
            var type                = typeof(IFooActionReferenceTypeParameterRef <object>);
            var methodName          = nameof(IFooActionReferenceTypeParameterRef <object> .MethodWithOneParameter);
            var signature           = type.GetMethod(methodName) ?? throw new MethodInfoException(type, methodName);
            var refParameterFeature = new ParameterRef(signature, new object[] { 9 });
            var invocation          = new Invocation(signature, refParameterFeature);
            var arrangment          = new RefParameterSequenceArrangement <object?>(
                signature, "first", new List <object?> {
                value1, value2, value3
            });

            // When
            var feature = invocation.GetFeature <IParameterRef>();

            arrangment.ApplyTo(invocation);
            var first = feature.RefParameterCollection.First().Value;

            arrangment.ApplyTo(invocation);
            var second = feature.RefParameterCollection.First().Value;

            arrangment.ApplyTo(invocation);
            var third = feature.RefParameterCollection.First().Value;

            arrangment.ApplyTo(invocation);
            var fourth = feature.RefParameterCollection.First().Value;

            // Then
            Assert.Equal(value1, first);
            Assert.Equal(value2, second);
            Assert.Equal(value3, third);
            Assert.Equal(value3, fourth);
        }
Exemplo n.º 8
0
        public void ApplyValueTypeRefParameterArrangement()
        {
            // Given
            var type                = typeof(IFooActionValueTypeParameterRef <int>);
            var methodName          = nameof(IFooActionValueTypeParameterRef <int> .MethodWithOneParameter);
            var signature           = type.GetMethod(methodName) ?? throw new MethodInfoException(type, methodName);
            var refParameterFeature = new ParameterRef(signature, new object?[] { 13 });
            var invocation          = new Invocation(signature, refParameterFeature);
            var arrangment          = new RefParameterArrangement <int>(signature, "first", 42);

            // When
            arrangment.ApplyTo(invocation);

            // Then
            Assert.True(invocation.HasFeature <IParameterRef>());
            var feature = invocation.GetFeature <IParameterRef>();

            Assert.Single(feature.RefParameterCollection);
            var parameter = feature.RefParameterCollection.Single();

            Assert.Equal("first", parameter.Name);
            Assert.Equal(typeof(int), parameter.Type);
            Assert.Equal(42, parameter.Value);
        }
        private ImportDefinitionBinding CreateImport(ParameterInfo parameter, ImmutableHashSet <IImportSatisfiabilityConstraint> importConstraints)
        {
            ImportDefinition result;

            Assumes.True(this.TryCreateImportDefinition(parameter.ParameterType, parameter, importConstraints, out result));
            return(new ImportDefinitionBinding(result, TypeRef.Get(parameter.Member.DeclaringType, this.Resolver), ParameterRef.Get(parameter, this.Resolver)));
        }
Exemplo n.º 10
0
 public RuntimeImport(ParameterRef importingParameterRef, TypeRef importingSiteTypeRef, TypeRef importingSiteTypeWithoutCollectionRef, ImportCardinality cardinality, IReadOnlyList <RuntimeExport> satisfyingExports, bool isNonSharedInstanceRequired, bool isExportFactory, IReadOnlyDictionary <string, object> metadata, IReadOnlyCollection <string> exportFactorySharingBoundaries)
     : this(importingSiteTypeRef, importingSiteTypeWithoutCollectionRef, cardinality, satisfyingExports, isNonSharedInstanceRequired, isExportFactory, metadata, exportFactorySharingBoundaries)
 {
     this.ImportingParameterRef = importingParameterRef;
 }
Exemplo n.º 11
0
 public void Add(Element element)
 {
     element.Apply(
         onScoredProperty: x => _scoredProperties.Add(x),
         onProperty: x => _properties.Add(x),
         onParameterRef: x =>
         {
             ThrowIfValueOrParameterRefExists();
             _parameterRef = x;
         },
         onValue: x =>
         {
             ThrowIfValueOrParameterRefExists();
             _value = x;
         });
 }
Exemplo n.º 12
0
 public PrintSchemaParameterRef(ParameterName name)
 {
     _parameterRef = new ParameterRef(name);
 }
        private ImportDefinitionBinding CreateImport(ParameterInfo parameter)
        {
            ImportDefinition definition;

            Assumes.True(this.TryCreateImportDefinition(parameter.ParameterType, parameter, out definition));
            return(new ImportDefinitionBinding(definition, TypeRef.Get(parameter.Member.DeclaringType, this.Resolver), ParameterRef.Get(parameter, this.Resolver)));
        }
Exemplo n.º 14
0
        private static void Write(XmlWriter writer, ParameterRef element)
        {
            if (element == null) return;

            writer.WriteStartElement(Psf.ParameterRef.LocalName, Psf.Namespace.NamespaceName);
            writer.WriteAttributeString("name", element.Name.ToQName(writer));
            writer.WriteEndElement();
        }
Exemplo n.º 15
0
 public ValueOrParameterRef(ParameterRef paramterRef)
 {
     _content = paramterRef;
 }
Exemplo n.º 16
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ImportDefinitionBinding"/> class
        /// to represent a parameter in an importing constructor.
        /// </summary>
        public ImportDefinitionBinding(ImportDefinition importDefinition, TypeRef composablePartType, ParameterRef importingConstructorParameter)
        {
            Requires.NotNull(importDefinition, nameof(importDefinition));
            Requires.NotNull(composablePartType, nameof(composablePartType));

            this.ImportDefinition      = importDefinition;
            this.ComposablePartTypeRef = composablePartType;
            this.ImportingParameterRef = importingConstructorParameter;
        }
Exemplo n.º 17
0
        private static void Collect(HashSet<XNamespace> result, ParameterRef element)
        {
            if (element == null) return;

            result.AddXNamespaceOfXName(element.Name);
        }
Exemplo n.º 18
0
 public ImportDefinitionBinding(ImportDefinition importDefinition, TypeRef composablePartType, ParameterRef importingConstructorParameter)
     : this(
         importDefinition,
         composablePartType,
         importingConstructorParameter,
         TypeRef.Get(importingConstructorParameter.Resolve().ParameterType, importingConstructorParameter.Resolver),
         TypeRef.Get(importDefinition.Cardinality == ImportCardinality.ZeroOrMore ? PartDiscovery.GetElementTypeFromMany(importingConstructorParameter.Resolve().ParameterType) : importingConstructorParameter.Resolve().ParameterType, importingConstructorParameter.Resolver))
 {
 }