Beispiel #1
0
        public void Constructor_DisposablePart()
        {
            Type        expectedType     = typeof(TestPart);
            Lazy <Type> expectedLazyType = expectedType.AsLazy();
            IDictionary <string, object> expectedMetadata = new Dictionary <string, object>();

            expectedMetadata["Key1"] = 1;
            expectedMetadata["Key2"] = "Value2";

            IEnumerable <ImportDefinition> expectedImports = CreateImports(expectedType);
            IEnumerable <ExportDefinition> expectedExports = CreateExports(expectedType);

            ICompositionElement expectedOrigin = new MockOrigin();

            ReflectionComposablePartDefinition definition = CreateReflectionPartDefinition(
                expectedLazyType,
                true,
                () => expectedImports,
                () => expectedExports,
                expectedMetadata,
                expectedOrigin);

            Assert.Same(expectedType, definition.GetPartType());
            Assert.True(definition.Metadata.Keys.SequenceEqual(expectedMetadata.Keys));
            Assert.True(definition.Metadata.Values.SequenceEqual(expectedMetadata.Values));
            Assert.True(definition.ExportDefinitions.SequenceEqual(expectedExports.Cast <ExportDefinition>()));
            Assert.True(definition.ImportDefinitions.SequenceEqual(expectedImports.Cast <ImportDefinition>()));
            Assert.Same(expectedOrigin, ((ICompositionElement)definition).Origin);
            Assert.NotNull(((ICompositionElement)definition).DisplayName);
            Assert.True(definition.IsDisposalRequired);
        }
        public GenericSpecializationPartCreationInfo(IReflectionPartCreationInfo originalPartCreationInfo, ReflectionComposablePartDefinition originalPart, Type[] specialization)
        {
            if (originalPartCreationInfo == null)
            {
                throw new ArgumentNullException(nameof(originalPartCreationInfo));
            }

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

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

            _originalPartCreationInfo = originalPartCreationInfo;
            _originalPart             = originalPart;
            _specialization           = specialization;
            _specializationIdentities = new string[_specialization.Length];
            for (int i = 0; i < _specialization.Length; i++)
            {
                _specializationIdentities[i] = AttributedModelServices.GetTypeIdentity(_specialization[i]);
            }
            _lazyPartType = new Lazy <Type>(
                () => _originalPartCreationInfo.GetPartType().MakeGenericType(specialization),
                LazyThreadSafetyMode.PublicationOnly);
        }
Beispiel #3
0
        public void Constructor_NullMetadata_ShouldSetMetadataPropertyToEmpty()
        {
            ReflectionComposablePartDefinition definition = CreateEmptyDefinition(typeof(object), typeof(object).GetConstructors().First(), null, new MockOrigin());

            Assert.NotNull(definition.Metadata);
            Assert.Equal(0, definition.Metadata.Count);
        }
Beispiel #4
0
        public void CreatePart_DoesntLoadType()
        {
            Type        expectedType     = typeof(TestPart);
            Lazy <Type> expectedLazyType = new Lazy <Type>(() => { throw new NotImplementedException(); /*"Part should not be loaded" */ });
            IDictionary <string, object> expectedMetadata = new Dictionary <string, object>();

            expectedMetadata["Key1"] = 1;
            expectedMetadata["Key2"] = "Value2";

            IEnumerable <ImportDefinition> expectedImports = CreateImports(expectedType);
            IEnumerable <ExportDefinition> expectedExports = CreateExports(expectedType);

            ICompositionElement expectedOrigin = new MockOrigin();

            ReflectionComposablePartDefinition definition = CreateReflectionPartDefinition(
                expectedLazyType,
                true,
                () => expectedImports,
                () => expectedExports,
                expectedMetadata,
                expectedOrigin);

            var part = definition.CreatePart();

            Assert.NotNull(part);
            Assert.True(part is IDisposable);
        }
