Exemplo n.º 1
0
        private static void LoadDefaultRevivers(Dictionary <Type, Func <string, string, object> > revivers)
        {
            revivers.Add(typeof(bool), (prop, val) =>
            {
                return(val != null && val.ToLower().ToString() != "false" && val != "0"); // null means the switch value was not specified.  If it was specified then it's automatically true
            });

            revivers.Add(typeof(Guid), (prop, val) =>
            {
                Guid ret;
                if (Guid.TryParse(val, out ret) == false)
                {
                    throw new FormatException("value must be a Guid: " + val);
                }
                return(ret);
            });

            revivers.Add(typeof(byte), (prop, val) =>
            {
                byte ret;
                if (byte.TryParse(val, out ret) == false)
                {
                    throw new FormatException("value must be a byte: " + val);
                }
                return(ret);
            });

            revivers.Add(typeof(int), (prop, val) =>
            {
                int ret;
                if (int.TryParse(val, out ret) == false && TryParseConstant(val, out ret) == false)
                {
                    throw new FormatException("value must be an integer: " + val);
                }
                return(ret);
            });

            revivers.Add(typeof(long), (prop, val) =>
            {
                long ret;
                if (long.TryParse(val, out ret) == false && TryParseConstant(val, out ret) == false)
                {
                    throw new FormatException("value must be an integer: " + val);
                }
                return(ret);
            });

            revivers.Add(typeof(float), (prop, val) =>
            {
                float ret;
                if (float.TryParse(val, out ret) == false && TryParseConstant(val, out ret) == false)
                {
                    throw new FormatException("value must be a number: " + val);
                }
                return(ret);
            });

            revivers.Add(typeof(double), (prop, val) =>
            {
                double ret;
                if (double.TryParse(val, out ret) == false && TryParseConstant(val, out ret) == false)
                {
                    throw new FormatException("value must be a number: " + val);
                }
                return(ret);
            });

            revivers.Add(typeof(string), (prop, val) =>
            {
                return(val);
            });

            revivers.Add(typeof(DateTime), (prop, val) =>
            {
                DateTime ret;
                if (DateTime.TryParse(val, out ret) == false)
                {
                    throw new FormatException("value must be a valid date time: " + val);
                }
                return(ret);
            });

            revivers.Add(typeof(SecureStringArgument), (prop, val) =>
            {
                if (val != null)
                {
                    throw new ArgException("The value for " + prop + " cannot be specified on the command line");
                }
                return(new SecureStringArgument(prop));
            });

            revivers.Add(typeof(Uri), (prop, val) =>
            {
                try
                {
                    return(new Uri(val));
                }
                catch (UriFormatException)
                {
                    throw new UriFormatException("value must be a valid URI: " + val);
                }
            });

            revivers.Add(typeof(IPAddress), (prop, val) =>
            {
                System.Net.IPAddress ret;
                if (System.Net.IPAddress.TryParse(val, out ret) == false)
                {
                    throw new FormatException("value must be a valid IP address: " + val);
                }
                return(ret);
            });

            revivers.Add(typeof(ConsoleString), (prop, val) =>
            {
                return(ConsoleString.Parse(val));
            });
        }