Пример #1
0
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            if (value != null)
            {
                switch (Type.GetTypeCode(value.GetType()))
                {
                case TypeCode.Int16:
                case TypeCode.Int32:
                case TypeCode.Int64:
                case TypeCode.Decimal:
                case TypeCode.Single:
                case TypeCode.Double:
                    return(new GradAngle((double)value));

                case TypeCode.String:
                    return(GradAngle.Parse((string)value));

                case TypeCode.Object:
                    if (value.GetType() == typeof(GradAngle))
                    {
                        return(value);
                    }
                    break;
                }
            }
            return(base.ConvertFrom(context, culture, value));
        }
Пример #2
0
        public override bool IsValid(ITypeDescriptorContext context, object value)
        {
            if (value != null)
            {
                switch (Type.GetTypeCode(value.GetType()))
                {
                case TypeCode.Int16:
                case TypeCode.Int32:
                case TypeCode.Int64:
                case TypeCode.Decimal:
                    decimal decValue = (decimal)value;
                    return(decValue > (decimal) - GradAngle.MaxTotalGrads && decValue < (decimal)GradAngle.MaxTotalGrads);

                case TypeCode.Single:
                case TypeCode.Double:
                    double dblValue = (double)value;
                    return(dblValue > (double)-GradAngle.MaxTotalGrads && dblValue < (double)GradAngle.MaxTotalGrads);

                case TypeCode.String:
                    GradAngle result;
                    return(GradAngle.TryParse((string)value, out result));

                case TypeCode.Object:
                    if (value.GetType() == typeof(GradAngle))
                    {
                        return(true);
                    }
                    break;
                }
            }
            return(false);
        }
Пример #3
0
        public static bool TryParse(string s, IFormatProvider provider, out GradAngle result)
        {
            if (provider == null)
            {
                provider = CultureInfo.CurrentCulture;
            }
            NumberFormatInfo nfi     = (NumberFormatInfo)provider.GetFormat(typeof(NumberFormatInfo));
            string           pattern = string.Format(PatternFormat, nfi.NumberDecimalSeparator);
            Match            match   = Regex.Match(s, pattern);

            if (match.Success)
            {
                int  grad      = string.IsNullOrEmpty(match.Groups["grad"].Value) ? 0 : int.Parse(match.Groups["grad"].Value);
                int  minute    = string.IsNullOrEmpty(match.Groups["minute"].Value) ? 0 : int.Parse(match.Groups["minute"].Value);
                int  second    = string.IsNullOrEmpty(match.Groups["second"].Value) ? 0 : int.Parse(match.Groups["second"].Value);
                int  hundredth = string.IsNullOrEmpty(match.Groups["hundredth"].Value) ? 0 : int.Parse(match.Groups["hundredth"].Value);
                bool negative  = match.Groups["sign"].Value == "-";
                result = new GradAngle(grad, minute, second, hundredth, negative);
                return(true);
            }
            else
            {
                pattern = string.Format(NumericPattern, nfi.NumberDecimalSeparator);
                match   = Regex.Match(s, pattern);
                if (match.Success)
                {
                    double angle;
                    if (match.Groups["degree"].Success && double.TryParse(match.Groups["angle"].Value, NumberStyles.Float, provider, out angle))
                    {
                        result = new GradAngle(angle);
                        return(true);
                    }
                    else if (match.Groups["radian"].Success && double.TryParse(match.Groups["angle"].Value, NumberStyles.Float, provider, out angle))
                    {
                        result = GradAngle.FromRadian(angle);
                        return(true);
                    }
                }
            }
            result = new GradAngle();
            return(false);
        }
Пример #4
0
 public static bool TryParse(string s, out GradAngle result)
 {
     return(TryParse(s, null, out result));
 }
Пример #5
0
 public GradAngle Subtract(GradAngle op)
 {
     return(new GradAngle((int)(((long)this._units - (long)op._units) % (long)MaxTotalUnits)));
 }
Пример #6
0
 public GradAngle Add(GradAngle op)
 {
     return(new GradAngle((int)(((long)this._units + (long)op._units) % (long)MaxTotalUnits)));
 }