public static System.ComponentModel.Composition.Primitives.ExportDefinition CreateExportDefinition(LazyMemberInfo exportingMember, string contractName, Lazy<IDictionary<string, Object>> metadata, System.ComponentModel.Composition.Primitives.ICompositionElement origin)
    {
      Contract.Requires(contractName != null);
      Contract.Ensures(Contract.Result<System.ComponentModel.Composition.Primitives.ExportDefinition>() != null);

      return default(System.ComponentModel.Composition.Primitives.ExportDefinition);
    }
    public static System.ComponentModel.Composition.Primitives.ContractBasedImportDefinition CreateImportDefinition(LazyMemberInfo importingMember, string contractName, string requiredTypeIdentity, IEnumerable<KeyValuePair<string, Type>> requiredMetadata, System.ComponentModel.Composition.Primitives.ImportCardinality cardinality, bool isRecomposable, System.ComponentModel.Composition.CreationPolicy requiredCreationPolicy, System.ComponentModel.Composition.Primitives.ICompositionElement origin)
    {
      Contract.Requires(contractName != null);
      Contract.Ensures(Contract.Result<System.ComponentModel.Composition.Primitives.ContractBasedImportDefinition>() != null);

      return default(System.ComponentModel.Composition.Primitives.ContractBasedImportDefinition);
    }
예제 #3
0
 public static ContractBasedImportDefinition CreateImportDefinition(
     LazyMemberInfo importingMember,
     string contractName,
     string requiredTypeIdentity,
     IEnumerable <KeyValuePair <string, Type> > requiredMetadata,
     ImportCardinality cardinality,
     bool isRecomposable,
     CreationPolicy requiredCreationPolicy,
     IDictionary <string, object> metadata,
     bool isExportFactory,
     ICompositionElement origin)
 {
     return(CreateImportDefinition(
                importingMember,
                contractName,
                requiredTypeIdentity,
                requiredMetadata,
                cardinality,
                isRecomposable,
                false,
                requiredCreationPolicy,
                metadata,
                isExportFactory,
                origin));
 }
예제 #4
0
        private MemberInfo[] GetAccessors(LazyMemberInfo originalLazyMember)
        {
            BuildTables();
            Assumes.NotNull(_membersTable);

            return(_membersTable[originalLazyMember]);
        }
예제 #5
0
 public void Constructor_PassAccessorsWithNulls()
 {
     Assert.Throws <ArgumentException>("accessors", () =>
     {
         LazyMemberInfo lazy = new LazyMemberInfo(MemberTypes.Field, new MemberInfo[] { null, null });
     });
 }
예제 #6
0
        public static ReflectionMember ToReflectionMember(this LazyMemberInfo lazyMember)
        {
            MemberInfo[] accessors  = lazyMember.GetAccessors();
            MemberTypes  memberType = lazyMember.MemberType;

            switch (memberType)
            {
            case MemberTypes.Field:
                if (accessors.Length != 1)
                {
                    throw new Exception(SR.Diagnostic_InternalExceptionMessage);
                }
                return(((FieldInfo)accessors[0]).ToReflectionField());

            case MemberTypes.Property:
                if (accessors.Length != 2)
                {
                    throw new Exception(SR.Diagnostic_InternalExceptionMessage);
                }
                return(ReflectionExtensions.CreateReflectionProperty((MethodInfo)accessors[0], (MethodInfo)accessors[1]));

            case MemberTypes.NestedType:
            case MemberTypes.TypeInfo:
                return(((Type)accessors[0]).ToReflectionType());

            default:
                if (memberType != MemberTypes.Method)
                {
                    throw new Exception(SR.Diagnostic_InternalExceptionMessage);
                }
                return(((MethodInfo)accessors[0]).ToReflectionMethod());
            }
        }
예제 #7
0
 public void Constructor_PassNullAccessorCreators()
 {
     ExceptionAssert.ThrowsArgument <ArgumentNullException>("accessorsCreator", () =>
     {
         LazyMemberInfo lazy = new LazyMemberInfo(MemberTypes.Field, (Func <MemberInfo[]>)null);
     });
 }
예제 #8
0
 public void Constructor_PassNullAccessors()
 {
     Assert.Throws <ArgumentNullException>("accessors", () =>
     {
         LazyMemberInfo lazy = new LazyMemberInfo(MemberTypes.Field, (MemberInfo[])null);
     });
 }
예제 #9
0
 public void Constructor_PassNullMember()
 {
     ExceptionAssert.ThrowsArgument<ArgumentNullException>("member", () =>
         {
             LazyMemberInfo lazy = new LazyMemberInfo((MemberInfo)null);
         });
 }
예제 #10
0
        public void Constructor()
        {
            MemberInfo     expectedMember = this.GetType();
            LazyMemberInfo expectedExportingMemberInfo = new LazyMemberInfo(expectedMember);

            string expectedContractName = "Contract";
            IDictionary <string, object> expectedMetadata = new Dictionary <string, object>();

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

            ReflectionMemberExportDefinition definition = CreateReflectionExportDefinition(expectedExportingMemberInfo, expectedContractName, expectedMetadata);

            Assert.Equal(expectedExportingMemberInfo, definition.ExportingLazyMember);
            Assert.Same(expectedMember, definition.ExportingLazyMember.GetAccessors()[0]);
            Assert.Equal(MemberTypes.TypeInfo, definition.ExportingLazyMember.MemberType);

            Assert.Same(expectedContractName, definition.ContractName);

            Assert.NotNull(definition.Metadata);
            Assert.True(definition.Metadata.Keys.SequenceEqual(expectedMetadata.Keys));
            Assert.True(definition.Metadata.Values.SequenceEqual(expectedMetadata.Values));

            Assert.Null(((ICompositionElement)definition).Origin);
        }
