コード例 #1
0
        public static IList TryConversion(this IList self, Type[] possibleTypes = null, bool raiseError = false)
        {
            possibleTypes = possibleTypes ?? DataMap.ConversionTypes;

            var type = GetDataType(self);

            ArgumentException cause = null;

            foreach (var t in possibleTypes)
            {
                if (t == type)
                {
                    return(self);
                }

                try
                {
                    return(SmartConverter.ConvertTo(t, self));
                }
                catch (ArgumentException ex)
                {
                    cause = ex;
                }
            }

            // If any possible types are not adequate, try to cast down the type of the first non-null element.

            return(CastDown(self));
        }
コード例 #2
0
ファイル: Helpers.cs プロジェクト: horker/oxyplotcli2
        public static T ConvertObjectType <T>(object value)
        {
            if (typeof(T) == typeof(bool))
            {
                return((T)(object)Bool.ConvertFrom(value));
            }

            if (typeof(T) == typeof(Category))
            {
                return((T)(object)new Category(value.ToString()));
            }

            if (typeof(T) == typeof(double))
            {
                return((T)(object)Double.ConvertFrom(value));
            }

            if (typeof(T) == typeof(int))
            {
                return((T)(object)SmartConverter.ToInt(value));
            }

            if (typeof(T) == typeof(string))
            {
                return((T)(object)value.ToString());
            }

            if (typeof(T) == typeof(OxyColor))
            {
                return((T)(object)OxyColor.ConvertFrom(value));
            }

            return((T)value);
        }
コード例 #3
0
ファイル: DoubleList.cs プロジェクト: horker/oxyplotcli2
        public DoubleList(object values)
            : this()
        {
            object v = values;

            if (values is PSObject psobj)
            {
                v = psobj.BaseObject;
            }

            if (v is ICollection <double> dc)
            {
                foreach (var e in dc)
                {
                    Values.Add(e);
                }
            }
            else if (v is ICollection <object> oc)
            {
                foreach (var e in oc)
                {
                    Values.Add(SmartConverter.ToDouble(e));
                }
            }
            else
            {
                throw new ArgumentException("Can't convert into a sequence of type double");
            }
        }
コード例 #4
0
        public static double ConvertFrom(string value)
        {
            try
            {
                return(SmartConverter.ToDouble(value));
            }
            catch (Exception)
            {
                try
                {
                    return(OxyPlot.Axes.DateTimeAxis.ToDouble(SmartConverter.ToDateTime(value)));
                }
                catch (Exception)
                {
                    try
                    {
                        return(OxyPlot.Axes.TimeSpanAxis.ToDouble(TimeSpan.Parse(value)));
                    }
                    catch (Exception)
                    {
                        try
                        {
                            var m = UnitPattern.Match(value);
                            if (m.Success)
                            {
                                value = m.Groups[1].Value;
                                var unit         = m.Groups[2].Value.ToLower();
                                var unitMultiply = 1.0;
                                if (unit == "px")
                                {
                                    unitMultiply = 1.0;
                                }
                                else if (unit == "in")
                                {
                                    unitMultiply = DpiX;
                                }
                                else if (unit == "cm")
                                {
                                    unitMultiply = DpiX / 2.54;
                                }
                                else if (unit == "pt")
                                {
                                    unitMultiply = DpiX / 72.0;
                                }

                                return(SmartConverter.ToDouble(value) * unitMultiply);
                            }
                            throw new ArgumentException($"Can't convert to double, DateTime nor TimeSpan: {value}");
                        }
                        catch (Exception)
                        {
                            throw new ArgumentException($"Can't convert to double, DateTime nor TimeSpan: {value}");
                        }
                    }
                }
            }
        }
コード例 #5
0
 public void OnGet(string q)
 {
     if (!string.IsNullOrEmpty(q))
     {
         var converter = new SmartConverter();
         Query  = q;
         Result = converter.Convert(q);
     }
 }
コード例 #6
0
ファイル: ArrayMethods.cs プロジェクト: horker/Horker.MXNet
        public static PSObject Mode(PSObject self, bool skipNaN = true)
        {
            object array = self.BaseObject;

            // Convert values to double if they are not numeric
            var type = array.GetType().GetElementType();

            if (!Utils.IsNumeric(type))
            {
                array = SmartConverter.ConvertTo <double>((dynamic)array);
            }

            return(GenericIListExtensions.Mode((dynamic)array, skipNaN));
        }
