/// <summary> /// Initializes a new instance of the <see cref="MethodBasedExportDefinition"/> class. /// </summary> /// <param name="contractName">The contract name that is used to identify the current export.</param> /// <param name="declaringType">The type which declares the method on which the import is placed.</param> /// <param name="method">The method for which the current object stores the serialized data.</param> private MethodBasedExportDefinition( string contractName, TypeIdentity declaringType, MethodDefinition method) : base(contractName, declaringType) { { Debug.Assert(method != null, "The method object should not be null."); } m_Method = method; }
/// <summary> /// Initializes a new instance of the <see cref="PropertyBasedExportDefinition"/> class. /// </summary> /// <param name="contractName">The contract name that is used to identify the current export.</param> /// <param name="declaringType">The type that declares the property on which the import is placed.</param> /// <param name="property">The property for which the current object stores the serialized data.</param> private PropertyBasedExportDefinition( string contractName, TypeIdentity declaringType, PropertyDefinition property) : base(contractName, declaringType) { { Debug.Assert(property != null, "The property object shouldn't be null."); } m_Property = property; }
/// <summary> /// Initializes a new instance of the <see cref="GroupPartDefinition"/> class. /// </summary> /// <param name="partType">The type of object for which this ID is valid.</param> /// <param name="number">The index of the object in the owning group.</param> /// <param name="exports">The collection of export registrations for the current object.</param> /// <param name="imports">The collection of import registrations for the current object.</param> /// <param name="actions">The collection of schedule action registrations for the current object.</param> /// <param name="conditions">The collection of schedule import registrations for the current object.</param> /// <exception cref="ArgumentNullException"> /// Thrown if <paramref name="partType"/> is <see langword="null" />. /// </exception> public GroupPartDefinition( TypeIdentity partType, int number, Dictionary<ExportRegistrationId, SerializableExportDefinition> exports, Dictionary<ImportRegistrationId, SerializableImportDefinition> imports, Dictionary<ScheduleActionRegistrationId, ScheduleActionDefinition> actions, Dictionary<ScheduleConditionRegistrationId, ScheduleConditionDefinition> conditions) { { Lokad.Enforce.Argument(() => partType); } m_Id = new PartRegistrationId(partType.AssemblyQualifiedName, number); m_Type = partType; m_Index = number; m_Exports = exports ?? new Dictionary<ExportRegistrationId, SerializableExportDefinition>(); m_Imports = imports ?? new Dictionary<ImportRegistrationId, SerializableImportDefinition>(); m_Actions = actions ?? new Dictionary<ScheduleActionRegistrationId, ScheduleActionDefinition>(); m_Conditions = conditions ?? new Dictionary<ScheduleConditionRegistrationId, ScheduleConditionDefinition>(); }
/// <summary> /// Initializes a new instance of the <see cref="PropertyBasedImportDefinition"/> class. /// </summary> /// <param name="contractName">The contract name that is used to identify the current import.</param> /// <param name="requiredTypeIdentity">The type identity of the export type expected.</param> /// <param name="cardinality"> /// One of the enumeration values that indicates the cardinality of the export object required by the import definition. /// </param> /// <param name="isRecomposable"> /// <see langword="true" /> to specify that the import definition can be satisfied multiple times throughout the lifetime of a parts; /// otherwise, <see langword="false" />. /// </param> /// <param name="creationPolicy"> /// A value that indicates that the importer requires a specific creation policy for the exports used to satisfy this import. /// </param> /// <param name="declaringType">The type that defines the property.</param> /// <param name="property">The property for which the current object stores the serialized data.</param> private PropertyBasedImportDefinition( string contractName, TypeIdentity requiredTypeIdentity, ImportCardinality cardinality, bool isRecomposable, CreationPolicy creationPolicy, TypeIdentity declaringType, PropertyDefinition property) : base(contractName, requiredTypeIdentity, cardinality, isRecomposable, false, creationPolicy, declaringType) { { Lokad.Enforce.Argument(() => property); } m_Property = property; }
/// <summary> /// Initializes a new instance of the <see cref="ConstructorBasedImportDefinition"/> class. /// </summary> /// <param name="contractName">The contract name that is used to identify the current import.</param> /// <param name="requiredTypeIdentity">The type identity of the export type expected.</param> /// <param name="cardinality"> /// One of the enumeration values that indicates the cardinality of the export object required by the import definition. /// </param> /// <param name="creationPolicy"> /// A value that indicates that the importer requires a specific creation policy for the exports used to satisfy this import. /// </param> /// <param name="declaringType">The type that declares the constructor on which the import is placed.</param> /// <param name="constructor">The constructor that declares the import.</param> /// <param name="parameter">The parameter on which the import is defined.</param> private ConstructorBasedImportDefinition( string contractName, TypeIdentity requiredTypeIdentity, ImportCardinality cardinality, CreationPolicy creationPolicy, TypeIdentity declaringType, ConstructorDefinition constructor, ParameterDefinition parameter) : base(contractName, requiredTypeIdentity, cardinality, false, true, creationPolicy, declaringType) { { Lokad.Enforce.Argument(() => parameter); } m_Constructor = constructor; m_Parameter = parameter; }
/// <summary> /// Returns the <see cref="TypeDefinition"/> for the given type. /// </summary> /// <param name="type">The type.</param> /// <returns>The requested type definition.</returns> public TypeDefinition TypeByIdentity(TypeIdentity type) { return(m_Repository.TypeByIdentity(type)); }
public bool IsSubTypeOf(TypeIdentity parent, TypeIdentity child) { return m_Repository.IsSubTypeOf(parent, child); }
public bool IsSubTypeOf(TypeIdentity parent, TypeIdentity child) { return(m_Repository.IsSubTypeOf(parent, child)); }
/// <summary> /// Initializes a new instance of the <see cref="TypeBasedExportDefinition"/> class. /// </summary> /// <param name="contractName">The contract name that is used to identify the current export.</param> /// <param name="declaringType">The type that owns the current export.</param> private TypeBasedExportDefinition(string contractName, TypeIdentity declaringType) : base(contractName, declaringType) { }
/// <summary> /// Returns the part that has the given type as declaring type. /// </summary> /// <param name="type">The declaring type.</param> /// <returns>The requested part.</returns> public PartDefinition Part(TypeIdentity type) { lock (m_Lock) { { Lokad.Enforce.Argument(() => type); Lokad.Enforce.With<UnknownPartDefinitionException>( m_Parts.ContainsKey(type), Resources.Exceptions_Messages_UnknownPartDefinition); } return m_Parts[type].Item1; } }
public bool ContainsDefinitionForType(TypeIdentity type) { return(m_Repository.ContainsDefinitionForType(type)); }
/// <summary> /// Creates a new instance of the <see cref="ConstructorBasedImportDefinition"/> class /// based on the given <see cref="ParameterInfo"/>. /// </summary> /// <param name="contractName">The contract name that is used to identify the current import.</param> /// <param name="requiredTypeIdentity">The type identity of the export type expected.</param> /// <param name="cardinality"> /// One of the enumeration values that indicates the cardinality of the export object required by the import definition. /// </param> /// <param name="creationPolicy"> /// A value that indicates that the importer requires a specific creation policy for the exports used to satisfy this import. /// </param> /// <param name="parameter">The method for which the current object stores the serialized data.</param> /// <returns>The serialized definition for the given parameter.</returns> /// <exception cref="ArgumentNullException"> /// Thrown if <paramref name="parameter"/> is <see langword="null" />. /// </exception> public static ConstructorBasedImportDefinition CreateDefinition( string contractName, TypeIdentity requiredTypeIdentity, ImportCardinality cardinality, CreationPolicy creationPolicy, ParameterInfo parameter) { return CreateDefinition( contractName, requiredTypeIdentity, cardinality, creationPolicy, parameter, t => TypeIdentity.CreateDefinition(t)); }
public bool ContainsDefinitionForType(TypeIdentity type) { lock (m_Lock) { return (type != null) && m_Types.ContainsKey(type); } }
/// <summary> /// Returns the part that has the given type as declaring type. /// </summary> /// <param name="type">The declaring type.</param> /// <returns>The requested part.</returns> public PartDefinition Part(TypeIdentity type) { return m_Repository.Part(type); }
public bool ContainsDefinitionForType(TypeIdentity type) { return m_Repository.ContainsDefinitionForType(type); }
public static bool OpenGenericIsAssignableFrom( TypeIdentity openGeneric, TypeDefinition type, Func<TypeIdentity, TypeDefinition> toDefinition) { // Terminate recursion if ((type == null) || openGeneric.Equals(type)) { return false; } // typeToCheck is a closure of openGenericType var isClosureOfGenericType = type.Identity.IsGenericType && openGeneric.Equals(type.GenericTypeDefinition); // typeToCheck is the subclass of a closure of openGenericType var isSubClassOfClosure = OpenGenericIsAssignableFrom(openGeneric, toDefinition(type.BaseType), toDefinition); // typeToCheck inherits from an interface which is the closure of openGenericType var inheritsClosureInterface = type.BaseInterfaces.Any( interfaceType => OpenGenericIsAssignableFrom(openGeneric, toDefinition(interfaceType), toDefinition)); return isClosureOfGenericType || isSubClassOfClosure || inheritsClosureInterface; }
/// <summary> /// Returns the part that has the given type as declaring type. /// </summary> /// <param name="type">The declaring type.</param> /// <returns>The requested part.</returns> public PartDefinition Part(TypeIdentity type) { return(m_Repository.Part(type)); }
public static PropertyBasedImportDefinition CreateDefinition( string contractName, TypeIdentity requiredTypeIdentity, ImportCardinality cardinality, bool isRecomposable, CreationPolicy creationPolicy, PropertyInfo property) { return CreateDefinition( contractName, requiredTypeIdentity, cardinality, isRecomposable, creationPolicy, property, t => TypeIdentity.CreateDefinition(t)); }
public static PropertyBasedImportDefinition CreateDefinition( string contractName, TypeIdentity requiredTypeIdentity, ImportCardinality cardinality, bool isRecomposable, CreationPolicy creationPolicy, PropertyInfo property, Func<Type, TypeIdentity> identityGenerator) { { Lokad.Enforce.Argument(() => property); Lokad.Enforce.Argument(() => identityGenerator); } return new PropertyBasedImportDefinition( contractName, requiredTypeIdentity, cardinality, isRecomposable, creationPolicy, identityGenerator(property.DeclaringType), PropertyDefinition.CreateDefinition(property, identityGenerator)); }
private static IEnumerable <PartDefinition> CreatePluginTypes() { var plugins = new List <PartDefinition> { new PartDefinition { Identity = TypeIdentity.CreateDefinition(typeof(ActionOnMethod)), Exports = new List <SerializableExportDefinition> { TypeBasedExportDefinition.CreateDefinition("ActionExport", typeof(ActionOnMethod)) }, Imports = new List <SerializableImportDefinition>(), Actions = new List <ScheduleActionDefinition> { ScheduleActionDefinition.CreateDefinition( "ActionMethod", typeof(ActionOnMethod).GetMethod("ActionMethod")) }, Conditions = new List <ScheduleConditionDefinition>(), }, new PartDefinition { Identity = TypeIdentity.CreateDefinition(typeof(ConditionOnMethod)), Exports = new List <SerializableExportDefinition> { TypeBasedExportDefinition.CreateDefinition("ConditionOnMethodExport", typeof(ConditionOnMethod)) }, Imports = new List <SerializableImportDefinition>(), Actions = new List <ScheduleActionDefinition>(), Conditions = new List <ScheduleConditionDefinition> { MethodBasedScheduleConditionDefinition.CreateDefinition( "OnMethod", typeof(ConditionOnMethod).GetMethod("ConditionMethod")) }, }, new PartDefinition { Identity = TypeIdentity.CreateDefinition(typeof(ConditionOnProperty)), Exports = new List <SerializableExportDefinition> { TypeBasedExportDefinition.CreateDefinition("ConditionOnPropertyExport", typeof(ConditionOnProperty)) }, Imports = new List <SerializableImportDefinition>(), Actions = new List <ScheduleActionDefinition>(), Conditions = new List <ScheduleConditionDefinition> { PropertyBasedScheduleConditionDefinition.CreateDefinition( "OnProperty", typeof(ConditionOnProperty).GetProperty("ConditionProperty")) }, }, new PartDefinition { Identity = TypeIdentity.CreateDefinition(typeof(ExportOnProperty)), Exports = new List <SerializableExportDefinition> { PropertyBasedExportDefinition.CreateDefinition( typeof(IExportingInterface).FullName, typeof(ExportOnProperty).GetProperty("ExportingProperty")) }, Imports = new List <SerializableImportDefinition>(), Actions = new List <ScheduleActionDefinition>(), Conditions = new List <ScheduleConditionDefinition>(), }, new PartDefinition { Identity = TypeIdentity.CreateDefinition(typeof(ImportOnProperty)), Exports = new List <SerializableExportDefinition> { TypeBasedExportDefinition.CreateDefinition(typeof(ImportOnProperty).FullName, typeof(ImportOnProperty)) }, Imports = new List <SerializableImportDefinition> { PropertyBasedImportDefinition.CreateDefinition( typeof(IExportingInterface).FullName, TypeIdentity.CreateDefinition(typeof(IExportingInterface)), ImportCardinality.ExactlyOne, false, CreationPolicy.Shared, typeof(ImportOnProperty).GetProperty("ImportingProperty")) }, Actions = new List <ScheduleActionDefinition>(), Conditions = new List <ScheduleConditionDefinition>(), } }; return(plugins); }
/// <summary> /// Returns the <see cref="TypeDefinition"/> for the given type. /// </summary> /// <param name="type">The type.</param> /// <returns>The requested type definition.</returns> public TypeDefinition TypeByIdentity(TypeIdentity type) { return m_Repository.TypeByIdentity(type); }
private bool AvailableTypeMatchesRequiredType(TypeIdentity requiredType, TypeIdentity availableType) { return (availableType != null) && (requiredType.Equals(availableType) || m_Repository.IsSubTypeOf(requiredType, availableType)); }
/// <summary> /// Creates a new instance of the <see cref="ConstructorBasedImportDefinition"/> class based /// on the given <see cref="ParameterInfo"/>. /// </summary> /// <param name="contractName">The contract name that is used to identify the current import.</param> /// <param name="requiredTypeIdentity">The type identity of the export type expected.</param> /// <param name="cardinality"> /// One of the enumeration values that indicates the cardinality of the export object required by the import definition. /// </param> /// <param name="creationPolicy"> /// A value that indicates that the importer requires a specific creation policy for the exports used to satisfy this import. /// </param> /// <param name="parameter">The method for which the current object stores the serialized data.</param> /// <param name="identityGenerator">The function that creates type identities.</param> /// <returns>The serialized definition for the given parameter.</returns> /// <exception cref="ArgumentNullException"> /// Thrown if <paramref name="parameter"/> is <see langword="null" />. /// </exception> /// <exception cref="ArgumentNullException"> /// Thrown if <paramref name="identityGenerator"/> is <see langword="null" />. /// </exception> public static ConstructorBasedImportDefinition CreateDefinition( string contractName, TypeIdentity requiredTypeIdentity, ImportCardinality cardinality, CreationPolicy creationPolicy, ParameterInfo parameter, Func<Type, TypeIdentity> identityGenerator) { { Lokad.Enforce.Argument(() => parameter); Lokad.Enforce.Argument(() => identityGenerator); } return new ConstructorBasedImportDefinition( contractName, requiredTypeIdentity, cardinality, creationPolicy, identityGenerator(parameter.Member.DeclaringType), ConstructorDefinition.CreateDefinition(parameter.Member as ConstructorInfo, identityGenerator), ParameterDefinition.CreateDefinition(parameter, identityGenerator)); }
private bool ExportMatchesActionImport(TypeIdentity importType, SerializableExportDefinition exportDefinition) { Debug.Assert(importType.TypeArguments.Count() > 0, "Action<T> should have at least 1 generic type argument."); var typeArguments = importType.TypeArguments.ToList(); // Export is a method that matches the signature of the Action<T> var methodExport = exportDefinition as MethodBasedExportDefinition; if (methodExport != null) { if (methodExport.Method.ReturnType != null) { return false; } var parameters = methodExport.Method.Parameters.ToList(); if (parameters.Count != typeArguments.Count) { return false; } for (int i = 0; i < typeArguments.Count; i++) { if (!AvailableTypeMatchesRequiredType(typeArguments[i], parameters[i].Identity)) { return false; } } return true; } return false; }
private bool ExportMatchesCollectionImport(TypeIdentity importType, TypeIdentity exportType, Func<TypeIdentity, TypeDefinition> toDefinition) { if (importType.TypeArguments.Count() != 1) { return false; } var genericType = importType.TypeArguments.First(); if (AvailableTypeMatchesRequiredType(genericType, exportType)) { return true; } // Handle IEnumerable<Lazy<T, TMeta>> if (toDefinition(genericType).IsLazy(toDefinition)) { return ExportMatchesLazyImport(genericType, exportType); } return false; }
private bool ExportMatchesFuncImport(TypeIdentity importType, TypeIdentity exportType, SerializableExportDefinition exportDefinition) { Debug.Assert(importType.TypeArguments.Count() > 0, "Func<T> should have at least 1 generic type argument."); var typeArguments = importType.TypeArguments.ToList(); if (typeArguments.Count == 1) { // The exported type matches the T of Func<T>or the export is a property that returns T (in Func<T>) var genericType = typeArguments[0]; if (AvailableTypeMatchesRequiredType(genericType, exportType)) { return true; } } // Export is a method that matches the signature of the Func<T> var methodExport = exportDefinition as MethodBasedExportDefinition; if (methodExport != null) { var returnType = methodExport.Method.ReturnType; if (!AvailableTypeMatchesRequiredType(typeArguments.Last(), returnType)) { return false; } var parameters = methodExport.Method.Parameters.ToList(); if (parameters.Count != (typeArguments.Count - 1)) { return false; } for (int i = 0; i < typeArguments.Count - 1; i++) { if (!AvailableTypeMatchesRequiredType(typeArguments[i], parameters[i].Identity)) { return false; } } return true; } return false; }
public bool IsSubTypeOf(TypeIdentity parent, TypeIdentity child) { var algorithm = new HoffmanPavleyRankedShortestPathAlgorithm<TypeIdentity, Edge<TypeIdentity>>( m_TypeGraph, e => 1.0); algorithm.ShortestPathCount = 10; algorithm.Compute(child, parent); return algorithm.ComputedShortestPathCount > 0; }
private bool ExportMatchesLazyImport(TypeIdentity importType, TypeIdentity exportType) { Debug.Assert(importType.TypeArguments.Count() >= 1, "Lazy<T> / Lazy<T, TMeta> should have at least 1 generic type argument"); var genericType = importType.TypeArguments.First(); return AvailableTypeMatchesRequiredType(genericType, exportType); }
/// <summary> /// Returns the <see cref="TypeDefinition"/> for the given type. /// </summary> /// <param name="type">The type.</param> /// <returns>The requested type definition.</returns> public TypeDefinition TypeByIdentity(TypeIdentity type) { lock (m_Lock) { { Lokad.Enforce.Argument(() => type); Lokad.Enforce.With<UnknownTypeDefinitionException>( m_Types.ContainsKey(type), Resources.Exceptions_Messages_UnknownTypeDefinition); } return m_Types[type]; } }
private static bool IsSubTypeOf(List<TypeDefinition> types, TypeIdentity parent, TypeIdentity child) { var childDef = types.Find(t => t.Identity.Equals(child)); var directParent = childDef.BaseType; while (directParent != null) { if (parent.Equals(directParent)) { return true; } var directParentDef = types.Find(t => t.Identity.Equals(directParent)); directParent = directParentDef.BaseType; } foreach (var baseInterface in childDef.BaseInterfaces) { if (parent.Equals(baseInterface)) { return true; } } return false; }