FromAttribute() public static method

public static FromAttribute ( ValueAttribute attribute, System conversionType ) : ValueSpecification
attribute ValueAttribute
conversionType System
return ValueSpecification
示例#1
0
        public static Specification FromProperty(PropertyInfo property)
        {
            var attrs = property.GetCustomAttributes(true);
            var oa    = attrs.OfType <OptionAttribute>();

            if (oa.Count() == 1)
            {
                var spec = OptionSpecification.FromAttribute(oa.Single(), property.PropertyType,
                                                             property.PropertyType.IsEnum
                        ? Enum.GetNames(property.PropertyType)
                        : Enumerable.Empty <string>());
                if (spec.ShortName.Length == 0 && spec.LongName.Length == 0)
                {
                    return(spec.WithLongName(property.Name.ToLowerInvariant()));
                }
                return(spec);
            }

            var va = attrs.OfType <ValueAttribute>();

            if (va.Count() == 1)
            {
                return(ValueSpecification.FromAttribute(va.Single(), property.PropertyType,
                                                        property.PropertyType.IsEnum
                        ? Enum.GetNames(property.PropertyType)
                        : Enumerable.Empty <string>()));
            }

            throw new InvalidOperationException();
        }
示例#2
0
        public static Specification FromProperty(PropertyInfo property)
        {
            System.Collections.Generic.List <string> enumList = new System.Collections.Generic.List <string>();
            if (property.PropertyType.IsEnum)
            {
                enumList.AddRange(Enum.GetNames(property.PropertyType));
            }

            var attrs = property.GetCustomAttributes(true);
            var oa    = attrs.OfType <OptionAttribute>();

            if (oa.Count() == 1)
            {
                var spec = OptionSpecification.FromAttribute(oa.Single(), property.PropertyType, enumList);
                if (spec.ShortName.Length == 0 && spec.LongName.Length == 0)
                {
                    return(spec.WithLongName(property.Name.ToLowerInvariant(), enumList));
                }
                return(spec);
            }

            var va = attrs.OfType <ValueAttribute>();

            if (va.Count() == 1)
            {
                return(ValueSpecification.FromAttribute(va.Single(), property.PropertyType));
            }

            throw new InvalidOperationException();
        }