예제 #11
0
 public void Constructor_PassNullMember()
 {
     Assert.Throws <ArgumentNullException>("member", () =>
     {
         LazyMemberInfo lazy = new LazyMemberInfo((MemberInfo)null);
     });
 }
        public ReflectionMemberExportDefinition(LazyMemberInfo member, ExportDefinition exportDefinition, ICompositionElement origin)
        {
            Assumes.NotNull(exportDefinition);

            this._member = member;
            this._exportDefinition = exportDefinition;
            this._origin = origin;
        }
        public ReflectionMemberExportDefinition(LazyMemberInfo member, ExportDefinition exportDefinition, ICompositionElement origin)
        {
            Assumes.NotNull(exportDefinition);

            this._member           = member;
            this._exportDefinition = exportDefinition;
            this._origin           = origin;
        }
        public ReflectionMemberExportDefinition(LazyMemberInfo member, ExportDefinition exportDefinition, ICompositionElement?origin)
        {
            ArgumentNullException.ThrowIfNull(exportDefinition);

            _member           = member;
            _exportDefinition = exportDefinition;
            _origin           = origin;
        }
        public void SetDefinition_OriginIsSet()
        {
            LazyMemberInfo member = CreateLazyMemberInfo();
            var            expectedPartDefinition       = PartDefinitionFactory.CreateAttributed(typeof(object));
            ReflectionMemberImportDefinition definition = new ReflectionMemberImportDefinition(
                member, "Contract", (string)null, null, ImportCardinality.ZeroOrMore, true, false, CreationPolicy.NonShared, null, expectedPartDefinition);

            Assert.Same(expectedPartDefinition, ((ICompositionElement)definition).Origin);
        }
        private MemberInfo[] GetAccessors(LazyMemberInfo originalLazyMember)
        {
            BuildTables();
            if (_membersTable == null)
            {
                throw new ArgumentNullException(nameof(_membersTable));
            }

            return(_membersTable[originalLazyMember]);
        }
예제 #17
0
        public static ReflectionWritableMember ToReflectionWriteableMember(this LazyMemberInfo lazyMember)
        {
            Assumes.IsTrue((lazyMember.MemberType == MemberTypes.Field) || (lazyMember.MemberType == MemberTypes.Property));

            ReflectionWritableMember reflectionMember = lazyMember.ToReflectionMember() as ReflectionWritableMember;

            Assumes.NotNull(reflectionMember);

            return(reflectionMember);
        }
 public PartCreatorMemberImportDefinition(
     LazyMemberInfo importingLazyMember,
     ICompositionElement origin,
     ContractBasedImportDefinition productImportDefinition)
     : base(importingLazyMember, CompositionConstants.PartCreatorContractName, CompositionConstants.PartCreatorTypeIdentity,
            productImportDefinition.RequiredMetadata, productImportDefinition.Cardinality, productImportDefinition.IsRecomposable, CreationPolicy.Any, origin)
 {
     Assumes.NotNull(productImportDefinition);
     this._productImportDefinition = productImportDefinition;
 }
 public PartCreatorMemberImportDefinition(
     LazyMemberInfo importingLazyMember,
     ICompositionElement origin,
     ContractBasedImportDefinition productImportDefinition)
     : base(importingLazyMember, CompositionConstants.PartCreatorContractName, CompositionConstants.PartCreatorTypeIdentity,
         productImportDefinition.RequiredMetadata, productImportDefinition.Cardinality, productImportDefinition.IsRecomposable, CreationPolicy.Any, origin)
 {
     Assumes.NotNull(productImportDefinition);
     this._productImportDefinition = productImportDefinition;
 }
        public void Constructor_WithNullRequiredMetadata()
        {
            LazyMemberInfo member = CreateLazyMemberInfo();

            ReflectionMemberImportDefinition definition = new ReflectionMemberImportDefinition(
                member, "Contract", (string)null, null, ImportCardinality.ZeroOrMore, true, false, CreationPolicy.NonShared, null, null);

            Assert.NotNull(definition.RequiredMetadata);
            Assert.Equal(0, definition.RequiredMetadata.Count());
        }
예제 #21
0
        public PartCreatorMemberImportDefinition(
            LazyMemberInfo importingLazyMember,
            ICompositionElement?origin,
            ContractBasedImportDefinition productImportDefinition)
            : base(importingLazyMember, CompositionConstants.PartCreatorContractName, CompositionConstants.PartCreatorTypeIdentity,
                   productImportDefinition.RequiredMetadata, productImportDefinition.Cardinality, productImportDefinition.IsRecomposable, false, productImportDefinition.RequiredCreationPolicy, MetadataServices.EmptyMetadata, origin)
        {
            ArgumentNullException.ThrowIfNull(productImportDefinition);

            _productImportDefinition = productImportDefinition;
        }
예제 #22
0
        public ReflectionMemberExportDefinition(LazyMemberInfo member, ExportDefinition exportDefinition, ICompositionElement?origin)
        {
            if (exportDefinition == null)
            {
                throw new ArgumentNullException(nameof(exportDefinition));
            }

            _member           = member;
            _exportDefinition = exportDefinition;
            _origin           = origin;
        }
예제 #23
0
 public static ContractBasedImportDefinition CreateImportDefinition(
     LazyMemberInfo importingMember,
     string contractName,
     string requiredTypeIdentity,
     IEnumerable<KeyValuePair<string, Type>> requiredMetadata,
     ImportCardinality cardinality,
     bool isRecomposable,
     CreationPolicy requiredCreationPolicy,
     ICompositionElement origin)
 {
     return CreateImportDefinition(importingMember, contractName, requiredTypeIdentity, requiredMetadata, cardinality, isRecomposable, requiredCreationPolicy, false, origin);
 }
예제 #24
0
 public static ContractBasedImportDefinition CreateImportDefinition(
     LazyMemberInfo importingMember,
     string contractName,
     string?requiredTypeIdentity,
     IEnumerable <KeyValuePair <string, Type> >?requiredMetadata,
     ImportCardinality cardinality,
     bool isRecomposable,
     CreationPolicy requiredCreationPolicy,
     ICompositionElement?origin)
 {
     return(CreateImportDefinition(importingMember, contractName, requiredTypeIdentity, requiredMetadata, cardinality, isRecomposable, requiredCreationPolicy, MetadataServices.EmptyMetadata, false, origin));
 }
