public object GetValue(string variable)
        {
            if (variable.StartsWith("_"))
            {
                if (values.ContainsKey(variable))
                {
                    return(values[variable]);
                }
                else
                {
                    var r = QsObject.CreateNativeObject(new Circle(variable));
                    values.Add(variable, r);

                    return(values[variable]);
                }
            }

            return(null);
        }
        public bool TryGetValue(string variable, out object q)
        {
            if (variable.StartsWith("_"))
            {
                if (values.ContainsKey(variable))
                {
                    q = values[variable];
                }
                else
                {
                    var r = QsObject.CreateNativeObject(new Circle(variable));
                    values.Add(variable, r);

                    q = r;
                }
                return(true);
            }
            else
            {
                q = null;
                return(false);
            }
        }
示例#3
0
        /// <summary>
        /// Created to be used for the retuen values of the function
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static QsValue NativeToQsConvert(object value)
        {
            if (value == null)
            {
                return(null);
            }


            if (value is Type)
            {
                var EnumType = value as Type;
                if (EnumType != null)
                {
                    List <QsTupleValue> tupleValues = new List <QsTupleValue>();

                    foreach (var eval in Enum.GetValues(EnumType))
                    {
                        int ival = (int)eval;

                        QsTupleValue tpv = new QsTupleValue(
                            ival, Enum.GetName(EnumType, eval), ival.ToScalarValue());

                        tupleValues.Add(tpv);
                    }
                    return(new QsFlowingTuple(tupleValues.ToArray()));
                }
            }

            if (value.GetType().BaseType == typeof(Enum))
            {
                // return the result as a flowing tuple instance
                var ff = QsFlowingTuple.FromCSharpEnum(value.GetType());
                return(ff[Enum.GetName(value.GetType(), value)]);
            }

            Type vType = value.GetType();

            if (vType == typeof(int) || vType == typeof(double) || vType == typeof(Single) || vType == typeof(float) || vType == typeof(Int64) || vType == typeof(uint) || vType == typeof(UInt16) || vType == typeof(UInt64))
            {
                double vv = (double)System.Convert.ChangeType(value, typeof(double), null);
                return(new QsScalar
                {
                    NumericalQuantity = vv.ToQuantity()
                });
            }

            if (vType == typeof(String))
            {
                return(new QsText((string)value));
            }

            if (vType.BaseType == typeof(QsValue))
            {
                return((QsValue)value);
            }

            if (vType.BaseType == typeof(AnyQuantity <double>))
            {
                return new QsScalar {
                           NumericalQuantity = (AnyQuantity <double>)value
                }
            }
            ;

            if (vType == typeof(int[]) || vType == typeof(double[]) || vType == typeof(Single[]) || vType == typeof(float[]) || vType == typeof(Int64[]) || vType == typeof(uint[]) || vType == typeof(UInt16[]) || vType == typeof(UInt64[]))
            {
                Array rr = (Array)value;

                QsVector v = new QsVector(rr.Length);
                foreach (var m in rr)
                {
                    var r = (double)System.Convert.ChangeType(m, typeof(double), null);
                    v.AddComponent(new QsScalar {
                        NumericalQuantity = r.ToQuantity()
                    });
                }
                return(v);
            }

            if (vType.IsArray)
            {
                // this is array of non numerical type and it should be returned as tuple
                Array          rr = (Array)value;
                QsFlowingTuple v  = new QsFlowingTuple();
                foreach (var m in rr)
                {
                    v.AddTupleValue(QsObject.CreateNativeObject(m));
                }
                return(v);
            }

            if (vType == typeof(SymbolicVariable))
            {
                return new QsScalar(ScalarTypes.SymbolicQuantity)
                       {
                           SymbolicQuantity = ((SymbolicVariable)value).ToQuantity()
                       }
            }
            ;

            // the last thing is to return object from this type
            if (!vType.IsValueType)
            {
                return(QsObject.CreateNativeObject(value));
            }

            if (vType == typeof(bool))
            {
                bool v = (bool)value;
                if (v)
                {
                    return(QsBoolean.True);
                }
                else
                {
                    return(QsBoolean.False);
                }
            }


            if (vType.IsValueType)
            {
                return(QsFlowingTuple.FromStruct((ValueType)value));
            }

            throw new QsException(vType + " doesn't have corresponding type in Quantity System");
        }