public static int ToInt32(this decimal value, IntPart part = IntPart.First)
        {
            var number = value.ToDecimalString();

            return(part switch
            {
                IntPart.First => int.Parse(number.Split('.')[0]),
                IntPart.Second => int.Parse(number.Split('.')[1]),
                _ => throw new ArgumentOutOfRangeException(nameof(part), part, null),
            });
예제 #2
0
            public override int GetHashCode()
            {
                var hashCode = 893539880;

                hashCode = hashCode * -1521134295 + Numerator.GetHashCode();
                hashCode = hashCode * -1521134295 + Denominator.GetHashCode();
                hashCode = hashCode * -1521134295 + IntPart.GetHashCode();
                hashCode = hashCode * -1521134295 + EqualityComparer <Rational> .Default.GetHashCode(FracPart);

                return(hashCode);
            }
예제 #3
0
        public static int ToInt32(this decimal value, IntPart part = IntPart.First)
        {
            var number = value.ToString("0.000");

            switch (part)
            {
            case IntPart.First:
                return(int.Parse(number.Split('.')[0]));

            case IntPart.Second:
                return(int.Parse(number.Split('.')[1]));

            default:
                throw new ArgumentOutOfRangeException(nameof(part), part, null);
            }
        }
예제 #4
0
        public AbstractRoute(string pattern, MethodInfo methodInfo, IWaConfig config)
        {
            this.config = config;
            method      = methodInfo;

            foreach (var part in pattern.Split(new[] { '/' }))
            {
                if (part.IndexOf('{') == 0)
                {
                    if (part[part.Length - 1] != '}')
                    {
                        throw new Exception("part must end with }");
                    }
                    var  pair = part.Substring(1, part.Length - 2).Split(new[] { ':' });
                    Part newPart;

                    /*if (pair.Length == 2)
                     * {
                     *  switch (pair[1])
                     *  {
                     *      case "int":
                     *          newPart = new IntPart();
                     *          break;
                     *      case "long":
                     *          newPart = new LongPart();
                     *          break;
                     *      default:
                     *          throw new Exception("unsupported type");
                     *  }
                     * }
                     * else
                     * {
                     *  newPart = new StringPart();
                     * }
                     * newPart.pattern = pair[0];
                     * if (newPart.pattern[newPart.pattern.Length -1 ] == '?')
                     * {
                     *  newPart.pattern = newPart.pattern.Substring(0, newPart.pattern.Length - 1);
                     *  newPart.optional = true;
                     * }*/
                    string partName = pair[0];
                    bool   optional = false;
                    if (partName[partName.Length - 1] == '?')
                    {
                        partName = partName.Substring(0, partName.Length - 1);
                        optional = true;
                    }

                    var parameter = methodInfo.GetParameters().SingleOrDefault(p => p.Name == partName);
                    if (parameter == null)
                    {
                        throw new Exception(string.Format("method {0} parameter {1} cannot be found (from pattern {2})", methodInfo.Name, partName, pattern));
                    }
                    if (parameter.ParameterType.IsEnum)
                    {
                        newPart = new EnumPart(parameter.ParameterType);
                    }
                    else if (parameter.ParameterType == typeof(string))
                    {
                        newPart = new StringPart();
                    }
                    else if (parameter.ParameterType == typeof(long))
                    {
                        newPart = new LongPart();
                    }
                    else if (parameter.ParameterType == typeof(int))
                    {
                        newPart = new IntPart();
                    }
                    else
                    {
                        throw new Exception("Unsupported uri part parameter type " + parameter.ParameterType.Name);
                    }
                    newPart.pattern  = partName;
                    newPart.optional = optional;
                    newPart.order    = parameter.Position;
                    parts.Add(newPart);
                }
                else if (part.Length > 0)
                {
                    parts.Add(new Part {
                        pattern = part
                    });
                }
            }

            body = method.GetParameters().Where(p => p.GetCustomAttribute <FromBodyAttribute>() != null).SingleOrDefault();

            foreach (var parameter in method.GetParameters().Where(p => p.GetCustomAttribute <FromUriAttribute>() != null))
            {
                if (query == null)
                {
                    query = new Dictionary <string, Part>();
                }
                if (parameter.ParameterType == typeof(int))
                {
                    query[parameter.Name] = new IntPart {
                        order = parameter.Position
                    }
                }
                ;
                else if (parameter.ParameterType == typeof(long))
                {
                    query[parameter.Name] = new LongPart {
                        order = parameter.Position
                    }
                }
                ;
                else if (parameter.ParameterType == typeof(string))
                {
                    query[parameter.Name] = new StringPart {
                        order = parameter.Position
                    }
                }
                ;
                else if (parameter.ParameterType == typeof(bool))
                {
                    query[parameter.Name] = new BoolPart {
                        order = parameter.Position
                    }
                }
                ;
                else if (parameter.ParameterType == typeof(double))
                {
                    query[parameter.Name] = new DoublePart {
                        order = parameter.Position
                    }
                }
                ;
                else if (parameter.ParameterType == typeof(DateTime))
                {
                    query[parameter.Name] = new DatePart {
                        order = parameter.Position
                    }
                }
                ;
                else if (parameter.ParameterType.IsEnum)
                {
                    query[parameter.Name] = new EnumPart(parameter.ParameterType)
                    {
                        order = parameter.Position
                    }
                }
                ;
                else
                {
                    throw new Exception("Parameter type " + parameter.ParameterType + " unsupported in " + method.DeclaringType.FullName + "." + method.Name);
                }

                if (parameter.HasDefaultValue)
                {
                    query[parameter.Name].optional     = true;
                    query[parameter.Name].defaultValue = parameter.DefaultValue;
                }
            }

            var unmappedParameter = method.GetParameters().FirstOrDefault(p => p.GetCustomAttribute <FromBodyAttribute>() == null && p.GetCustomAttribute <FromUriAttribute>() == null &&
                                                                          !parts.Any(x => x.order == p.Position));

            if (unmappedParameter != null)
            {
                throw new Exception("Parameter " + unmappedParameter.Name + " has not been mapped in " + method.Name + " from " + method.DeclaringType.Name);
            }

            numberOfMandatoryParts = parts.Count(p => !p.optional);
        }