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); }
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)); }
private MemberInfo[] GetAccessors(LazyMemberInfo originalLazyMember) { BuildTables(); Assumes.NotNull(_membersTable); return(_membersTable[originalLazyMember]); }
public void Constructor_PassAccessorsWithNulls() { Assert.Throws <ArgumentException>("accessors", () => { LazyMemberInfo lazy = new LazyMemberInfo(MemberTypes.Field, new MemberInfo[] { null, null }); }); }
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()); } }
public void Constructor_PassNullAccessorCreators() { ExceptionAssert.ThrowsArgument <ArgumentNullException>("accessorsCreator", () => { LazyMemberInfo lazy = new LazyMemberInfo(MemberTypes.Field, (Func <MemberInfo[]>)null); }); }
public void Constructor_PassNullAccessors() { Assert.Throws <ArgumentNullException>("accessors", () => { LazyMemberInfo lazy = new LazyMemberInfo(MemberTypes.Field, (MemberInfo[])null); }); }
public void Constructor_PassNullMember() { ExceptionAssert.ThrowsArgument<ArgumentNullException>("member", () => { LazyMemberInfo lazy = new LazyMemberInfo((MemberInfo)null); }); }
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); }
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]); }
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()); }
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; }
public ReflectionMemberExportDefinition(LazyMemberInfo member, ExportDefinition exportDefinition, ICompositionElement?origin) { if (exportDefinition == null) { throw new ArgumentNullException(nameof(exportDefinition)); } _member = member; _exportDefinition = exportDefinition; _origin = origin; }
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); }
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)); }
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())); } }
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); }
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)); }); } } }
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)); }
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; }
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; }
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); }
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)); } }
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); } }
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) }; }
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; }
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 !); }
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); }
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); }); } } }
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); }
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); }
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]; }
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); }); }
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)); }
public void Constructor_PassNullAccessorCreators() { ExceptionAssert.ThrowsArgument<ArgumentNullException>("accessorsCreator", () => { LazyMemberInfo lazy = new LazyMemberInfo(MemberTypes.Field, (Func<MemberInfo[]>)null); }); }
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); }); }
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); }
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); }
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)); }); } } }
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); }); }