예제 #25
0
        public void Constructor_PassAccessors()
        {
            foreach (var memberAndAccessorsInfo in GetMembersAndAccessors(typeof(LazyMemberTestClass)))
            {
                MemberInfo member = memberAndAccessorsInfo.Item1;
                MemberTypes memberType = memberAndAccessorsInfo.Item2.Item1;
                MemberInfo[] accessors = memberAndAccessorsInfo.Item2.Item2;

                LazyMemberInfo lazy = new LazyMemberInfo(memberType, accessors);
                Assert.AreEqual(memberType, lazy.MemberType);
                Assert.AreEqual(accessors.Length, lazy.GetAccessors().Length);
                Assert.IsTrue(accessors.SequenceEqual(lazy.GetAccessors()));
            }
        }
예제 #26
0
        public static ExportDefinition CreateExportDefinition(
            LazyMemberInfo exportingMember,
            string contractName,
            Lazy<IDictionary<string, object>> metadata,
            ICompositionElement origin)
        {
            Requires.NotNullOrEmpty(contractName, "contractName");
            Requires.IsInMembertypeSet(exportingMember.MemberType, "exportingMember", MemberTypes.Field | MemberTypes.Property | MemberTypes.NestedType | MemberTypes.TypeInfo | MemberTypes.Method);

            return new ReflectionMemberExportDefinition(
                exportingMember,
                new LazyExportDefinition(contractName, metadata),
                origin);
        }
예제 #27
0
 public void Constructor_PassAccessorsInvalidMemberType()
 {
     MemberTypes[] validMemberTypes = GetValidMemberTypes().ToArray();
     foreach (MemberTypes memberType in GetMemberTypeValues())
     {
         if (!validMemberTypes.Contains(memberType))
         {
             Assert.Throws <ArgumentException>("memberType", () =>
             {
                 LazyMemberInfo lazy = new LazyMemberInfo(memberType, typeof(LazyMemberTestClass));
             });
         }
     }
 }
예제 #28
0
        public static ExportDefinition CreateExportDefinition(
            LazyMemberInfo exportingMember,
            string contractName,
            Lazy <IDictionary <string, object> > metadata,
            ICompositionElement origin)
        {
            Requires.NotNullOrEmpty(contractName, "contractName");
            Requires.IsInMembertypeSet(exportingMember.MemberType, "exportingMember", MemberTypes.Field | MemberTypes.Property | MemberTypes.NestedType | MemberTypes.TypeInfo | MemberTypes.Method);

            return(new ReflectionMemberExportDefinition(
                       exportingMember,
                       new LazyExportDefinition(contractName, metadata),
                       origin));
        }
예제 #29
0
        public void Constructor_PassAccessors()
        {
            foreach (var memberAndAccessorsInfo in GetMembersAndAccessors(typeof(LazyMemberTestClass)))
            {
                MemberInfo   member     = memberAndAccessorsInfo.Item1;
                MemberTypes  memberType = memberAndAccessorsInfo.Item2.Item1;
                MemberInfo[] accessors  = memberAndAccessorsInfo.Item2.Item2;

                LazyMemberInfo lazy = new LazyMemberInfo(memberType, accessors);
                Assert.Equal(memberType, lazy.MemberType);
                Assert.Equal(accessors.Length, lazy.GetAccessors().Length);
                Assert.True(accessors.SequenceEqual(lazy.GetAccessors()));
            }
        }
        public ReflectionMemberImportDefinition(
            LazyMemberInfo importingLazyMember,
            string contractName, 
            string requiredTypeIdentity,
            IEnumerable<KeyValuePair<string, Type>> requiredMetadata,
            ImportCardinality cardinality, 
            bool isRecomposable, 
            CreationPolicy requiredCreationPolicy,
            ICompositionElement origin) 
            : base(contractName, requiredTypeIdentity, requiredMetadata, cardinality, isRecomposable, false, requiredCreationPolicy, origin)
        {
            Assumes.NotNull(contractName);

            this._importingLazyMember = importingLazyMember;
        }
예제 #31
0
        public ReflectionMemberImportDefinition(
            LazyMemberInfo importingLazyMember,
            string contractName,
            string requiredTypeIdentity,
            IEnumerable <string> requiredMetadata,
            ImportCardinality cardinality,
            bool isRecomposable,
            CreationPolicy requiredCreationPolicy,
            ICompositionElement origin)
            : base(contractName, requiredTypeIdentity, requiredMetadata, cardinality, isRecomposable, false, requiredCreationPolicy, origin)
        {
            Assumes.NotNull(contractName);

            this._importingLazyMember = importingLazyMember;
        }
        public static ExportDefinition ReadExportDefinition(ComposablePartDefinition owner, IDictionary<string, object> cache)
        {
            Assumes.NotNull(owner);
            Assumes.NotNull(cache);

            LazyMemberInfo exportingMemberInfo = new LazyMemberInfo(
                cache.ReadValue<MemberTypes>(AttributedCacheServices.CacheKeys.MemberType, MemberTypes.TypeInfo),
                cache.ReadLazyAccessors(ReflectionModelServices.GetPartType(owner)));
                

            return ReflectionModelServices.CreateExportDefinition(
                exportingMemberInfo,
                cache.ReadContractName(),
                cache.ReadLazyMetadata(),
                owner as ICompositionElement);
        }
        public LazyMemberInfo(MemberTypes memberType, params MemberInfo[] accessors)
        {
            EnsureSupportedMemberType(memberType, nameof(memberType));
            Requires.NotNull(accessors, nameof(accessors));

            string errorMessage;

            if (!LazyMemberInfo.AreAccessorsValid(memberType, accessors, out errorMessage))
            {
                throw new ArgumentException(errorMessage, nameof(accessors));
            }

            _memberType       = memberType;
            _accessors        = accessors;
            _accessorsCreator = null;
        }