コード例 #7
0
        public static List <T> ToList <T>(this IList value)
        {
            if (value is IList <T> l)
            {
                return(new List <T>(l));
            }

            var result = new List <T>(value.Count);

            for (var i = 0; i < value.Count; ++i)
            {
                result.Add(SmartConverter.ConvertTo <T>(value[i]));
            }

            return(result);
        }
コード例 #8
0
        public static T[] ToArray <T>(this IList value)
        {
            if (GetDataType(value) == typeof(T))
            {
                ((IList <T>)value).ToArray();
            }

            var result = new T[value.Count];

            for (var i = 0; i < result.Length; ++i)
            {
                result[i] = SmartConverter.ConvertTo <T>(value[i]);
            }

            return(result);
        }
コード例 #9
0
        public Type InferValueType()
        {
            var t = _value.GetType();

            if (t == typeof(double) || t == typeof(float) || t == typeof(long) || t == typeof(int) ||
                t == typeof(short) || t == typeof(byte) || t == typeof(sbyte))
            {
                return(typeof(double));
            }

            if (t == typeof(DateTime) || t == typeof(DateTimeOffset))
            {
                return(typeof(DateTime));
            }

            if (t == typeof(TimeSpan))
            {
                return(typeof(TimeSpan));
            }

            try
            {
                var dummy = SmartConverter.ToDouble(_value);
                return(typeof(double));
            }
            catch (Exception)
            {
                try
                {
                    var dummy = DateTime.Parse(_value.ToString());
                    return(typeof(DateTime));
                }
                catch (Exception)
                {
                    try
                    {
                        var dummy = TimeSpan.Parse(_value.ToString());
                        return(typeof(TimeSpan));
                    }
                    catch (Exception)
                    {
                        return(_value.GetType());
                    }
                }
            }
        }
コード例 #10
0
        public override void Fit(SeriesBase data)
        {
            _encoding   = new Dictionary <object, T>();
            _categories = new List <object>();

            var count = 0;

            foreach (var value in data.UnderlyingList)
            {
                if (!_encoding.ContainsKey(value))
                {
                    _encoding.Add(value, SmartConverter.ConvertTo <T>(count));
                    _categories.Add(value);
                    ++count;
                }
            }
        }
コード例 #11
0
        public static OxyPlot.PlotLength ConvertFrom(string value)
        {
            var unitMultiply   = 1.0;
            var plotLengthUnit = OxyPlot.PlotLengthUnit.Data;

            var m = UnitPattern.Match(value);

            if (m.Success)
            {
                value = m.Groups[1].Value;
                var unit = m.Groups[2].Value.ToLower();

                switch (unit)
                {
                case "px":
                    unitMultiply = 1.0;
                    break;

                case "in":
                    unitMultiply = DpiX;
                    break;

                case "cm":
                    unitMultiply = DpiX / 2.54;
                    break;

                case "pt":
                    unitMultiply = DpiX / 72.0;
                    break;

                case "viewport":
                    plotLengthUnit = OxyPlot.PlotLengthUnit.RelativeToViewport;
                    break;

                case "plotarea":
                    plotLengthUnit = OxyPlot.PlotLengthUnit.RelativeToPlotArea;
                    break;

                default:
                    throw new ArgumentException($"Unknown unit: {m.Groups[2].Value}");
                }
            }

            return(new OxyPlot.PlotLength(SmartConverter.ToDouble(value) * unitMultiply, plotLengthUnit));
        }
コード例 #12
0
        public static double[,] ConvertFrom(object[] values)
        {
            var first = values[0];

            if (first is PSObject pso)
            {
                first = pso.BaseObject;
            }

            if (!(first is Array firstRow))
            {
                throw new ArgumentException("Failed to convert to a two-dimensional array");
            }

            var columnLength = firstRow.Length;
            var result       = new double[columnLength, values.Length];

            for (var i = 0; i < values.Length; ++i)
            {
                var v = values[i];
                if (v is PSObject p)
                {
                    v = p.BaseObject;
                }

                if (!(v is Array row))
                {
                    throw new ArgumentException("Failed to convert to a two-dimensional array");
                }

                var j = 0;
                foreach (var e in row)
                {
                    result[j, i] = SmartConverter.ToDouble(e);
                    ++j;
                }
            }

            return(result);
        }