Beispiel #5
0
        public void Constructor_NullOrigin_ShouldSetOriginPropertyToNull()
        {
            ReflectionComposablePartDefinition definition = CreateEmptyDefinition(typeof(object), typeof(object).GetConstructors().First(), MetadataServices.EmptyMetadata, null);

            Assert.NotNull(((ICompositionElement)definition).DisplayName);
            Assert.Null(((ICompositionElement)definition).Origin);
        }
Beispiel #6
0
        public void CreatePart()
        {
            Type        expectedType     = typeof(TestPart);
            Lazy <Type> expectedLazyType = expectedType.AsLazy();
            IDictionary <string, object> expectedMetadata = new Dictionary <string, object>();

            expectedMetadata["Key1"] = 1;
            expectedMetadata["Key2"] = "Value2";

            IEnumerable <ImportDefinition> expectedImports = CreateImports(expectedType);
            IEnumerable <ExportDefinition> expectedExports = CreateExports(expectedType);

            ICompositionElement expectedOrigin = new MockOrigin();

            ReflectionComposablePartDefinition definition = CreateReflectionPartDefinition(
                expectedLazyType,
                false,
                () => expectedImports,
                () => expectedExports,
                expectedMetadata,
                expectedOrigin);

            var part = definition.CreatePart();

            Assert.NotNull(part);
            Assert.False(part is IDisposable);
        }
        public override bool Equals(object obj)
        {
            ReflectionComposablePartDefinition that = obj as ReflectionComposablePartDefinition;

            if (that == null)
            {
                return(false);
            }

            return(this._creationInfo.Equals(that._creationInfo));
        }
        internal bool TryMakeGenericPartDefinition(Type[] genericTypeParameters, out ComposablePartDefinition genericPartDefinition)
        {
            genericPartDefinition = null;

            if (!GenericSpecializationPartCreationInfo.CanSpecialize(Metadata, genericTypeParameters))
            {
                return(false);
            }

            genericPartDefinition = new ReflectionComposablePartDefinition(new GenericSpecializationPartCreationInfo(_creationInfo, this, genericTypeParameters));
            return(true);
        }
Beispiel #9
0
        public static bool IsDisposalRequired(ComposablePartDefinition partDefinition)
        {
            Requires.NotNull(partDefinition, "partDefinition");

            ReflectionComposablePartDefinition reflectionPartDefinition = partDefinition as ReflectionComposablePartDefinition;

            if (reflectionPartDefinition == null)
            {
                throw ExceptionBuilder.CreateReflectionModelInvalidPartDefinition("partDefinition", partDefinition.GetType());
            }

            return(reflectionPartDefinition.IsDisposalRequired);
        }
        public static Lazy <Type> GetPartType(ComposablePartDefinition partDefinition)
        {
            Requires.NotNull(partDefinition, nameof(partDefinition));

            ReflectionComposablePartDefinition reflectionPartDefinition = partDefinition as ReflectionComposablePartDefinition;

            if (reflectionPartDefinition == null)
            {
                throw ExceptionBuilder.CreateReflectionModelInvalidPartDefinition(nameof(partDefinition), partDefinition.GetType());
            }

            return(reflectionPartDefinition.GetLazyPartType());
        }
        public ReflectionComposablePart(ReflectionComposablePartDefinition definition, object attributedPart)
        {
            Requires.NotNull(definition, nameof(definition));
            Requires.NotNull(attributedPart, nameof(attributedPart));

            _definition = definition;

            if (attributedPart is ValueType)
            {
                throw new ArgumentException(SR.ArgumentValueType, nameof(attributedPart));
            }
            _cachedInstance = attributedPart;
        }
Beispiel #12
0
        public ReflectionComposablePart(ReflectionComposablePartDefinition definition, object attributedPart)
        {
            Requires.NotNull(definition, "definition");
            Requires.NotNull(attributedPart, "attributedPart");

            this._definition = definition;

            if (attributedPart is ValueType)
            {
                throw new ArgumentException(Strings.ArgumentValueType, "attributedPart");
            }
            this._cachedInstance = attributedPart;
        }