예제 #34
0
        public static ReflectionWritableMember ToReflectionWriteableMember(this LazyMemberInfo lazyMember)
        {
            if ((lazyMember.MemberType != MemberTypes.Field) && (lazyMember.MemberType != MemberTypes.Property))
            {
                throw new Exception(SR.Diagnostic_InternalExceptionMessage);
            }

            ReflectionWritableMember?reflectionMember = lazyMember.ToReflectionMember() as ReflectionWritableMember;

            if (reflectionMember == null)
            {
                throw new Exception(SR.Diagnostic_InternalExceptionMessage);
            }

            return(reflectionMember);
        }
예제 #35
0
        public static ContractBasedImportDefinition CreateImportDefinition(
            LazyMemberInfo importingMember,
            string contractName,
            string requiredTypeIdentity,
            IEnumerable <KeyValuePair <string, Type> > requiredMetadata,
            ImportCardinality cardinality,
            bool isRecomposable,
            bool isPreRequisite,
            CreationPolicy requiredCreationPolicy,
            IDictionary <string, object> metadata,
            bool isExportFactory,
            ICompositionElement origin)
        {
            Requires.NotNullOrEmpty(contractName, "contractName");
            Requires.IsInMembertypeSet(importingMember.MemberType, "importingMember", MemberTypes.Property | MemberTypes.Field);
            Contract.Ensures(Contract.Result <ContractBasedImportDefinition>() != null);

            if (isExportFactory)
            {
                return(new PartCreatorMemberImportDefinition(
                           importingMember,
                           origin,
                           new ContractBasedImportDefinition(
                               contractName,
                               requiredTypeIdentity,
                               requiredMetadata,
                               cardinality,
                               isRecomposable,
                               isPreRequisite,
                               CreationPolicy.NonShared,
                               metadata)));
            }
            else
            {
                return(new ReflectionMemberImportDefinition(
                           importingMember,
                           contractName,
                           requiredTypeIdentity,
                           requiredMetadata,
                           cardinality,
                           isRecomposable,
                           isPreRequisite,
                           requiredCreationPolicy,
                           metadata,
                           origin));
            }
        }
예제 #36
0
        public static ContractBasedImportDefinition CreateImportDefinition(
            LazyMemberInfo importingMember,
            string contractName,
            string requiredTypeIdentity,
            IEnumerable<KeyValuePair<string, Type>> requiredMetadata,
            ImportCardinality cardinality,
            bool isRecomposable,
            CreationPolicy requiredCreationPolicy,
            bool isPartCreator,
            ICompositionElement origin)
        {
            Requires.NotNullOrEmpty(contractName, "contractName");
            Requires.NullOrNotNullElements(requiredMetadata, "requiredMetadata");
            Requires.IsInMembertypeSet(importingMember.MemberType, "importingMember", MemberTypes.Property | MemberTypes.Field);

            if (isPartCreator)
            {
            #if SILVERLIGHT
                return new PartCreatorMemberImportDefinition(
                    importingMember,
                    origin,
                    new ContractBasedImportDefinition(
                        contractName,
                        requiredTypeIdentity,
                        requiredMetadata,
                        cardinality,
                        isRecomposable,
                        false,
                        CreationPolicy.NonShared));
            #else
                throw new ArgumentException("PartCreator is only support in Silverlight version of MEF", "isPartCreator");
            #endif
            }
            else
            {
                return new ReflectionMemberImportDefinition(
                    importingMember,
                    contractName,
                    requiredTypeIdentity,
                    requiredMetadata,
                    cardinality,
                    isRecomposable,
                    requiredCreationPolicy,
                    origin);
            }
        }
예제 #37
0
		private void ConfigureExportDefinitions(Type implementationType, Type contractType)
		{
			var lazyMember = new LazyMemberInfo(implementationType);
			var contractName = !_registration.HasName()
								  ? AttributedModelServices.GetContractName(_registration.Service)
								  : _registration.Name;

			var metadata = new Lazy<IDictionary<string, object>>(() =>
			{
				var md = new Dictionary<string, object>();
				md.Add(CompositionConstants.ExportTypeIdentityMetadataName,
					   AttributedModelServices.GetTypeIdentity(contractType));
				return md;
			});

			_exports = new[] { ReflectionModelServices.CreateExportDefinition(lazyMember, contractName, metadata, null) };
		}
예제 #38
0
        public ReflectionMemberImportDefinition(
            LazyMemberInfo importingLazyMember,
            string contractName,
            string requiredTypeIdentity,
            IEnumerable <KeyValuePair <string, Type> > requiredMetadata,
            ImportCardinality cardinality,
            bool isRecomposable,
            bool isPrerequisite,
            CreationPolicy requiredCreationPolicy,
            IDictionary <string, object> metadata,
            ICompositionElement origin)
            : base(contractName, requiredTypeIdentity, requiredMetadata, cardinality, isRecomposable, isPrerequisite, requiredCreationPolicy, metadata, origin)
        {
            Assumes.NotNull(contractName);

            this._importingLazyMember = importingLazyMember;
        }
