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, CreationPolicy.NonShared, expectedPartDefinition); Assert.AreSame(expectedPartDefinition, ((ICompositionElement)definition).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); }
public void Constructor_WithNullRequiredMetadata() { LazyMemberInfo member = CreateLazyMemberInfo(); ReflectionMemberImportDefinition definition = new ReflectionMemberImportDefinition( member, "Contract", (string)null, null, ImportCardinality.ZeroOrMore, true, CreationPolicy.NonShared, null); Assert.IsNotNull(definition.RequiredMetadata); Assert.AreEqual(0, definition.RequiredMetadata.Count()); }
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()); }
public static LazyMemberInfo GetImportingMember(ImportDefinition importDefinition) { Requires.NotNull(importDefinition, "importDefinition"); ReflectionMemberImportDefinition reflectionMemberImportDefinition = importDefinition as ReflectionMemberImportDefinition; if (reflectionMemberImportDefinition == null) { throw new ArgumentException( string.Format(CultureInfo.CurrentCulture, Strings.ReflectionModel_InvalidMemberImportDefinition, importDefinition.GetType()), "importDefinition"); } return(reflectionMemberImportDefinition.ImportingLazyMember); }
public static LazyMemberInfo GetImportingMember(ImportDefinition importDefinition) { Requires.NotNull(importDefinition, nameof(importDefinition)); ReflectionMemberImportDefinition reflectionMemberImportDefinition = importDefinition as ReflectionMemberImportDefinition; if (reflectionMemberImportDefinition == null) { throw new ArgumentException( SR.Format(SR.ReflectionModel_InvalidMemberImportDefinition, importDefinition.GetType()), nameof(importDefinition)); } return(reflectionMemberImportDefinition.ImportingLazyMember); }
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 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())); }
private ImportDefinition TranslateImport(ReflectionImportDefinition reflectionImport, List <LazyMemberInfo> members, List <Lazy <ParameterInfo> > parameters) { bool isExportFactory = false; ContractBasedImportDefinition productImport = reflectionImport; if (reflectionImport is IPartCreatorImportDefinition exportFactoryImportDefinition) { productImport = exportFactoryImportDefinition.ProductImportDefinition; isExportFactory = true; } string contractName = Translate(productImport.ContractName); string requiredTypeIdentity = Translate(productImport.RequiredTypeIdentity !); IDictionary <string, object?> metadata = TranslateImportMetadata(productImport); ImportDefinition?import = null; if (reflectionImport is ReflectionMemberImportDefinition memberImport) { 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; if (parameterImport == null) { throw new Exception(SR.Diagnostic_InternalExceptionMessage); } 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 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; }