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); } }
/// <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"); }
/// <summary> /// This function try to convert the Qs Parameters into the native corresponding parameters in the target method and return the converted parameters into array. /// </summary> /// <param name="method"></param> /// <param name="parameters"></param> /// <returns></returns> public static object[] QsParametersToNativeValues(MethodInfo method, params QsParameter[] parameters) { List <object> NativeParameters = new List <object>(); int iy = 0; ParameterInfo[] paramInfos = method.GetParameters(); foreach (var p in parameters) { if (p.QsNativeValue == null) { NativeParameters.Add(null); } else if (p.QsNativeValue is QsScalar) { var scalar = (QsScalar)p.QsNativeValue; //test the corresponding parameter if it is scalar or quantity or normal if (paramInfos[iy].ParameterType.IsSubclassOf(typeof(QsValue))) { // target is QsValue type so we make direct cast object nativeValue = System.Convert.ChangeType(scalar, paramInfos[iy].ParameterType, null); NativeParameters.Add(nativeValue); } else if (paramInfos[iy].ParameterType.IsGenericType) { // target is generic type which maybe AnyQuantity<double> if (paramInfos[iy].ParameterType == typeof(AnyQuantity <double>)) { // yes convert safely to the AnyQuantity<double> NativeParameters.Add(scalar.NumericalQuantity); } else { object nativeValue = System.Convert.ChangeType(scalar.NumericalQuantity, paramInfos[iy].ParameterType, null); NativeParameters.Add(nativeValue); } } else if (paramInfos[iy].ParameterType == typeof(SymbolicVariable)) { // target is symbolic variable NativeParameters.Add(scalar.SymbolicQuantity.Value); } else { object nativeValue = System.Convert.ChangeType(scalar.NumericalQuantity.Value, paramInfos[iy].ParameterType, null); NativeParameters.Add(nativeValue); } } else if (p.QsNativeValue is QsText) { NativeParameters.Add(((QsText)p.QsNativeValue).Text); } else if (p.QsNativeValue is QsVector) { // source is vector if (paramInfos[iy].ParameterType.IsArray) { // target is array QsVector vec = (QsVector)p.QsNativeValue; System.Type ArrayType = System.Type.GetType(paramInfos[iy].ParameterType.FullName.Trim('[', ']')); System.Array arr = System.Array.CreateInstance(ArrayType, vec.Count); for (int i = 0; i < vec.Count; i++) { object val = System.Convert.ChangeType(vec[i].NumericalQuantity.Value, ArrayType, null); arr.SetValue(val, i); } NativeParameters.Add(arr); } else if (paramInfos[iy].ParameterType == typeof(QsVector)) { // target is QsVector NativeParameters.Add((QsVector)p.QsNativeValue); } else { throw new QsException("The target parameter is neither QsVector nor Array"); } } else if (p.QsNativeValue is QsObject) { // source is an object // we need to test the target type if it is the same as this object type or not then make conversion or throw exception QsObject qso = (QsObject)p.QsNativeValue; if (qso.InstanceType.IsSubclassOf(paramInfos[iy].ParameterType) || qso.InstanceType.Equals(paramInfos[iy].ParameterType)) { //NativeParameters.Add(Convert.ChangeType((object)qso.ThisObject, paramInfos[iy].ParameterType)); NativeParameters.Add(qso.ThisObject); } else { throw new QsException(string.Format("Converting {0} to {1} is not supported", qso.InstanceType.Name, paramInfos[iy].Name)); } } else { throw new QsException(string.Format("Converting from {0} to {1} is not supported.", p.ParameterRawText, paramInfos[iy].GetType().Name)); } iy++; } return(NativeParameters.ToArray()); }