예제 #39
0
        public static ContractBasedImportDefinition CreateImportDefinition(
            LazyMemberInfo importingMember,
            string contractName,
            string requiredTypeIdentity,
            IEnumerable <KeyValuePair <string, Type> > requiredMetadata,
            ImportCardinality cardinality,
            bool isRecomposable,
            CreationPolicy requiredCreationPolicy,
            bool isPartCreator,
            ICompositionElement origin)
        {
            Requires.NotNullOrEmpty(contractName, "contractName");
            Requires.NullOrNotNullElements(requiredMetadata, "requiredMetadata");
            Requires.IsInMembertypeSet(importingMember.MemberType, "importingMember", MemberTypes.Property | MemberTypes.Field);

            if (isPartCreator)
            {
#if SILVERLIGHT
                return(new PartCreatorMemberImportDefinition(
                           importingMember,
                           origin,
                           new ContractBasedImportDefinition(
                               contractName,
                               requiredTypeIdentity,
                               requiredMetadata,
                               cardinality,
                               isRecomposable,
                               false,
                               CreationPolicy.NonShared)));
#else
                throw new ArgumentException("PartCreator is only support in Silverlight version of MEF", "isPartCreator");
#endif
            }
            else
            {
                return(new ReflectionMemberImportDefinition(
                           importingMember,
                           contractName,
                           requiredTypeIdentity,
                           requiredMetadata,
                           cardinality,
                           isRecomposable,
                           requiredCreationPolicy,
                           origin));
            }
        }
        public                               MemberInfo[] GetAccessors()
        {
            if ((_accessors == null) && (_accessorsCreator != null))
            {
                MemberInfo[] accessors = _accessorsCreator.Invoke();

                string errorMessage;
                if (!LazyMemberInfo.AreAccessorsValid(MemberType, accessors, out errorMessage))
                {
                    throw new InvalidOperationException(errorMessage);
                }

                _accessors = accessors;
            }

            return(_accessors !);
        }
예제 #41
0
        public void Constructor_NullMetadata()
        {
            MemberInfo     expectedMember = this.GetType();
            LazyMemberInfo expectedExportingMemberInfo = new LazyMemberInfo(expectedMember);

            string expectedContractName = "Contract";

            ReflectionMemberExportDefinition definition = CreateReflectionExportDefinition(expectedExportingMemberInfo, expectedContractName, null);

            Assert.Equal(expectedExportingMemberInfo, definition.ExportingLazyMember);
            Assert.Same(expectedMember, definition.ExportingLazyMember.GetAccessors()[0]);
            Assert.Equal(MemberTypes.TypeInfo, definition.ExportingLazyMember.MemberType);

            Assert.Same(expectedContractName, definition.ContractName);

            Assert.NotNull(definition.Metadata);
            Assert.Equal(0, definition.Metadata.Count);

            Assert.Null(((ICompositionElement)definition).Origin);
        }
        public void Constructor()
        {
            PropertyInfo expectedMember = typeof(PublicImportsExpectingPublicExports).GetProperty("PublicImportPublicProperty");
            LazyMemberInfo expectedImportingMemberInfo = new LazyMemberInfo(expectedMember);
            IEnumerable<KeyValuePair<string, Type>> requiredMetadata = new KeyValuePair<string, Type>[] { new KeyValuePair<string, Type>("Foo", typeof(object)) };

            ReflectionMemberImportDefinition definition = new ReflectionMemberImportDefinition(
                expectedImportingMemberInfo, "Contract", (string)null, requiredMetadata, ImportCardinality.ZeroOrMore, true, CreationPolicy.NonShared, null);

            Assert.AreEqual(expectedImportingMemberInfo, definition.ImportingLazyMember);

            Assert.AreEqual("Contract", definition.ContractName);
            Assert.AreSame(requiredMetadata, definition.RequiredMetadata);
            Assert.AreEqual(CreationPolicy.NonShared, definition.RequiredCreationPolicy);
            Assert.AreEqual(true, definition.IsRecomposable);
            Assert.AreEqual(false, definition.IsPrerequisite);
            Assert.IsNull(((ICompositionElement)definition).Origin);
            Assert.IsNotNull(((ICompositionElement)definition).DisplayName);
            Assert.IsTrue(((ICompositionElement)definition).DisplayName.Contains(expectedMember.GetDisplayName()));
        }
        public ReflectionMemberImportDefinition(
            LazyMemberInfo importingLazyMember,
            string contractName,
            string?requiredTypeIdentity,
            IEnumerable <KeyValuePair <string, Type> >?requiredMetadata,
            ImportCardinality cardinality,
            bool isRecomposable,
            bool isPrerequisite,
            CreationPolicy requiredCreationPolicy,
            IDictionary <string, object?> metadata,
            ICompositionElement?origin)
            : base(contractName, requiredTypeIdentity, requiredMetadata, cardinality, isRecomposable, isPrerequisite, requiredCreationPolicy, metadata, origin)
        {
            if (contractName == null)
            {
                throw new ArgumentNullException(nameof(contractName));
            }

            _importingLazyMember = importingLazyMember;
        }
        public void Constructor_NullMetadata()
        {
            MemberInfo expectedMember = this.GetType();
            LazyMemberInfo expectedExportingMemberInfo = new LazyMemberInfo(expectedMember);

            string expectedContractName = "Contract";

            ReflectionMemberExportDefinition definition = CreateReflectionExportDefinition(expectedExportingMemberInfo, expectedContractName, null);

            Assert.AreEqual(expectedExportingMemberInfo, definition.ExportingLazyMember);
            Assert.AreSame(expectedMember, definition.ExportingLazyMember.GetAccessors()[0]);
            Assert.AreEqual(MemberTypes.TypeInfo, definition.ExportingLazyMember.MemberType);

            Assert.AreSame(expectedContractName, definition.ContractName);

            Assert.IsNotNull(definition.Metadata);
            Assert.AreEqual(0, definition.Metadata.Count);

            Assert.IsNull(((ICompositionElement)definition).Origin);
        }
예제 #45
0
        public void Constructor_PassInvalidAccessors()
        {
            foreach (var memberAndAccessorsInfo in GetMembersAndAccessors(typeof(LazyMemberTestClass)))
            {
                MemberInfo member = memberAndAccessorsInfo.Item1;
                MemberTypes memberType = memberAndAccessorsInfo.Item2.Item1;
                MemberInfo[] accessors = memberAndAccessorsInfo.Item2.Item2;

                foreach (MemberTypes wrongMemberType in GetValidMemberTypes())
                {
                    if (wrongMemberType == memberType)
                    {
                        continue;
                    }
                    ExceptionAssert.ThrowsArgument<ArgumentException>("accessors", () =>
                    {
                        LazyMemberInfo lazy = new LazyMemberInfo(wrongMemberType, accessors);
                    });
                }
            }
        }
