Пример #1
0
        internal static void Revive(this PropertyInfo prop, object toRevive, ArgHook.HookContext context)
        {
            if (ArgRevivers.CanRevive(prop.PropertyType) && context.ArgumentValue != null)
            {
                try
                {
                    if (prop.PropertyType.IsEnum)
                    {
                        bool ignoreCase = true;

                        if (prop.HasAttr <ArgIgnoreCase>() && prop.Attr <ArgIgnoreCase>().IgnoreCase == false)
                        {
                            ignoreCase = true;
                        }

                        context.RevivedProperty = ArgRevivers.ReviveEnum(prop.PropertyType, context.ArgumentValue, ignoreCase);
                    }
                    else
                    {
                        context.RevivedProperty = ArgRevivers.Revive(prop.PropertyType, prop.GetArgumentName(), context.ArgumentValue);
                    }
                    prop.SetValue(toRevive, context.RevivedProperty, null);
                }
                catch (ArgException)
                {
                    throw;
                }
                catch (Exception ex)
                {
                    if (ex.InnerException != null && ex.InnerException is ArgException)
                    {
                        throw ex.InnerException;
                    }
                    else
                    {
                        if (prop.PropertyType.IsEnum)
                        {
                            throw new ArgException("'" + context.ArgumentValue + "' is not a valid value for " + prop.GetArgumentName() + ". Available values are [" + string.Join(", ", Enum.GetNames(prop.PropertyType)) + "]", ex);
                        }
                        else
                        {
                            throw new ArgException(ex.Message, ex);
                        }
                    }
                }
            }
            else if (ArgRevivers.CanRevive(prop.PropertyType) && prop.PropertyType == typeof(SecureStringArgument))
            {
                context.RevivedProperty = ArgRevivers.Revive(prop.PropertyType, prop.GetArgumentName(), context.ArgumentValue);
                prop.SetValue(toRevive, context.RevivedProperty, null);
            }
            else if (context.ArgumentValue != null)
            {
                throw new ArgException("Unexpected argument '" + prop.GetArgumentName() + "' with value '" + context.ArgumentValue + "'");
            }
        }
Пример #2
0
 internal void Revive(string commandLineValue)
 {
     if (ArgRevivers.CanRevive(ArgumentType) && commandLineValue != null)
     {
         try
         {
             if (ArgumentType.IsEnum)
             {
                 RevivedValue = ArgRevivers.ReviveEnum(ArgumentType, commandLineValue, IgnoreCase);
             }
             else
             {
                 RevivedValue = ArgRevivers.Revive(ArgumentType, Aliases.First(), commandLineValue);
             }
         }
         catch (ArgException)
         {
             throw;
         }
         catch (Exception ex)
         {
             if (ex.InnerException != null && ex.InnerException is ArgException)
             {
                 throw ex.InnerException;
             }
             else
             {
                 if (ArgumentType.IsEnum)
                 {
                     throw new ArgException("'" + commandLineValue + "' is not a valid value for " + Aliases.First() + ". Available values are [" + string.Join(", ", Enum.GetNames(ArgumentType)) + "]", ex);
                 }
                 else
                 {
                     throw new ArgException(ex.Message, ex);
                 }
             }
         }
     }
     else if (ArgRevivers.CanRevive(ArgumentType) && ArgumentType == typeof(SecureStringArgument))
     {
         RevivedValue = ArgRevivers.Revive(ArgumentType, Aliases.First(), commandLineValue);
     }
     else if (commandLineValue != null && ArgRevivers.CanRevive(ArgumentType))
     {
         throw new ArgException("Unexpected argument '" + Aliases.First() + "' with value '" + commandLineValue + "'");
     }
     else if (commandLineValue != null && ArgRevivers.CanRevive(ArgumentType) == false)
     {
         throw new InvalidArgDefinitionException("There is no reviver for type '" + ArgumentType.Name + '"');
     }
 }