public override string ToString() { Type t = this.DataType.Type; object v = VPLUtil.GetDefaultValue(t); return(v.ToString()); }
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e) { if (e.Node != null) { mi = e.Node.Tag as MethodInfo; if (mi != null) { this.Text = "Method:" + e.Node.Text; ParameterInfo[] pis = mi.GetParameters(); parameters = new PropertyTable(); for (int i = 0; i < pis.Length; i++) { try { object v = VPLUtil.GetDefaultValue(pis[i].ParameterType); Type t = pis[i].ParameterType; if (t.Equals(typeof(object))) { t = typeof(string); } parameters.Properties.Add(new PropertySpec(pis[i].Name, t, "", "", v)); parameters[pis[i].Name] = v; } catch (Exception err) { MathNode.Log(this, err); } } propertyGrid1.SelectedObject = parameters; } } }
public bool CreateTypedNamedValue(string name, Type type) { if (_properties == null) { _properties = new SortedList <string, TypedNamedValue>(); } if (_properties.ContainsKey(name)) { return(false); } _properties.Add(name, new TypedNamedValue(name, new TypedValue(type, VPLUtil.GetDefaultValue(type)))); return(true); }
public override bool OnExportJavaScriptCode(ActionBranch previousAction, ActionBranch nextAction, StringCollection jsCode, StringCollection methodCode, JsMethodCompiler data) { string indexName = RepeatIndex.CodeName; methodCode.Add(Indentation.GetIndent()); methodCode.Add("for("); methodCode.Add(indexName); methodCode.Add("=0;"); methodCode.Add(indexName); methodCode.Add("<"); methodCode.Add(RepeatCount.CreateJavaScript(methodCode)); methodCode.Add(";"); methodCode.Add(indexName); methodCode.Add("++) {\r\n"); Indentation.IndentIncrease(); string indents = Indentation.GetIndent(); StringCollection sc = new StringCollection(); if (_iconList != null) { foreach (ComponentIcon ci in _iconList) { ComponentIconLocal cil = ci as ComponentIconLocal; if (cil != null && cil.ScopeGroupId == this.BranchId) { sc.Add(indents); sc.Add("var "); sc.Add(cil.LocalPointer.CodeName); sc.Add("="); sc.Add(ObjectCreationCodeGen.ObjectCreateJavaScriptCode(VPLUtil.GetDefaultValue(cil.LocalPointer.BaseClassType))); sc.Add(";\r\n"); } } } SetWithinLoop(); Method.SubMethod.Push(this); data.AddSubMethod(this); bool bRet = base.OnExportJavaScriptCode(previousAction, nextAction, jsCode, sc, data); Method.SubMethod.Pop(); // for (int i = 0; i < sc.Count; i++) { methodCode.Add(sc[i]); } Indentation.IndentDecrease(); methodCode.Add(Indentation.GetIndent()); methodCode.Add("}\r\n"); return(bRet); }
public void AddValue(string name, Type type) { if (_properties == null) { _properties = new List <TypedNamedValue>(); } for (int i = 0; i < _properties.Count; i++) { if (string.CompareOrdinal(name, _properties[i].Name) == 0) { return; } } _properties.Add(new TypedNamedValue(name, new TypedValue(type, VPLUtil.GetDefaultValue(type)))); }
/// <summary> /// for a non-static abstract base type, use a dictionary to hold the value /// </summary> /// <param name="component"></param> /// <returns></returns> public override object GetValue(object component) { IXType xt = component as IXType; if (xt != null) { if (xt.IsPropertyValueSet(Name)) { return(xt.GetPropertyValue(Name)); } } if (_pif == null) { return(_defVal); } if (IsStatic) { //use the type to get the static value //indexers are treated as methods return(_pif.GetValue(typeof(T), null)); } else { if (typeof(T).IsAbstract) { if (_propertyValues != null) { object v; if (_propertyValues.TryGetValue(_pif.Name, out v)) { return(v); } } return(VPLUtil.GetDefaultValue(typeof(T))); } else { if (_owner != null) { return(_pif.GetValue(_owner, null)); } return(_pif.GetValue(component, null)); } } }
public TreeNodeValue CreateValue(Type t) { this.Expand(); string name = CreateNewValueName(); if (_data == null) { _data = new Dictionary <string, TypedNamedValue>(); } TypedValue v = new TypedValue(t, VPLUtil.GetDefaultValue(t)); _data.Add(name, new TypedNamedValue(name, v)); TreeNodeValue tnv = new TreeNodeValue(name, v); Nodes.Add(tnv); return(tnv); }
public bool RenameTypedNamedValue(string oldName, string name, Type type) { if (_properties != null) { if (_properties.ContainsKey(oldName)) { if (type == null) { type = _properties[oldName].Value.ValueType; } object v = _properties[oldName].Value.Value; if (!type.IsAssignableFrom(_properties[oldName].Value.ValueType)) { v = VPLUtil.GetDefaultValue(type); } _properties.Remove(oldName); _properties.Add(name, new TypedNamedValue(name, new TypedValue(type, v))); return(true); } } return(false); }
public override void ResetValue(object component) { _param.Field.Value = VPLUtil.GetDefaultValue(EPField.ToSystemType(_param.Type)); }
public override void ResetValue(object component) { _data.ObjectInstance = VPLUtil.GetDefaultValue(_data.ObjectType); }
public bool ExportToCsvFile(string filename, DataTable tblSrc, bool append, EnumCharEncode encode) { _error = null; System.IO.StreamWriter sw = null; if (tblSrc == null) { _error = "Source data is null"; } else if (tblSrc.Columns.Count == 0) { _error = "Source data is empty"; } else { try { bool bWriteHeader = HasHeader; if (bWriteHeader && append) { if (System.IO.File.Exists(filename)) { System.IO.FileInfo fi = new System.IO.FileInfo(filename); if (fi.Length > 0) { bWriteHeader = false; } } } System.Text.StringBuilder sb; // sw = new System.IO.StreamWriter(filename, append, EncodeUtility.GetEncoding(encode), 2048); if (bWriteHeader && !append) { sb = new System.Text.StringBuilder(); sb.Append(DataTransferConvert.Encode(_delimiter, tblSrc.Columns[0].ColumnName)); for (int i = 1; i < tblSrc.Columns.Count; i++) { sb.Append(DataTransferConvert.Delimiter(_delimiter)); sb.Append(DataTransferConvert.Encode(_delimiter, tblSrc.Columns[i].ColumnName)); } sw.WriteLine(sb.ToString()); } for (int r = 0; r < tblSrc.Rows.Count; r++) { sb = new System.Text.StringBuilder(); object v = tblSrc.Rows[r][0]; if (v == DBNull.Value || v == null) { v = VPLUtil.GetDefaultValue(tblSrc.Columns[0].DataType); } sb.Append(DataTransferConvert.Encode(_delimiter, StringUtility.ToString(v))); for (int i = 1; i < tblSrc.Columns.Count; i++) { sb.Append(DataTransferConvert.Delimiter(_delimiter)); v = tblSrc.Rows[r][i]; if (v == DBNull.Value || v == null) { v = VPLUtil.GetDefaultValue(tblSrc.Columns[i].DataType); } sb.Append(DataTransferConvert.Encode(_delimiter, StringUtility.ToString(v))); } sw.WriteLine(sb.ToString()); } } catch (Exception er) { _error = ExceptionLimnorDatabase.FormExceptionText(er, "Error saving data to file {0}", filename); } finally { if (sw != null) { sw.Close(); } } } return(string.IsNullOrEmpty(_error)); }
public override bool OnExportJavaScriptCode(ActionBranch previousAction, ActionBranch nextAction, StringCollection jsCode, StringCollection methodCode, JsMethodCompiler data) { string c; if (_logicExpression == null) { c = "true"; } else { _logicExpression.PrepareForCompile(this.Method); c = _logicExpression.CreateJavaScript(methodCode); } methodCode.Add(Indentation.GetIndent()); string initCodeStr = null; string increCodeStr = null; if (_initAction != null) { if (_initAction.Action == null) { _initAction.OnPostRootDeserialize(); } if (_initAction.Action != null) { StringCollection initCode = new StringCollection(); _initAction.Action.ExportJavaScriptCode(null, null, methodCode, initCode, data); StringBuilder sb = new StringBuilder(); for (int i = 0; i < initCode.Count; i++) { sb.Append(initCode[i]); } initCodeStr = sb.ToString().Replace("\r\n", ""); } } if (_increAction != null) { if (_increAction.Action == null) { _increAction.OnPostRootDeserialize(); } if (_increAction.Action != null) { StringCollection increCode = new StringCollection(); _increAction.Action.ExportJavaScriptCode(null, null, methodCode, increCode, data); StringBuilder sb = new StringBuilder(); for (int i = 0; i < increCode.Count; i++) { sb.Append(increCode[i]); } increCodeStr = sb.ToString().Replace("\r\n", ""); } } if (string.IsNullOrEmpty(initCodeStr)) { initCodeStr = ";"; } if (!string.IsNullOrEmpty(increCodeStr)) { increCodeStr = increCodeStr.Trim(); while (increCodeStr.EndsWith(";", StringComparison.Ordinal)) { increCodeStr = increCodeStr.Substring(0, increCodeStr.Length - 1); increCodeStr = increCodeStr.Trim(); } } methodCode.Add("for("); methodCode.Add(initCodeStr); methodCode.Add(c); methodCode.Add(";"); methodCode.Add(increCodeStr); methodCode.Add(") {\r\n"); Indentation.IndentIncrease(); string indents = Indentation.GetIndent(); StringCollection sc = new StringCollection(); if (_iconList != null) { foreach (ComponentIcon ci in _iconList) { ComponentIconLocal cil = ci as ComponentIconLocal; if (cil != null && cil.ScopeGroupId == this.BranchId) { sc.Add(indents); sc.Add("var "); sc.Add(cil.LocalPointer.CodeName); sc.Add("="); sc.Add(ObjectCreationCodeGen.ObjectCreateJavaScriptCode(VPLUtil.GetDefaultValue(cil.LocalPointer.BaseClassType))); sc.Add(";\r\n"); } } } Method.SubMethod.Push(this); data.AddSubMethod(this); bool bRet = base.OnExportJavaScriptCode(previousAction, nextAction, jsCode, sc, data); Method.SubMethod.Pop(); // for (int i = 0; i < sc.Count; i++) { methodCode.Add(sc[i]); } Indentation.IndentDecrease(); methodCode.Add(Indentation.GetIndent()); methodCode.Add("}\r\n"); return(bRet); }
public override void ResetValue(object component) { _field.SetValue(VPLUtil.GetDefaultValue(PropertyType)); }
public override void ResetValue(object component) { _value.Value.Value = VPLUtil.GetDefaultValue(_value.Value.ValueType); }
public void ResetValue() { SetSetting(VPLUtil.GetDefaultValue(_type)); }
public override void ResetValue(object component) { _owner.SetValue(Name, VPLUtil.GetDefaultValue(_valueType)); }
public static CodeExpression GetDefaultCodeByType(Type t) { if (t.Equals(typeof(void))) { return(new CodePrimitiveExpression(null)); } TypeCode tc = Type.GetTypeCode(t); switch (tc) { case TypeCode.Boolean: return(new CodePrimitiveExpression(false)); case TypeCode.Byte: return(new CodePrimitiveExpression(default(byte))); case TypeCode.Char: return(new CodePrimitiveExpression(default(char))); case TypeCode.DateTime: return(ObjectCreationCodeGen.ObjectCreationCode(default(DateTime))); case TypeCode.Decimal: return(new CodePrimitiveExpression((decimal)0)); case TypeCode.Double: return(new CodePrimitiveExpression(default(double))); case TypeCode.Int16: return(new CodePrimitiveExpression(default(Int16))); case TypeCode.Int32: if (t.IsEnum) { Array a = Enum.GetValues(t); return(new CodeFieldReferenceExpression( new CodeTypeReferenceExpression(t), a.GetValue(0).ToString())); } return(new CodePrimitiveExpression(default(Int32))); case TypeCode.Int64: return(new CodePrimitiveExpression(default(Int64))); case TypeCode.SByte: return(new CodePrimitiveExpression(default(sbyte))); case TypeCode.Single: return(new CodePrimitiveExpression(default(Single))); case TypeCode.String: return(new CodePrimitiveExpression("")); case TypeCode.UInt16: return(new CodePrimitiveExpression(default(UInt16))); case TypeCode.UInt32: return(new CodePrimitiveExpression(default(UInt32))); case TypeCode.UInt64: return(new CodePrimitiveExpression(default(UInt64))); case TypeCode.Object: return(ObjectCreationCodeGen.ObjectCreationCode(VPLUtil.GetDefaultValue(t))); case TypeCode.DBNull: return(new CodePrimitiveExpression(null)); case TypeCode.Empty: return(new CodePrimitiveExpression(null)); default: return(new CodeDefaultValueExpression(new CodeTypeReference(t))); } }
/// <summary> /// multi-thread support: /// /// thread_{branch id}(object data) /// { /// object[] ps = (object[])data; /// type1 var1 = ps[0]; /// type2 var2 = ps[1]; /// ... /// {branch code} /// } /// </summary> /// <param name="compiler"></param> /// <param name="method"></param> /// <returns>true:all barnches of the main thread have method return or goto; false: at least one branch of the main thread does not have method return or goto</returns> public bool ExportCode(ILimnorCodeCompiler compiler, CodeMemberMethod method, CodeStatementCollection statements) { bool bRet = false; MathNode.Trace("BranchList.ExportCode. Method {0}, action blocks {1}================", method.Name, this.Count); //create code threads List <ActionBranch> independentThreads; if (_independentThreads == null) { independentThreads = FindoutActionThreads(true); } else { independentThreads = _independentThreads; } IsMultiThreads = (independentThreads.Count > 1); //the case of Count == 0 (empty method) is handled by MethodClass.ExportCode if (independentThreads.Count > 0) { int k0 = 0; // main thread index this.ActionThreads.Clear(); foreach (ActionBranch a in independentThreads) { _threads.Add(a.BranchId, a); } for (int k = 0; k < independentThreads.Count; k++) { if (independentThreads[k].IsMainThread) { k0 = k; break; } } if (k0 == 0) { independentThreads[0].IsMainThread = true; } this.MainThreadId = independentThreads[k0].BranchId; List <UInt32> usedBranches = new List <uint>(); for (int k = 0; k < independentThreads.Count; k++) { independentThreads[k].IsMainThread = (k == k0); independentThreads[k].SetIsMainThreadForSubBranches(usedBranches); } //generate additional threads CodeExpression[] obs = null; CodeExpression[] ps = null; for (int k = 0; k < independentThreads.Count; k++) { if (k != k0) { ActionBranch ab = independentThreads[k]; ab.IsMainThread = false; //method for this additional thread CodeMemberMethod mt = new CodeMemberMethod(); mt.Name = "thread_" + ab.BranchId.ToString("x"); compiler.TypeDeclaration.Members.Add(mt); mt.Parameters.Add(new CodeParameterDeclarationExpression(typeof(object), "data")); //use an object[] to hold the method parameters which is passed to the thread method via the data parameter string pas = "******" + Guid.NewGuid().GetHashCode().ToString("x"); mt.Statements.Add(new CodeVariableDeclarationStatement(typeof(object[]), pas, new CodeCastExpression(typeof(object[]), new CodeArgumentReferenceExpression("data")))); //create variables named after the method parameters and assign values to them //so that the actions within the method can use them as if they are method parameters. for (int i = 0; i < _method.ParameterCount; i++) { mt.Statements.Add(new CodeVariableDeclarationStatement(_method.Parameters[i].TypeString, _method.Parameters[i].Name, new CodeCastExpression(_method.Parameters[i].TypeString, new CodeArrayIndexerExpression(new CodeVariableReferenceExpression(pas), new CodePrimitiveExpression(i))))); } //method contents: a single thread ab.ExportThreadCode(compiler, mt, mt.Statements); // if (compiler.Debug) { mt.Statements.Add(new CodeExpressionStatement( new CodeMethodInvokeExpression( new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), LimnorDebugger.Debugger), "ThreadFinished" //,new CodePrimitiveExpression(ab.BranchId) ) ) ); } //code to launch this thread if (obs == null) { //create an object[] to hold all method parameters to be passed into the thread method obs = new CodeExpression[_method.ParameterCount]; for (int i = 0; i < _method.ParameterCount; i++) { obs[i] = new CodeArgumentReferenceExpression(_method.Parameters[i].Name); } } //parameters for launch the thread ps = new CodeExpression[2]; pas = "******" + Guid.NewGuid().GetHashCode().ToString("x"); statements.Add( new CodeVariableDeclarationStatement(typeof(object[]), pas, new CodeArrayCreateExpression(typeof(object), obs))); //delegate to the thread method ps[0] = new CodeDelegateCreateExpression(new CodeTypeReference(typeof(System.Threading.WaitCallback)), new CodeThisReferenceExpression(), mt.Name); //parameter to the thread method ps[1] = new CodeVariableReferenceExpression(pas); //queue the thread statements.Add(new CodeMethodInvokeExpression( new CodeTypeReferenceExpression(typeof(System.Threading.ThreadPool)), "QueueUserWorkItem", ps)); } } //main thread code bRet = independentThreads[k0].ExportThreadCode(compiler, method, statements); // if (_method == null) { MathNode.LogError("method is null"); } else // { if (_method.ReturnValue != null) { if (!typeof(void).Equals(_method.ReturnValue.ObjectType)) { //check whether all branches ends with a method return statement if (!independentThreads[k0].AllBranchesEndWithMethodReturnStatement()) { CodeExpression pe; if (_method.ReturnValue.ObjectType.IsArray) { pe = new CodePrimitiveExpression(null); } else { pe = ObjectCreationCodeGen.ObjectCreationCode(VPLUtil.GetDefaultValue(_method.ReturnValue.ObjectType)); } statements.Add(new CodeMethodReturnStatement(pe)); } } } } } MathNode.Trace("End of BranchList.ExportCode. Method {0}================", method.Name); return(bRet); }
public static CodeExpression GetDefaultValueExpression(Type t) { if (t == null) { return(null); } if (t.IsGenericType) { return(null); } if (t.IsGenericParameter) { return(null); } if (t.IsGenericTypeDefinition) { return(null); } bool isByref = t.IsByRef; if (isByref) { t = t.GetElementType(); if (typeof(object).Equals(t)) { //System.Reflection.Missing.Value return(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(System.Reflection.Missing)), "Value")); } } if (t.IsArray) { return(new CodePrimitiveExpression(null)); } if (t.IsEnum) { Array a = Enum.GetValues(t); if (a.Length > 0) { return(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(t), a.GetValue(0).ToString())); } return(new CodeCastExpression(t, new CodePrimitiveExpression(0))); } if (t.Equals(typeof(DateTime))) { return(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(DateTime)), "MinValue")); } if (t.Equals(typeof(IntPtr))) { return(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(IntPtr)), "Zero")); } if (t.Equals(typeof(UIntPtr))) { return(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(UIntPtr)), "Zero")); } if (t.IsPrimitive) { return(new CodePrimitiveExpression(VPLUtil.GetDefaultValue(t))); } if (t.Equals(typeof(string))) { return(new CodePrimitiveExpression(null)); } if (t.IsValueType) { // use Activator.CreateInstance<T>() CodeMethodInvokeExpression cmie = new CodeMethodInvokeExpression(); cmie.Method = new CodeMethodReferenceExpression(); cmie.Method.TargetObject = new CodeTypeReferenceExpression(typeof(Activator)); cmie.Method.MethodName = "CreateInstance"; cmie.Method.TypeArguments.Add(new CodeTypeReference(t)); return(cmie); } object v = VPLUtil.GetDefaultValue(t); return(ObjectCreationCodeGen.ObjectCreationCode(v)); }
public void ResetValue() { _value = VPLUtil.GetDefaultValue(_type); }
public override void ResetValue(object component) { _owner[Name] = VPLUtil.GetDefaultValue(_type); }
public override void ResetValue(object component) { _pif.SetValue(component, VPLUtil.GetDefaultValue(_valueType), null); }