예제 #46
0
        public static ContractBasedImportDefinition CreateImportDefinition(
            LazyMemberInfo importingMember,
            string contractName,
            string requiredTypeIdentity,
            IEnumerable<string> requiredMetadata,
            ImportCardinality cardinality,
            bool isRecomposable,
            CreationPolicy requiredCreationPolicy,
            ICompositionElement origin)
        {
            Requires.NotNullOrEmpty(contractName, "contractName");
            Requires.NotNullOrNullElements(requiredMetadata, "requiredMetadata");
            Requires.IsInMembertypeSet(importingMember.MemberType, "importingMember", MemberTypes.Property | MemberTypes.Field);

            return new ReflectionMemberImportDefinition(
                importingMember,
                contractName,
                requiredTypeIdentity,
                requiredMetadata,
                cardinality,
                isRecomposable,
                requiredCreationPolicy,
                origin);
        }
        public void Constructor()
        {
            MemberInfo expectedMember = this.GetType();
            LazyMemberInfo expectedExportingMemberInfo = new LazyMemberInfo(expectedMember);

            string expectedContractName = "Contract";
            IDictionary<string, object> expectedMetadata = new Dictionary<string, object>();
            expectedMetadata["Key1"] = 1;
            expectedMetadata["Key2"] = "Value2";

            ReflectionMemberExportDefinition definition = CreateReflectionExportDefinition(expectedExportingMemberInfo, expectedContractName, expectedMetadata);

            Assert.AreEqual(expectedExportingMemberInfo, definition.ExportingLazyMember);
            Assert.AreSame(expectedMember, definition.ExportingLazyMember.GetAccessors()[0]);
            Assert.AreEqual(MemberTypes.TypeInfo, definition.ExportingLazyMember.MemberType);

            Assert.AreSame(expectedContractName, definition.ContractName);

            Assert.IsNotNull(definition.Metadata);
            Assert.IsTrue(definition.Metadata.Keys.SequenceEqual(expectedMetadata.Keys));
            Assert.IsTrue(definition.Metadata.Values.SequenceEqual(expectedMetadata.Values));

            Assert.IsNull(((ICompositionElement)definition).Origin);
        }
예제 #48
0
        public void GetPartCreatorProductImportDefinition_PartCreatorImport_()
        {
            LazyMemberInfo bogusMember = new LazyMemberInfo(MemberTypes.Field, () => new MemberInfo[] { typeof(ReflectionModelServicesTests) });
            var import = ReflectionModelServices.CreateImportDefinition(
                bogusMember,
                "Foo",
                "Foo",
                Enumerable.Empty<KeyValuePair<string, Type>>(),
                ImportCardinality.ZeroOrMore,
                false,
                CreationPolicy.Any,
                true, //isPartCreator
                null);

            var productImport = ReflectionModelServices.GetPartCreatorProductImportDefinition(import);

            var import2 = ReflectionModelServices.CreateImportDefinition(
                bogusMember,
                productImport.ContractName,
                productImport.RequiredTypeIdentity,
                productImport.RequiredMetadata,
                productImport.Cardinality,
                productImport.IsRecomposable,
                productImport.RequiredCreationPolicy,
                true, //isPartCreator
                null);

            Assert.AreEqual(import.ContractName, import2.ContractName);
            Assert.AreEqual(import.Cardinality, import2.Cardinality);
            Assert.AreEqual(import.IsRecomposable, import2.IsRecomposable);
            Assert.AreEqual(import.RequiredCreationPolicy, import2.RequiredCreationPolicy);
            Assert.AreEqual(import.RequiredTypeIdentity, import2.RequiredTypeIdentity);
            EnumerableAssert.AreEqual(import.RequiredMetadata, import2.RequiredMetadata);
        }
