Exemplo n.º 1
0
        /// <summary>
        /// Creates the <see cref="RegistrationBuilder" /> builder instance to be used in
        /// conjunction with the project's <see cref="AssemblyCatalog" /> instance.
        /// </summary>
        /// <returns>
        /// The <see cref="RegistrationBuilder" /> builder instance to be used in
        /// conjunction with the project's <see cref="AssemblyCatalog" /> instance.
        /// </returns>
        /// <remarks>
        /// In a nutshell, the instance returned by this method represents the following
        /// export rules: "For each type defined within the current assembly that has the
        /// <see cref="OperationAttribute" /> applied to it, export all
        /// <see cref="IOperation{T}" /> interfaces that it implements; a class may
        /// implement the interface multiple times - for different generic arguments
        /// (operand types)."
        ///
        /// In addition, the metadata (i.e., name and symbol) expressed by individual
        /// classes' <see cref="OperationAttribute" /> instances is added accordingly.
        /// </remarks>
        private static RegistrationBuilder CreateRegistrationBuilder()
        {
            var  registrationBuilder = new RegistrationBuilder();
            Type currentType         = null;
            var  processedTypes      = new HashSet <Type>();

            registrationBuilder
            .ForTypesMatching(IsOperationType)
            .ExportInterfaces(IsOperationInterface, ExportInterface);
            return(registrationBuilder);

            bool IsOperationType(Type type)
            {
                currentType = type;
                return(GetOperationAttribute(type) != null);
            };

            void ExportInterface(Type interfaceType, ExportBuilder exportBuilder)
            {
                exportBuilder.AsContractType(interfaceType);
                if (processedTypes.Add(currentType))
                {
                    OperationAttribute attribute = GetOperationAttribute(currentType);
                    exportBuilder.AddMetadata(nameof(attribute.Name), attribute.Name);
                    exportBuilder.AddMetadata(nameof(attribute.Symbol), attribute.Symbol);
                }
            };
        }
Exemplo n.º 2
0
        /// <summary>
        /// Retrieves metadata for the operation(s) represented by the specified type
        /// (class).
        /// </summary>
        /// <param name="operationType">
        /// The type (class) representing the operation(s) to retrieve metadata for.
        /// </param>
        /// <returns>
        /// The metadata for the operation(s) represented by the specified type (class).
        /// </returns>
        public OperationMetadata GetMetadata(Type operationType)
        {
            OperationAttribute attribute =
                operationType.GetCustomAttribute <OperationAttribute>();

            return(new OperationMetadata(attribute.Name, attribute.Symbol));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Retrieves metadata for the operation(s) represented by the specified type
        /// (class).
        /// </summary>
        /// <param name="operationType">
        /// The type (class) representing the operation(s) to retrieve metadata for.
        /// </param>
        /// <returns>
        /// The metadata for the operation(s) represented by the specified type (class).
        /// </returns>
        public OperationMetadata GetMetadata(Type operationType)
        {
            OperationAttribute attribute =
                operationType.GetCustomAttribute <OperationAttribute>();

            return(new OperationMetadata(
                       new Dictionary <string, object>()
            {
                [nameof(attribute.Name)] = attribute.Name,
                [nameof(attribute.Symbol)] = attribute.Symbol
            }));
        }
        GetCustomAttributes(Type reflectedType, MemberInfo member)
        {
            IEnumerable <Attribute> attributes = Enumerable.Empty <Attribute>();

            if (member is Type type)
            {
                OperationAttribute operationAttribute =
                    type.GetCustomAttribute <OperationAttribute>();
                if (operationAttribute != null)
                {
                    attributes = this.GetExportAttributes(type, operationAttribute);
                }
            }
            return(attributes);
        }
        private IEnumerable <Attribute> GetExportAttributes(
            Type operationType, OperationAttribute operationAttribute)
        {
            var attributes = new List <Attribute>();

            attributes.AddRange(
                operationType
                .GetInterfaces()
                .Where(this.IsOperationInterface)
                .Select(interfaceType => new ExportAttribute(interfaceType)));
            attributes.Add(new ExportMetadataAttribute(
                               nameof(operationAttribute.Name), operationAttribute.Name));
            attributes.Add(new ExportMetadataAttribute(
                               nameof(operationAttribute.Symbol), operationAttribute.Symbol));
            return(attributes);
        }