Beispiel #13
0
        public static bool TryMakeGenericPartDefinition(ComposablePartDefinition partDefinition, IEnumerable <Type> genericParameters, out ComposablePartDefinition specialization)
        {
            Requires.NotNull(partDefinition, "partDefinition");

            specialization = null;
            ReflectionComposablePartDefinition reflectionPartDefinition = partDefinition as ReflectionComposablePartDefinition;

            if (reflectionPartDefinition == null)
            {
                throw ExceptionBuilder.CreateReflectionModelInvalidPartDefinition("partDefinition", partDefinition.GetType());
            }

            return(reflectionPartDefinition.TryMakeGenericPartDefinition(genericParameters.ToArray(), out specialization));
        }
Beispiel #14
0
        public static Lazy <Type> GetPartType(ComposablePartDefinition partDefinition)
        {
            Requires.NotNull(partDefinition, "partDefinition");
            Contract.Ensures(Contract.Result <Lazy <Type> >() != null);

            ReflectionComposablePartDefinition reflectionPartDefinition = partDefinition as ReflectionComposablePartDefinition;

            if (reflectionPartDefinition == null)
            {
                throw ExceptionBuilder.CreateReflectionModelInvalidPartDefinition("partDefinition", partDefinition.GetType());
            }

            return(reflectionPartDefinition.GetLazyPartType());
        }