예제 #49
0
        public void CreateExportDefinition()
        {
            PropertyInfo property = typeof(TestPart).GetProperties().First();
            LazyMemberInfo expectedLazyMember = new LazyMemberInfo(property);

            IDictionary<string, object> expectedMetadata = new Dictionary<string, object>();
            expectedMetadata["Key1"] = 1;
            expectedMetadata["Key2"] = "Value2";

            string expectedContractName = "Foo";

            ICompositionElement expectedOrigin = new MockOrigin();

            ExportDefinition exportDefinition = ReflectionModelServices.CreateExportDefinition(expectedLazyMember, expectedContractName, expectedMetadata.AsLazy(), expectedOrigin);
            Assert.IsNotNull(exportDefinition);
            ReflectionMemberExportDefinition definition = exportDefinition as ReflectionMemberExportDefinition;
            Assert.IsNotNull(definition);

            Assert.AreEqual(expectedContractName, definition.ContractName);
            Assert.IsTrue(definition.Metadata.Keys.SequenceEqual(expectedMetadata.Keys));
            Assert.IsTrue(definition.Metadata.Values.SequenceEqual(expectedMetadata.Values));
            Assert.AreEqual(expectedOrigin, ((ICompositionElement)definition).Origin);
            Assert.AreEqual(expectedLazyMember, definition.ExportingLazyMember);
        }
        public ExportDefinition TranslateExpot(ReflectionMemberExportDefinition reflectionExport, List<LazyMemberInfo> members)
        {
            ExportDefinition export = null;
            LazyMemberInfo lazyMember = reflectionExport.ExportingLazyMember;
            var capturedLazyMember = lazyMember;
            var capturedReflectionExport = reflectionExport;

            string contractName = this.Translate(reflectionExport.ContractName, reflectionExport.Metadata.GetValue<int[]>(CompositionConstants.GenericExportParametersOrderMetadataName));

            LazyMemberInfo exportingMember = new LazyMemberInfo(capturedLazyMember.MemberType, () => GetAccessors(capturedLazyMember));
            Lazy<IDictionary<string, object>> lazyMetadata = new Lazy<IDictionary<string, object>>(() => this.TranslateExportMetadata(capturedReflectionExport));



            export = new ReflectionMemberExportDefinition(
                exportingMember,
                new LazyExportDefinition(contractName, lazyMetadata),
                ((ICompositionElement)reflectionExport).Origin);

            members.Add(capturedLazyMember);

            return export;
        }
        private ImportDefinition TranslateImport(ReflectionImportDefinition reflectionImport, List<LazyMemberInfo> members, List<Lazy<ParameterInfo>> parameters)
        {
            bool isExportFactory = false;
            ContractBasedImportDefinition productImport = reflectionImport;

            IPartCreatorImportDefinition exportFactoryImportDefinition = reflectionImport as IPartCreatorImportDefinition;
            if (exportFactoryImportDefinition != null)
            {
                productImport = exportFactoryImportDefinition.ProductImportDefinition;
                isExportFactory = true;
            }

            string contractName = this.Translate(productImport.ContractName);
            string requiredTypeIdentity = this.Translate(productImport.RequiredTypeIdentity);
            IDictionary<string, object> metadata = this.TranslateImportMetadata(productImport);
            
            ReflectionMemberImportDefinition memberImport = reflectionImport as ReflectionMemberImportDefinition;
            ImportDefinition import = null;
            if (memberImport != null)
            {
                LazyMemberInfo lazyMember = memberImport.ImportingLazyMember;
                LazyMemberInfo importingMember = new LazyMemberInfo(lazyMember.MemberType, () => GetAccessors(lazyMember));

                if (isExportFactory)
                {
                    import = new PartCreatorMemberImportDefinition(
                        importingMember,
                        ((ICompositionElement)memberImport).Origin,
                        new ContractBasedImportDefinition(
                            contractName,
                            requiredTypeIdentity,
                            productImport.RequiredMetadata,
                            productImport.Cardinality,
                            productImport.IsRecomposable,
                            false,
                            CreationPolicy.NonShared,
                            metadata));
                }
                else
                {
                    import = new ReflectionMemberImportDefinition(
                         importingMember,
                         contractName,
                         requiredTypeIdentity,
                         productImport.RequiredMetadata,
                         productImport.Cardinality,
                         productImport.IsRecomposable,
                         false,
                         productImport.RequiredCreationPolicy,
                         metadata,
                         ((ICompositionElement)memberImport).Origin);
                }

                members.Add(lazyMember);
            }
            else
            {
                ReflectionParameterImportDefinition parameterImport = reflectionImport as ReflectionParameterImportDefinition;
                Assumes.NotNull(parameterImport);

                Lazy<ParameterInfo> lazyParameter = parameterImport.ImportingLazyParameter;
                Lazy<ParameterInfo> parameter = new Lazy<ParameterInfo>(() => GetParameter(lazyParameter));

                if (isExportFactory)
                {
                    import = new PartCreatorParameterImportDefinition(
                            parameter,
                            ((ICompositionElement)parameterImport).Origin,
                            new ContractBasedImportDefinition(
                                contractName,
                                requiredTypeIdentity,
                                productImport.RequiredMetadata,
                                productImport.Cardinality,
                                false,
                                true,
                                CreationPolicy.NonShared,
                                metadata));
                }
                else
                {
                    import = new ReflectionParameterImportDefinition(
                         parameter,
                         contractName,
                         requiredTypeIdentity,
                         productImport.RequiredMetadata,
                         productImport.Cardinality,
                         productImport.RequiredCreationPolicy,
                         metadata,
                         ((ICompositionElement)parameterImport).Origin);
                }

                parameters.Add(lazyParameter);
            }

            return import;
        }
        private MemberInfo[] GetAccessors(LazyMemberInfo originalLazyMember)
        {
            this.BuildTables();
            Assumes.NotNull(this._membersTable);

            return this._membersTable[originalLazyMember];
        }
예제 #53
0
        public void CreateExportDefinition_NullAsContractName_ThrowsNullArgument()
        {
            PropertyInfo property = typeof(TestPart).GetProperties().First();
            LazyMemberInfo expectedLazyMember = new LazyMemberInfo(property);

            IDictionary<string, object> expectedMetadata = new Dictionary<string, object>();
            expectedMetadata["Key1"] = 1;
            expectedMetadata["Key2"] = "Value2";


            ICompositionElement expectedOrigin = new MockOrigin();

            ExceptionAssert.ThrowsArgument<ArgumentNullException>("contractName", () =>
            {
                ReflectionModelServices.CreateExportDefinition(expectedLazyMember, null, expectedMetadata.AsLazy(), expectedOrigin);
            });
        }
예제 #54
0
        public void IsImportingParameter_OnMemberImport()
        {
            PropertyInfo property = typeof(TestPart).GetProperties().First();
            LazyMemberInfo expectedLazyMember = new LazyMemberInfo(property);

            string expectedContractName = "Foo";
            string expectedRequiredTypeIdentity = "Bar";
            KeyValuePair<string, Type>[] expectedRequiredMetadata = new KeyValuePair<string, Type>[] { new KeyValuePair<string, Type>("Key1", typeof(string)), new KeyValuePair<string, Type>("Key2", typeof(int)) };
            ImportCardinality expectedCardinality = ImportCardinality.ExactlyOne;
            CreationPolicy expectedCreationPolicy = CreationPolicy.NonShared;
            bool expectedRecomposable = true;

            ICompositionElement expectedOrigin = new MockOrigin();

            ImportDefinition importDefinition = ReflectionModelServices.CreateImportDefinition(
                expectedLazyMember,
                expectedContractName,
                expectedRequiredTypeIdentity,
                expectedRequiredMetadata,
                expectedCardinality,
                expectedRecomposable,
                expectedCreationPolicy,
                expectedOrigin);
            Assert.IsNotNull(importDefinition);

            Assert.IsFalse(ReflectionModelServices.IsImportingParameter(importDefinition));
        }