コード例 #13
0
        public OxyPalette(OxyPaletteItem[] values)
        {
            if (values.Length == 0)
            {
                Value = new OxyPlot.OxyPalette();
                return;
            }

            if (values.Length == 1)
            {
                var v = values[0].Value;
                if (v is PSObject pso)
                {
                    v = pso.BaseObject;
                }
                if (v is OxyPlot.OxyPalette p)
                {
                    Value = p;
                    return;
                }
            }

            if (values.Length > 2)
            {
                throw new ArgumentException("Specify a palette name and an optional palette size to create an OxyPalette; A palette name is one of BlueWhiteRed31, Hot64, Hue64, BlackWhiteRed, BlueWhiteRed, Cool, Gray, Hot, Hue, HueDistinct, Jet or Rainbow");
            }

            string name = values[0].Value.ToString();

            var paletteSize = 100;

            if (values.Length == 2)
            {
                paletteSize = SmartConverter.ToInt32(values[1].Value);
            }

            Value = ConvertFrom(name, paletteSize);
        }
コード例 #14
0
ファイル: Helpers.cs プロジェクト: horker/oxyplotcli2
        public static object ConvertObjectType(Type type, object value)
        {
            if (type == typeof(bool))
            {
                return(Bool.ConvertFrom(value));
            }

            if (type == typeof(Category))
            {
                return(Category.Create(value));
            }

            if (type == typeof(double))
            {
                return(Double.ConvertFrom(value));
            }

            if (type == typeof(OxyPlot.DataPoint))
            {
                return(DataPoint.ConvertFrom(value));
            }

            if (type == typeof(OxyPlot.IInterpolationAlgorithm))
            {
                return(InterpolationAlgorithm.ConvertFrom(value));
            }

            if (type == typeof(OxyPlot.OxyColor))
            {
                return(OxyColor.ConvertFrom(value));
            }

            if (type == typeof(OxyPlot.OxyImage))
            {
                return(OxyImage.ConvertFrom(value));
            }

            if (type == typeof(OxyPlot.OxyPalette))
            {
                return(OxyPalette.ConvertFrom(value));
            }

            if (type == typeof(OxyPlot.OxyThickness))
            {
                return(OxyThickness.ConvertFrom(value));
            }

            if (type == typeof(OxyPlot.PlotLength))
            {
                return(PlotLength.ConvertFrom(value));
            }

            if (type == typeof(OxyPlot.ScreenPoint))
            {
                return(ScreenPoint.ConvertFrom(value));
            }

            if (type == typeof(OxyPlot.ScreenVector))
            {
                return(ScreenVector.ConvertFrom(value));
            }

            if (type == typeof(int))
            {
                return(SmartConverter.ToInt(value));
            }

            if (type == typeof(string))
            {
                return((string)value);
            }

            if (type.IsEnum)
            {
                var name       = ((string)value).ToLower();
                var enumNames  = type.GetEnumNames();
                var enumValues = type.GetEnumValues();
                var i          = 0;
                foreach (var v in enumValues)
                {
                    if (enumNames[i++].ToLower() == name)
                    {
                        return(v);
                    }
                }

                throw new ArgumentException($"Unknown enum value '{value}' for {type.FullName}");
            }

            return(value);
        }
コード例 #15
0
ファイル: Bool.cs プロジェクト: horker/oxyplotcli2
 public Bool(object value)
 {
     Value = SmartConverter.ToBool(value);
 }
コード例 #16
0
ファイル: SeriesBuilder.cs プロジェクト: horker/oxyplotcli2
        private Type InferAxisTypeBasedOnValueType(int axisIndex, object value)
        {
            // Use a default type except a linear axis.
            if (DefaultAxisTypes[axisIndex] != typeof(LinearAxis))
            {
                return(DefaultAxisTypes[axisIndex]);
            }

            if (value == null)
            {
                return(null);
            }

            Type t;

            if (value is TypeAdaptors.Double d)
            {
                t = d.InferValueType();
            }
            else
            {
                t = value.GetType();
            }

            if (t == typeof(double) || t == typeof(float) || t == typeof(long) || t == typeof(int) ||
                t == typeof(short) || t == typeof(byte) || t == typeof(sbyte))
            {
                return(typeof(LinearAxis));
            }

            if (t == typeof(DateTime) || t == typeof(DateTimeOffset))
            {
                return(typeof(DateTimeAxis));
            }

            if (t == typeof(TimeSpan))
            {
                return(typeof(TimeSpanAxis));
            }

            try
            {
                var dummy = SmartConverter.ToDouble(value);
                return(typeof(LinearAxis));
            }
            catch (Exception)
            {
                try
                {
                    var dummy = DateTime.Parse(value.ToString());
                    return(typeof(DateTimeAxis));
                }
                catch (Exception)
                {
                    try
                    {
                        var dummy = TimeSpan.Parse(value.ToString());
                        return(typeof(TimeSpanAxis));
                    }
                    catch (Exception)
                    {
                        return(typeof(LinearAxis));
                    }
                }
            }
        }