ContractBasedImportDefinition GetProductImportDefinition(ContractBasedImportDefinition cbid, Type createdType, Type metadataViewType) { var productContractName = cbid.ContractName == cbid.RequiredTypeIdentity ? AttributedModelServices.GetContractName(createdType) : cbid.ContractName; return(new ContractBasedImportDefinition( productContractName, AttributedModelServices.GetTypeIdentity(createdType), GetProductRequiredMetadataKeys(cbid.RequiredMetadata, metadataViewType), cbid.Cardinality, cbid.IsRecomposable, cbid.IsPrerequisite, CreationPolicy.NonShared)); }
private IEnumerable <ExportDefinition> GetExportDefinitions(Type implementationType, Type contractType) { var lazyMember = new LazyMemberInfo(implementationType); var contracName = AttributedModelServices.GetContractName(contractType); var metadata = new Lazy <IDictionary <string, object> >(() => { var md = new Dictionary <string, object>(); md.Add(CompositionConstants.ExportTypeIdentityMetadataName, AttributedModelServices.GetTypeIdentity(contractType)); return(md); }); return(new [] { ReflectionModelServices.CreateExportDefinition(lazyMember, contracName, metadata, null) }); }
protected ConfigurationPart(IEnumerable <FileInfo> configurationFileInfos, Type configurationType, object configurationInstance) { ConfigurationType = configurationType; ConfigurationInstance = configurationInstance; _configurationFileInfos = configurationFileInfos; var metadata = new Dictionary <string, object>() { { "ExportTypeIdentity", AttributedModelServices.GetTypeIdentity(ConfigurationType) } }; var contractName = AttributedModelServices.GetContractName(ConfigurationType); ExportDefinition = new ExportDefinition(contractName, metadata); PopulateFromFiles(configurationInstance); }
public void Register(Func <object> factory, Type serviceType, string contract = null) { var batch = new CompositionBatch(); if (contract == null) { contract = AttributedModelServices.GetContractName(serviceType); } var typeIdentity = AttributedModelServices.GetTypeIdentity(serviceType); var metadata = new Dictionary <string, object> { ["ExportTypeIdentity"] = typeIdentity, }; batch.AddExport(new Export(contract, metadata, factory)); _container.Compose(batch); }
public static string GetDisplayName(this MemberInfo member) { if (member == null) { throw new ArgumentNullException(nameof(member)); } switch (member.MemberType) { case MemberTypes.TypeInfo: case MemberTypes.NestedType: return(AttributedModelServices.GetTypeIdentity(((Type)member))); } return(GetDisplayName(member.DeclaringType, member.Name)); }
/// <summary> /// Gets type identity for the provided <see cref="IExportConvention"/>. /// </summary> /// <param name="exportConvention">The <see cref="IExportConvention"/> that the type identity should be retreived for.</param> /// <param name="member">The <see cref="MemberInfo"/> that is being exported.</param> /// <returns>A <see cref="string"/> containing the type identity for the export.</returns> /// <exception cref="ArgumentNullException">The value of the <paramref name="member"/> or <paramref name="exportConvention"/> parameter was <see langword="null"/>.</exception> public override string GetExportTypeIdentity(IExportConvention exportConvention, MemberInfo member) { if (member == null) { throw new ArgumentNullException("member", "The member cannot be null."); } var contractMember = member.GetContractMember(); var defaultContractType = this.DefaultConventions.Where(x => x.TargetType.Equals(contractMember)) .Select(x => x.ContractType).LastOrDefault(); return(defaultContractType != null? AttributedModelServices.GetTypeIdentity(defaultContractType) : base.GetExportTypeIdentity(exportConvention, member)); }
public GenericSpecializationPartCreationInfo(IReflectionPartCreationInfo originalPartCreationInfo, ReflectionComposablePartDefinition originalPart, Type[] specialization) { ArgumentNullException.ThrowIfNull(originalPartCreationInfo); ArgumentNullException.ThrowIfNull(originalPart); ArgumentNullException.ThrowIfNull(specialization); _originalPartCreationInfo = originalPartCreationInfo; _originalPart = originalPart; _specialization = specialization; _specializationIdentities = new string[_specialization.Length]; for (int i = 0; i < _specialization.Length; i++) { _specializationIdentities[i] = AttributedModelServices.GetTypeIdentity(_specialization[i]); } _lazyPartType = new Lazy <Type>( () => _originalPartCreationInfo.GetPartType().MakeGenericType(specialization), LazyThreadSafetyMode.PublicationOnly); }
internal static string GetTypeIdentityFromExport(this MemberInfo member, Type typeIdentityType) { if (typeIdentityType != null) { string typeIdentity = AttributedModelServices.GetTypeIdentity(typeIdentityType); if (typeIdentityType.ContainsGenericParameters) { typeIdentity = AdjustTypeIdentity(typeIdentity, typeIdentityType); } return(typeIdentity); } else { MethodInfo method = member as MethodInfo; Assumes.NotNull(method); return(AttributedModelServices.GetTypeIdentity(method)); } }
public GenericSpecializationPartCreationInfo(IReflectionPartCreationInfo originalPartCreationInfo, ReflectionComposablePartDefinition originalPart, Type[] specialization) { Assumes.NotNull(originalPartCreationInfo); Assumes.NotNull(specialization); Assumes.NotNull(originalPart); this._originalPartCreationInfo = originalPartCreationInfo; this._originalPart = originalPart; this._specialization = specialization; this._specializationIdentities = new string[this._specialization.Length]; for (int i = 0; i < this._specialization.Length; i++) { this._specializationIdentities[i] = AttributedModelServices.GetTypeIdentity(this._specialization[i]); } this._lazyPartType = new Lazy <Type>( () => this._originalPartCreationInfo.GetPartType().MakeGenericType(specialization), LazyThreadSafetyMode.PublicationOnly); }
private CompositionBatch CreateBatch(IEnumerable <Tuple <Type, object> > parts) { var batch = new CompositionBatch(); foreach (var item in parts) { var contractName = AttributedModelServices.GetContractName(item.Item1); var typeIdentity = AttributedModelServices.GetTypeIdentity(item.Item1); batch.AddExport(new Export(contractName, new Dictionary <string, object> { { "ExportTypeIdentity", typeIdentity } }, () => item.Item2)); } batch.AddExportedValue <ICompositionService>(container); return(batch); }
/// <summary> /// Returns <c>true</c> if the type given by <paramref name="implementationType"/> is already available as an export /// of <paramref cref="contractType"/> in the container. /// </summary> /// <param name="container"></param> /// <param name="contractType"></param> /// <param name="implementationType"></param> /// <returns></returns> bool IsTypeRegistered(TInternalContainer container, Type contractType, Type implementationType) { return(container.GetExports( new ContractBasedImportDefinition( AttributedModelServices.GetContractName(contractType), AttributedModelServices.GetTypeIdentity(contractType), null, ImportCardinality.ZeroOrMore, false, false, CreationPolicy.Any)) .Select(i => ReflectionModelServices.GetExportingMember(i.Definition)) .Where(i => // export must be associated with a Type, implemented by the specified type i.MemberType == MemberTypes.TypeInfo && i.GetAccessors().Any(j => ((TypeInfo)j).UnderlyingSystemType == implementationType.UnderlyingSystemType)) .Any()); }
internal static string GetContractNameFromExport(this MemberInfo member, ExportAttribute export) { if (!string.IsNullOrEmpty(export.ContractName)) { return(export.ContractName); } if (export.ContractType != null) { return(AttributedModelServices.GetContractName(export.ContractType)); } if (member.MemberType == MemberTypes.Method) { return(AttributedModelServices.GetTypeIdentity((MethodInfo)member)); } return(AttributedModelServices.GetContractName(member.GetDefaultTypeFromMember())); }
public void GetExportTypeIdentity_should_return_type_identity_from_convention_when_not_null() { var convention = new ExportConvention { ContractName = x => string.Empty, ContractType = x => typeof(string) }; var service = GetServiceWithoutDefaultConventions(); var result = service.GetExportTypeIdentity(convention, ReflectionServices.GetField <FakePart>(x => x.Count)); var expectedTypeIdentity = AttributedModelServices.GetTypeIdentity(typeof(string)); result.ShouldEqual(expectedTypeIdentity); }
public void ExportFactory_QueryContainerDirectly_ShouldWork() { var container = CreateWithAttributedCatalog(typeof(Foo)); var importDef = ReflectionModelServicesEx.CreateImportDefinition( new LazyMemberInfo(MemberTypes.Field, () => new MemberInfo[] { typeof(ExportFactoryTests) }), // Give it a bogus member AttributedModelServices.GetContractName(typeof(Foo)), AttributedModelServices.GetTypeIdentity(typeof(Foo)), Enumerable.Empty <KeyValuePair <string, Type> >(), ImportCardinality.ZeroOrMore, true, CreationPolicy.Any, true, // isExportFactory null); var exports = container.GetExports(importDef); var partCreator = exports.Single(); // Manually walk the steps of using a raw part creator which is modeled as a PartDefinition with // a single ExportDefinition. var partDef = (ComposablePartDefinition)partCreator.Value; var part = partDef.CreatePart(); var foo = (Foo)part.GetExportedValue(partDef.ExportDefinitions.Single()); Assert.NotNull(foo); var foo1 = (Foo)part.GetExportedValue(partDef.ExportDefinitions.Single()); Assert.Equal(foo, foo1); // creating a new part should result in getting a new exported value var part2 = partDef.CreatePart(); var foo2 = (Foo)part2.GetExportedValue(partDef.ExportDefinitions.Single()); Assert.NotEqual(foo, foo2); // Disposing of part should cause foo to be disposed ((IDisposable)part).Dispose(); Assert.True(foo.IsDisposed); }
internal static string GetTypeIdentityFromExport(this MemberInfo member, Type?typeIdentityType) { if (typeIdentityType != null) { string typeIdentity = AttributedModelServices.GetTypeIdentity(typeIdentityType); if (typeIdentityType.ContainsGenericParameters) { typeIdentity = AdjustTypeIdentity(typeIdentity, typeIdentityType); } return(typeIdentity); } else { MethodInfo?method = member as MethodInfo; if (method == null) { throw new Exception(SR.Diagnostic_InternalExceptionMessage); } return(AttributedModelServices.GetTypeIdentity(method)); } }
public void GetExportContractName_should_return_type_identity_of_method_type_when_called_with_method_and_contract_name_and_type_are_null() { var member = ReflectionServices.GetMethod <FakePart>(x => x.DoWork()); var convention = new ExportConvention { ContractName = null, ContractType = null, Members = x => new[] { member } }; var results = this.Service.GetExportContractName(convention, member); var expectedContractName = AttributedModelServices.GetTypeIdentity(member); results.ShouldEqual(expectedContractName); }
public void GetImportTypeIdentity_should_return_type_identity_of_default_contract_type_when_match_is_available() { var member = ReflectionServices.GetProperty <FakePart>(x => x.Name); var convention = new ImportConvention { ContractName = null, ContractType = null, Members = x => new[] { member } }; var result = GetServiceWithDefaultConventions().GetImportTypeIdentity(convention, member); var expectedTypeIdentity = AttributedModelServices.GetTypeIdentity(typeof(int)); result.ShouldEqual(expectedTypeIdentity); }
/// <summary> /// Gets the export contract name based on the provided hints. /// </summary> /// <param name="contractName">A <see cref="string"/> containing the name of the contract to use for the export.</param> /// <param name="contractType">The <see cref="Type"/> of the contract to use for the export.</param> /// <param name="member">A <see cref="MemberInfo"/> instance for the member that is being exported.</param> /// <returns>A <see cref="string"/> containing the contract name.</returns> /// <exception cref="ArgumentNullException">The value of the <paramref name="member"/> parameter was <see langword="null"/>.</exception> public static string GetExportContractName(string contractName, Type contractType, MemberInfo member) { if (member == null) { throw new ArgumentNullException("member", "The member cannot be null."); } if (contractName != null) { return(contractName); } if (contractType != null) { return(AttributedModelServices.GetContractName(contractType)); } return(member.MemberType == MemberTypes.Method ? AttributedModelServices.GetTypeIdentity((MethodInfo)member) : AttributedModelServices.GetContractName(member.GetContractMember())); }
/// <summary> /// Creates an import definition for the specified type by using the specified contract name and cardinality. /// </summary> /// <param name="type">The type to import into.</param> /// <param name="contractName">The contract name to use for the import.</param> /// <param name="cardinality">The cardinality of the import.</param> /// <returns>An import definition created for the specified type.</returns> internal static ImportDefinition CreateImportDefinition(Type type, string contractName, ImportCardinality cardinality) { string typeIdentity = null; if (type != typeof(object)) { typeIdentity = AttributedModelServices.GetTypeIdentity(type); } Dictionary <string, object> metadata = null; if (type.IsGenericType && !type.ContainsGenericParameters) { metadata = new Dictionary <string, object>(); metadata.Add(CompositionConstants.GenericContractMetadataName, AttributedModelServices.GetTypeIdentity(type.GetGenericTypeDefinition())); metadata.Add(CompositionConstants.GenericParametersMetadataName, type.GetGenericArguments()); } return(new ContractBasedImportDefinition(contractName, typeIdentity, null, cardinality, false, false, CreationPolicy.Any, metadata)); }
public void GetImportContractName_should_return_type_identity_of_convention_contract_type_when_contract_type_is_null() { var contractType = typeof(string); var convention = new ImportConvention { ContractName = null, ContractType = x => contractType }; var service = GetServiceWithoutDefaultConventions(); var result = service.GetImportContractName( convention, ReflectionServices.GetField <FakePart>(x => x.Count)); var expectedContractName = AttributedModelServices.GetTypeIdentity(contractType); result.ShouldEqual(expectedContractName); }
public void GetImportTypeIdentity_should_return_type_identity_of_convention_contract_type_when_contract_type_is_not_object_or_delegate() { var contractType = typeof(string); var convention = new ImportConvention() { ContractName = null, ContractType = x => contractType }; var service = GetServiceWithoutDefaultConventions(); var results = service.GetImportTypeIdentity( convention, ReflectionServices.GetProperty <FakePart>(x => x.Name)); var expectedTypeIdentity = AttributedModelServices.GetTypeIdentity(contractType); results.ShouldEqual(expectedTypeIdentity); }
public IFactory CreateFactory(string type) { var exports = container.GetExports(new ContractBasedImportDefinition( type, AttributedModelServices.GetTypeIdentity(typeof(IFactory)), null, ImportCardinality.ZeroOrMore, false, false, CreationPolicy.NonShared)).ToList(); if (exports == null || !exports.Any()) { throw new InvalidOperationException("Unknown tag type : " + type); } if (exports.Count > 1) { throw new InvalidOperationException("Know many tag type : " + type); } return((IFactory)exports.FirstOrDefault()?.Value); }
public IronPythonComposablePart(IronPythonTypeWrapper typeWrapper, IEnumerable <Type> exports, IEnumerable <KeyValuePair <string, IronPythonImportDefinition> > imports, IDictionary <object, object> exportMetadatas) { _typeWrapper = typeWrapper; _instance = typeWrapper.Activator(); _exports = new List <ExportDefinition>(exports.Count()); _imports = new Dictionary <string, ImportDefinition>(imports.Count()); foreach (var export in exports) { var metadata = new Dictionary <string, object>() { { "ExportTypeIdentity", AttributedModelServices.GetTypeIdentity(export) } }; if (exportMetadatas != null) { foreach (var data in exportMetadatas) { metadata.Add(data.Key.ToString(), data.Value); } } var contractName = AttributedModelServices.GetContractName(export); _exports.Add(new ExportDefinition(contractName, metadata)); } foreach (var import in imports) { var contractName = AttributedModelServices.GetContractName(import.Value.Type); var metadata = new Dictionary <string, Type>(); _imports[import.Key] = new IronPythonContractBasedImportDefinition( import.Key, contractName, AttributedModelServices.GetTypeIdentity(import.Value.Type), metadata.ToList(), import.Value.Cardinality, import.Value.IsRecomposable, import.Value.IsPrerequisite, CreationPolicy.Any); } }
public void GetExportTypeIdentity_should_return_type_identify_of_method_when_called_with_method_member_and_convention_contract_type_is_null() { var convention = new ExportConvention { ContractName = null, ContractType = null }; var member = ReflectionServices.GetMethod <FakePart>(x => x.DoWork()); var service = GetServiceWithoutDefaultConventions(); var result = service.GetExportTypeIdentity(convention, member); var expectedTypeIdentity = AttributedModelServices.GetTypeIdentity(member); result.ShouldEqual(expectedTypeIdentity); }
private IEnumerable <ImportDefinition> GetImportDefinitions(Type implementationType) { var imports = new List <ImportDefinition>(); var defaultConstructor = implementationType.GetConstructors().FirstOrDefault(); if (defaultConstructor != null) { foreach (var param in defaultConstructor.GetParameters()) { imports.Add( ReflectionModelServices.CreateImportDefinition( new Lazy <ParameterInfo>(() => param), AttributedModelServices.GetContractName(param.ParameterType), AttributedModelServices.GetTypeIdentity(param.ParameterType), Enumerable.Empty <KeyValuePair <string, Type> >(), ImportCardinality.ExactlyOne, CreationPolicy.Any, null)); } } return(imports); }
public static void ComposeExportedValue(this CompositionContainer container, string contractName, object exportedValue) { if (container == null) { throw new ArgumentNullException("container"); } if (exportedValue == null) { throw new ArgumentNullException("exportedValue"); } CompositionBatch batch = new CompositionBatch(); var metadata = new Dictionary <string, object> { { "ExportTypeIdentity", AttributedModelServices.GetTypeIdentity(exportedValue.GetType()) } }; batch.AddExport(new Export(contractName, metadata, () => exportedValue)); container.Compose(batch); }
private void RewriteContract(Type typeToDecorate, IList <ImportDefinition> importDefs, string newContract) { var importToDecorate = importDefs.SingleOrDefault(d => d.ContractName == myContractName); Contract.Requires(importToDecorate != null, "No import found for contract {0} on type {1}", myContractName, typeToDecorate); importDefs.Remove(importToDecorate); Contract.Invariant(importToDecorate.Cardinality == ImportCardinality.ExactlyOne, "Decoration of Cardinality " + importToDecorate.Cardinality + " not supported"); Contract.Invariant(ReflectionModelServices.IsImportingParameter(importToDecorate), "Decoration of property injection not supported"); var param = ReflectionModelServices.GetImportingParameter(importToDecorate); var importDef = ReflectionModelServices.CreateImportDefinition( param, newContract, AttributedModelServices.GetTypeIdentity(param.Value.ParameterType), Enumerable.Empty <KeyValuePair <string, Type> >(), importToDecorate.Cardinality, CreationPolicy.Any, null); importDefs.Add(importDef); }
public void GetImportTypeIdentity_should_return_type_identity_of_invoke_method_when_convention_contract_type_is_delegate() { var member = ReflectionServices.GetProperty <FakePart>(x => x.Delegate); var convention = new ImportConvention() { ContractName = null, ContractType = null, Members = x => new[] { member } }; var service = GetServiceWithoutDefaultConventions(); var results = service.GetImportTypeIdentity(convention, member); var expectedTypeIdentity = AttributedModelServices.GetTypeIdentity(member.PropertyType.GetMethod("Invoke")); results.ShouldEqual(expectedTypeIdentity); }
/// <summary> /// Gets import type identity based on the provided hints. /// </summary> /// <param name="contractType">The <see cref="Type"/> of the type identity to use for the import.</param> /// <param name="member">A <see cref="MemberInfo"/> instance for the member that is being imported.</param> /// <returns>A <see cref="string"/> containing the type identity.</returns> /// <exception cref="ArgumentNullException">The value of the <paramref name="member"/> parameter was <see langword="null"/>.</exception> public static string GetImportTypeIdentity(Type contractType, MemberInfo member) { if (member == null) { throw new ArgumentNullException("member", "The member cannot be null."); } if (contractType == null) { return(AttributedModelServices.GetTypeIdentity(member.GetContractMember())); } if (contractType.Equals(typeof(object))) { return(null); } var memberType = member.GetContractMember(); return(memberType.IsSubclassOf(typeof(Delegate)) ? AttributedModelServices.GetTypeIdentity(memberType.GetMethod("Invoke")) : AttributedModelServices.GetTypeIdentity(contractType)); }
public Export GetValueByContract(string contractName, CreationPolicy policy) { if (Container == null) { return(null); } var viewModelTypeIdentity = AttributedModelServices.GetTypeIdentity(typeof(object)); var definition = new ContractBasedImportDefinition(contractName, viewModelTypeIdentity, null, ImportCardinality.ExactlyOne, false, false, policy); var vmExports = Container.GetExports(definition); var vmExport = vmExports.FirstOrDefault(); if (vmExport != null) { return(vmExport); } return(null); }