예제 #1
0
        private static void DiscoverProperties(Type t, ValueHandler valueHandler, Dictionary <string, ResultBox> result)
        {
            IEnumerable <PropertyInfo> properties = GetHierarchyPublicProperties(t);

            foreach (PropertyInfo pi in properties)
            {
                ResultBox pbox = pi.PropertyType.GetTypeInfo().IsInterface
               ? (ResultBox) new ProxyResultBox(pi.Name, pi.PropertyType)
               : (ResultBox) new PropertyResultBox(pi.Name, pi.PropertyType);

                ValidateSupportedType(pbox, valueHandler);

                AddAttributes(pbox, pi, valueHandler);

                result[pi.Name] = pbox;
            }
        }
예제 #2
0
        private static void AddAttributes(ResultBox box, ValueHandler valueHandler, OptionAttribute optionAttribute, EncryptAttribute encryptAttribute)
        {
            object defaultValue = null;

            if (optionAttribute != null)
            {
                if (optionAttribute.Alias != null)
                {
                    box.StoreByName = optionAttribute.Alias;
                }

                //validate that types for default value match
                Type dvt = optionAttribute.DefaultValue?.GetType();
                if (optionAttribute.DefaultValue != null)
                {
                    if (dvt != box.ResultType && dvt != typeof(string))
                    {
                        throw new InvalidCastException($"Default value for option {box.Name} is of type {dvt.FullName} whereas the property has type {box.Name}. To fix this, either set default value to type {box.ResultType.FullName} or a string parseable to the target type.");
                    }

                    if (box.ResultType != typeof(string) && dvt == typeof(string))
                    {
                        valueHandler.TryParse(box.ResultType, (string)optionAttribute.DefaultValue, out defaultValue);
                    }
                }

                if (defaultValue == null)
                {
                    defaultValue = optionAttribute.DefaultValue;
                }
            }

            if (defaultValue == null)
            {
                defaultValue = GetDefaultValue(box.ResultType);
            }

            box.DefaultResult = defaultValue;

            if (encryptAttribute != null)
            {
                box.ShouldEncrypt = true;
            }
        }
예제 #3
0
      private static void DiscoverProperties(Type t, ValueHandler valueHandler, Dictionary<string, ResultBox> result, string basePath)
      {
         IEnumerable<PropertyInfo> properties = GetHierarchyPublicProperties(t);

         foreach (PropertyInfo pi in properties)
         {
            Type propertyType = pi.PropertyType;
            ResultBox rbox;
            bool isCollection = false;

            if(ResultBox.TryGetCollection(propertyType, out propertyType))
            {
               if(pi.SetMethod != null)
               {
                  throw new NotSupportedException($"Collection properties cannot have a setter. Detected at '{OptionPath.Combine(basePath, pi.Name)}'");
               }

               isCollection = true;
            }

            if(propertyType.GetTypeInfo().IsInterface)
            {
               rbox = new ProxyResultBox(pi.Name, propertyType);
            }
            else
            {
               rbox = new PropertyResultBox(pi.Name, propertyType);
            }

            ValidateSupportedType(rbox, valueHandler);

            AddAttributes(rbox, pi, valueHandler);

            //adjust to collection
            if(isCollection)
            {
               rbox = new CollectionResultBox(pi.Name, rbox);
            }

            result[pi.Name] = rbox;
         }
      }
예제 #4
0
 public CollectionResultBox(string name, ResultBox elementBox) : base(name, elementBox.ResultType, null)
 {
     _elementResultBox = elementBox;
 }
예제 #5
0
 private static void AddAttributes(ResultBox box, PropertyInfo pi, ValueHandler valueHandler)
 {
    AddAttributes(box, valueHandler, pi.GetCustomAttribute<OptionAttribute>());
 }
예제 #6
0
 private static void AddAttributes(ResultBox box, MethodInfo mi, ValueHandler valueHandler)
 {
    AddAttributes(box, valueHandler, mi.GetCustomAttribute<OptionAttribute>());
 }