예제 #55
0
 public void Constructor_PassNullAccessorCreators()
 {
     ExceptionAssert.ThrowsArgument<ArgumentNullException>("accessorsCreator", () =>
     {
         LazyMemberInfo lazy = new LazyMemberInfo(MemberTypes.Field, (Func<MemberInfo[]>)null);
     });
 }
예제 #56
0
        public void CreateExportDefinition_InvalidLazymemberInfo_ShouldThrowArtument()
        {
            EventInfo _event = typeof(TestPart).GetEvents().First();
            LazyMemberInfo expectedLazyMember = new LazyMemberInfo(_event);

            IDictionary<string, object> expectedMetadata = new Dictionary<string, object>();
            expectedMetadata["Key1"] = 1;
            expectedMetadata["Key2"] = "Value2";

            string expectedContractName = "Foo";

            ICompositionElement expectedOrigin = new MockOrigin();

            ExceptionAssert.ThrowsArgument<ArgumentException>("exportingMember", () =>
            {
                ReflectionModelServices.CreateExportDefinition(expectedLazyMember, expectedContractName, expectedMetadata.AsLazy(), expectedOrigin);
            });
        }
예제 #57
0
        public void GetExportingMember()
        {
            PropertyInfo property = typeof(TestPart).GetProperties().First();
            LazyMemberInfo expectedLazyMember = new LazyMemberInfo(property);

            IDictionary<string, object> expectedMetadata = new Dictionary<string, object>();
            expectedMetadata["Key1"] = 1;
            expectedMetadata["Key2"] = "Value2";

            string expectedContractName = "Foo";

            ICompositionElement expectedOrigin = new MockOrigin();

            ExportDefinition exportDefinition = ReflectionModelServices.CreateExportDefinition(expectedLazyMember, expectedContractName, expectedMetadata.AsLazy(), expectedOrigin);
            Assert.IsNotNull(exportDefinition);

            LazyMemberInfo lazyMember = ReflectionModelServices.GetExportingMember(exportDefinition);
            Assert.AreEqual(expectedLazyMember, lazyMember);
        }
예제 #58
0
        public void CreateImportDefinition_Member()
        {
            PropertyInfo property = typeof(TestPart).GetProperties().First();
            LazyMemberInfo expectedLazyMember = new LazyMemberInfo(property);

            string expectedContractName = "Foo";
            string expectedRequiredTypeIdentity = "Bar";
            KeyValuePair<string, Type>[] expectedRequiredMetadata = new KeyValuePair<string, Type>[] { new KeyValuePair<string, Type>("Key1", typeof(string)), new KeyValuePair<string, Type>("Key2", typeof(int)) };
            ImportCardinality expectedCardinality = ImportCardinality.ExactlyOne;
            CreationPolicy expectedCreationPolicy = CreationPolicy.NonShared;
            bool expectedRecomposable = true;

            ICompositionElement expectedOrigin = new MockOrigin();

            ImportDefinition importDefinition = ReflectionModelServices.CreateImportDefinition(
                expectedLazyMember,
                expectedContractName,
                expectedRequiredTypeIdentity,
                expectedRequiredMetadata,
                expectedCardinality,
                expectedRecomposable,
                expectedCreationPolicy,
                expectedOrigin);
            Assert.IsNotNull(importDefinition);

            ReflectionMemberImportDefinition definition = importDefinition as ReflectionMemberImportDefinition;
            Assert.IsNotNull(definition);

            Assert.AreEqual(expectedLazyMember, definition.ImportingLazyMember);
            Assert.AreEqual(definition.ContractName, expectedContractName);
            Assert.AreEqual(definition.RequiredTypeIdentity, expectedRequiredTypeIdentity);
            Assert.IsTrue(definition.RequiredMetadata.SequenceEqual(expectedRequiredMetadata));
            Assert.AreEqual(definition.Cardinality, expectedCardinality);
            Assert.AreEqual(definition.RequiredCreationPolicy, expectedCreationPolicy);
            Assert.AreEqual(definition.IsRecomposable, expectedRecomposable);
            Assert.AreSame(expectedOrigin, ((ICompositionElement)definition).Origin);
            Assert.IsFalse(definition.IsPrerequisite);
        }
예제 #59
0
 public void Constructor_PassAccessorsInvalidMemberType()
 {
     MemberTypes[] validMemberTypes = GetValidMemberTypes().ToArray();
     foreach (MemberTypes memberType in GetMemberTypeValues())
     {
         if (!validMemberTypes.Contains(memberType))
         {
             ExceptionAssert.ThrowsArgument<ArgumentException>("memberType", () =>
             {
                 LazyMemberInfo lazy = new LazyMemberInfo(memberType, typeof(LazyMemberTestClass));
             });
         }
     }
 }
예제 #60
0
        public void CreateImportDefinition_Member_InvalidMember_ShouldThrowArgument()
        {
            MethodInfo method = typeof(TestPart).GetMethods().First();
            LazyMemberInfo expectedLazyMember = new LazyMemberInfo(method);

            string expectedContractName = "Foo";
            string expectedRequiredTypeIdentity = "Bar";
            KeyValuePair<string, Type>[] expectedRequiredMetadata = new KeyValuePair<string, Type>[] { new KeyValuePair<string, Type>("Key1", typeof(string)), new KeyValuePair<string, Type>("Key2", typeof(int)) };
            ImportCardinality expectedCardinality = ImportCardinality.ExactlyOne;
            CreationPolicy expectedCreationPolicy = CreationPolicy.NonShared;
            bool expectedRecomposable = true;

            ICompositionElement expectedOrigin = new MockOrigin();

            ExceptionAssert.ThrowsArgument<ArgumentException>("importingMember", () =>
            {
                ReflectionModelServices.CreateImportDefinition(
                expectedLazyMember,
                expectedContractName,
                expectedRequiredTypeIdentity,
                expectedRequiredMetadata,
                expectedCardinality,
                expectedRecomposable,
                expectedCreationPolicy,
                expectedOrigin);
            });
        }