Beispiel #15
0
        public static bool IsDisposalRequired(ComposablePartDefinition partDefinition)
        {
            Requires.NotNull(partDefinition, "partDefinition");

            ReflectionComposablePartDefinition reflectionPartDefinition = partDefinition as ReflectionComposablePartDefinition;

            if (reflectionPartDefinition == null)
            {
                throw new ArgumentException(
                          string.Format(CultureInfo.CurrentCulture, Strings.ReflectionModel_InvalidPartDefinition, partDefinition.GetType()),
                          "partDefinition");
            }

            return(reflectionPartDefinition.IsDisposalRequired);
        }
        public static Lazy <Type> GetPartType(ComposablePartDefinition partDefinition)
        {
            Requires.NotNull(partDefinition, "partDefinition");
            Contract.Ensures(Contract.Result <Lazy <Type> >() != null);

            ReflectionComposablePartDefinition reflectionPartDefinition = partDefinition as ReflectionComposablePartDefinition;

            if (reflectionPartDefinition == null)
            {
                throw new ArgumentException(
                          string.Format(CultureInfo.CurrentCulture, Strings.ReflectionModel_InvalidPartDefinition, partDefinition.GetType()),
                          "partDefinition");
            }

            return(reflectionPartDefinition.GetLazyPartType());
        }
        public void ImportaAndExports_CreatorsShouldBeCalledLazilyAndOnce()
        {
            Type expectedType = typeof(TestPart);

            IEnumerable <ImportDefinition> expectedImports = CreateImports(expectedType);
            IEnumerable <ExportDefinition> expectedExports = CreateExports(expectedType);

            bool importsCreatorCalled = false;
            Func <IEnumerable <ImportDefinition> > importsCreator = () =>
            {
                Assert.IsFalse(importsCreatorCalled);
                importsCreatorCalled = true;
                return(expectedImports.Cast <ImportDefinition>());
            };

            bool exportsCreatorCalled = false;
            Func <IEnumerable <ExportDefinition> > exportsCreator = () =>
            {
                Assert.IsFalse(exportsCreatorCalled);
                exportsCreatorCalled = true;
                return(expectedExports.Cast <ExportDefinition>());
            };

            ReflectionComposablePartDefinition definition = CreateReflectionPartDefinition(
                expectedType.AsLazy(),
                false,
                importsCreator,
                exportsCreator,
                null,
                null);

            IEnumerable <ExportDefinition> exports;

            Assert.IsFalse(exportsCreatorCalled);
            exports = definition.ExportDefinitions;
            Assert.IsTrue(exportsCreatorCalled);
            exports = definition.ExportDefinitions;


            IEnumerable <ImportDefinition> imports;

            Assert.IsFalse(importsCreatorCalled);
            imports = definition.ImportDefinitions;
            Assert.IsTrue(importsCreatorCalled);
            imports = definition.ImportDefinitions;
        }
        public override bool Equals(object obj)
        {
            if (_creationInfo.IsIdentityComparison)
            {
                return(object.ReferenceEquals(this, obj));
            }
            else
            {
                ReflectionComposablePartDefinition that = obj as ReflectionComposablePartDefinition;
                if (that == null)
                {
                    return(false);
                }

                return(_creationInfo.Equals(that._creationInfo));
            }
        }
        public GenericSpecializationPartCreationInfo(IReflectionPartCreationInfo originalPartCreationInfo, ReflectionComposablePartDefinition originalPart, Type[] specialization)
        {
            Assumes.NotNull(originalPartCreationInfo);
            Assumes.NotNull(specialization);
            Assumes.NotNull(originalPart);

            this._originalPartCreationInfo = originalPartCreationInfo;
            this._originalPart             = originalPart;
            this._specialization           = specialization;
            this._specializationIdentities = new string[this._specialization.Length];
            for (int i = 0; i < this._specialization.Length; i++)
            {
                this._specializationIdentities[i] = AttributedModelServices.GetTypeIdentity(this._specialization[i]);
            }
            this._lazyPartType = new Lazy <Type>(
                () => this._originalPartCreationInfo.GetPartType().MakeGenericType(specialization),
                LazyThreadSafetyMode.PublicationOnly);
        }
        public GenericSpecializationPartCreationInfo(IReflectionPartCreationInfo originalPartCreationInfo, ReflectionComposablePartDefinition originalPart, Type[] specialization)
        {
            Assumes.NotNull(originalPartCreationInfo);
            Assumes.NotNull(specialization);
            Assumes.NotNull(originalPart);

            this._originalPartCreationInfo = originalPartCreationInfo;
            this._originalPart = originalPart;
            this._specialization = specialization;
            this._specializationIdentities = new string[this._specialization.Length];
            for (int i = 0; i < this._specialization.Length; i++)
            {
                this._specializationIdentities[i] = AttributedModelServices.GetTypeIdentity(this._specialization[i]);
            }
            this._lazyPartType = new Lazy<Type>(
                () => this._originalPartCreationInfo.GetPartType().MakeGenericType(specialization),
                LazyThreadSafetyMode.PublicationOnly);

        }
        internal bool TryMakeGenericPartDefinition(Type[] genericTypeParameters, out ComposablePartDefinition genericPartDefinition)
        {
            genericPartDefinition = null;

            if (!GenericSpecializationPartCreationInfo.CanSpecialize(this.Metadata, genericTypeParameters))
            {
                return false;
            }

            genericPartDefinition = new ReflectionComposablePartDefinition(new GenericSpecializationPartCreationInfo(this._creationInfo, this, genericTypeParameters));
            return true;
        }
 public DisposableReflectionComposablePart(ReflectionComposablePartDefinition definition)
     : base(definition)
 {
 }
Beispiel #23
0
        public ReflectionComposablePart(ReflectionComposablePartDefinition definition)
        {
            Requires.NotNull(definition, "definition");

            this._definition = definition;
        }
        public ReflectionComposablePart(ReflectionComposablePartDefinition definition)
        {
            Requires.NotNull(definition, nameof(definition));

            _definition = definition;
        }
 public DisposableReflectionComposablePart(ReflectionComposablePartDefinition definition)
     : base(definition)
 {
 }