Esempio n. 1
0
        //============================================================================*
        // CompareBasics()
        //============================================================================*

        public int CompareBasics(cBallistics Ballistics)
        {
            if (Ballistics == null)
            {
                return(1);
            }

            int rc = BallisticCoefficient.CompareTo(Ballistics.BallisticCoefficient);

            if (rc == 0)
            {
                rc = BulletDiameter.CompareTo(Ballistics.BulletDiameter);

                if (rc == 0)
                {
                    rc = BulletWeight.CompareTo(Ballistics.BulletWeight);

                    if (rc == 0)
                    {
                        rc = BulletLength.CompareTo(Ballistics.BulletLength);

                        if (rc == 0)
                        {
                            rc = MuzzleVelocity.CompareTo(Ballistics.MuzzleVelocity);
                        }
                    }
                }
            }

            return(rc);
        }
Esempio n. 2
0
        public void Serialize()
        {
            var bc1 = new BallisticCoefficient(1.2345678, DragTableId.G7);
            var s   = JsonSerializer.Serialize(bc1);
            var bc2 = JsonSerializer.Deserialize <BallisticCoefficient>(s);

            bc2.Value.Should().Be(1.2345678);
            bc2.Table.Should().Be(DragTableId.G7);
        }
Esempio n. 3
0
        public void ToStringAndParse(double value, DragTableId tableId, string format, string expected, double expectedAccuracy)
        {
            var bc1 = new BallisticCoefficient(value, tableId);

            if (format != null)
            {
                bc1.ToString(format, CultureInfo.InvariantCulture).Should().Be(expected);
            }
            else
            {
                bc1.ToString(CultureInfo.InvariantCulture).Should().Be(expected);
            }

            BallisticCoefficient.TryParse(expected, out BallisticCoefficient bc2).Should().BeTrue();
            bc2.Value.Should().BeApproximately(value, expectedAccuracy);
            bc2.Table.Should().Be(tableId);
        }
Esempio n. 4
0
        /// <summary>
        /// Reads a simple value from the attribute
        /// </summary>
        /// <param name="type">The type being read</param>
        /// <param name="element"></param>
        /// <param name="propertyName"></param>
        /// <param name="propertyType"></param>
        /// <param name="propertyAttribute"></param>
        /// <param name="attributePrefix"></param>
        /// <returns></returns>
        private object ReadAttribute(Type type, XmlElement element, string propertyName, Type propertyType, BXmlPropertyAttribute propertyAttribute, string attributePrefix)
        {
            object propertyValue = null;

            string name;

            if (string.IsNullOrEmpty(attributePrefix))
            {
                name = propertyAttribute.Name;
            }
            else
            {
                name = $"{attributePrefix}-{propertyAttribute.Name}";
            }

            string propertyText = element.Attributes[name]?.Value;

            if (propertyText != null)
            {
                var propertyType1 = Nullable.GetUnderlyingType(propertyType);
                if (propertyType1 != null && propertyType1 != propertyType)
                {
                    propertyType = propertyType1;
                }
                if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(Measurement <>))
                {
                    var ci = propertyType.GetConstructor(new Type[] { typeof(string) });
                    if (ci != null)
                    {
                        try
                        {
                            propertyValue = ci.Invoke(new object[] { propertyText });
                        }
                        catch (Exception)
                        {
                            propertyValue = null;
                        }
                    }
                }
                else if (propertyType.IsEnum)
                {
                    propertyValue = Enum.Parse(propertyType, propertyText);
                }
                else if (propertyType == typeof(double))
                {
                    if (double.TryParse(propertyText, NumberStyles.Float, CultureInfo.InvariantCulture, out double x))
                    {
                        propertyValue = x;
                    }
                }
                else if (propertyType == typeof(float))
                {
                    if (float.TryParse(propertyText, NumberStyles.Float, CultureInfo.InvariantCulture, out float x))
                    {
                        propertyValue = x;
                    }
                }
                else if (propertyType == typeof(int))
                {
                    if (int.TryParse(propertyText, NumberStyles.Any, CultureInfo.InvariantCulture, out int x))
                    {
                        propertyValue = x;
                    }
                }
                else if (propertyType == typeof(bool))
                {
                    propertyValue = propertyText == "true";
                }
                else if (propertyType == typeof(DateTime))
                {
                    if (DateTime.TryParseExact(propertyText, "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal | DateTimeStyles.AllowWhiteSpaces, out DateTime d))
                    {
                        propertyValue = d;
                    }
                    else if (DateTime.TryParseExact(propertyText, "yyyy-MM-dd HH:mm", CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal | DateTimeStyles.AllowWhiteSpaces, out d))
                    {
                        propertyValue = d;
                    }
                    else if (DateTime.TryParseExact(propertyText, "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal | DateTimeStyles.AllowWhiteSpaces, out d))
                    {
                        propertyValue = d;
                    }
                }
                else if (propertyType == typeof(TimeSpan))
                {
                    if (double.TryParse(propertyText, NumberStyles.Float, CultureInfo.InvariantCulture, out double x))
                    {
                        propertyValue = TimeSpan.FromMilliseconds(x);
                    }
                    else if (TimeSpan.TryParse(propertyText, out TimeSpan ts))
                    {
                        propertyValue = ts;
                    }
                }
                else if (propertyType == typeof(string))
                {
                    propertyValue = propertyText;
                }
                else if (propertyType == typeof(BallisticCoefficient))
                {
                    if (BallisticCoefficient.TryParse(propertyText, CultureInfo.InvariantCulture, out BallisticCoefficient ballisticCoefficient))
                    {
                        propertyValue = ballisticCoefficient;
                    }
                }
                else
                {
                    throw new InvalidOperationException($"The type {propertyType.FullName} of the property {type.FullName}.{propertyName} is not supported");
                }
            }
            return(propertyValue);
        }