Inheritance: Specification
コード例 #1
0
ファイル: Specification.cs プロジェクト: kbrooks/commandline
        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
ファイル: Specification.cs プロジェクト: paulwh/commandline
        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();
        }