/// <summary>
        /// Adds an enum value on the API enumeration type by selecting the CLR enum value that represents the API enumeration value.
        /// </summary>
        /// <typeparam name="TEnum">The CLR type of enum to select the CLR enum value on.</typeparam>
        /// <param name="clrEnumValueSelector">Expression that selects the CLR enum value on the CLR enum type.</param>
        /// <returns>A fluent-style API enumeration type builder for the API enumeration type.</returns>
        public static IApiEnumerationTypeBuilder <TEnum> ApiEnumerationValue <TEnum>(this IApiEnumerationTypeBuilder <TEnum> apiEnumerationTypeBuilder, Expression <Func <TEnum> > clrEnumValueSelector)
            where TEnum : Enum
        {
            Contract.Requires(clrEnumValueSelector != null);

            return(apiEnumerationTypeBuilder.ApiEnumerationValue(clrEnumValueSelector, null));
        }
Esempio n. 2
0
        // PUBLIC METHODS ///////////////////////////////////////////////////
        #region IApiObjectTypeConvention Implementation
        public void Apply(IApiEnumerationTypeBuilder apiEnumerationTypeBuilder, ApiConventionSettings apiConventionSettings)
        {
            Contract.Requires(apiEnumerationTypeBuilder != null);

            // Call ApiEnumerationValue method on all the discoverable CLR enum values for the given CLR enum type.
            var clrEnumerationType = apiEnumerationTypeBuilder.ClrType;
            var clrEnumerationFieldInfoCollection = ClrEnumerationValueDiscoveryRules.GetClrEnumerationValues(clrEnumerationType);

            foreach (var clrFieldInfo in clrEnumerationFieldInfoCollection)
            {
                var clrName    = clrFieldInfo.Name;
                var clrOrdinal = (int)Enum.Parse(clrEnumerationType, clrName);

                apiEnumerationTypeBuilder.ApiEnumerationValue(clrName, clrOrdinal);
            }
        }
        // PRIVATE METHODS //////////////////////////////////////////////////
        #region Methods
        private static void HandleApiEnumerationValueAttribute(IApiEnumerationTypeBuilder apiEnumerationTypeBuilder,
                                                               ApiPrecedenceStack apiPrecedenceStack,
                                                               FieldInfo clrFieldInfo)
        {
            Contract.Requires(apiEnumerationTypeBuilder != null);
            Contract.Requires(apiPrecedenceStack != null);
            Contract.Requires(clrFieldInfo != null);

            var apiEnumerationValueAttribute = (ApiEnumerationValueAttribute)Attribute.GetCustomAttribute(clrFieldInfo, typeof(ApiEnumerationValueAttribute));

            if (apiEnumerationValueAttribute == null)
            {
                return;
            }

            IApiEnumerationValueBuilder ApiEnumerationValueConfiguration(IApiEnumerationValueBuilder apiEnumerationValueBuilder)
            {
                apiPrecedenceStack.Push(ApiPrecedenceLevel.Annotation);

                var apiName = apiEnumerationValueAttribute.Name;

                if (apiName != null)
                {
                    apiEnumerationValueBuilder.HasName(apiName);
                }

                var apiDescription = apiEnumerationValueAttribute.Description;

                if (apiDescription != null)
                {
                    apiEnumerationValueBuilder.HasDescription(apiDescription);
                }

                apiPrecedenceStack.Pop();

                return(apiEnumerationValueBuilder);
            }

            var clrEnumerationType = apiEnumerationTypeBuilder.ClrType;
            var clrName            = clrFieldInfo.Name;
            var clrOrdinal         = (int)Enum.Parse(clrEnumerationType, clrName);

            apiPrecedenceStack.Push(ApiPrecedenceLevel.Annotation);
            apiEnumerationTypeBuilder.ApiEnumerationValue(clrName, clrOrdinal, ApiEnumerationValueConfiguration);
            apiPrecedenceStack.Pop();
        }
        // PUBLIC METHODS ///////////////////////////////////////////////////
        #region Extension Methods
        /// <summary>
        /// Adds an enum value on the API enumeration type by the given CLR name and value that represents the API enumeration value.
        /// </summary>
        /// <param name="clrName">The CLR name of the CLR enum value</param>
        /// <param name="clrOrdinal">The CLR value of the CLR enum value</param>
        /// <returns>A fluent-style API enumeration type builder for the API enumeration type.</returns>
        public static IApiEnumerationTypeBuilder ApiEnumerationValue(this IApiEnumerationTypeBuilder apiEnumerationTypeBuilder, string clrName, int clrOrdinal)
        {
            Contract.Requires(clrName.SafeHasContent());

            return(apiEnumerationTypeBuilder.ApiEnumerationValue(clrName, clrOrdinal, null));
        }