private void btnAddMethod_Click(object sender, EventArgs e) { //This method is now greatly changed to accomodate the new format of having variables in there //mostly just a awful lot of type checking, input verification stuff if (lblMethodName.Text.Split('-').Length == 2 && MethodsView.SelectedNode!=null) { TreeNode curSelection = MethodsView.SelectedNode; ListBoxProtocolItem CurProtocolItem = new ListBoxProtocolItem(); string instName = lblMethodName.Text.Split('-')[0]; string methName = lblMethodName.Text.Split('-')[1]; CurProtocolItem.Instruction.InstrumentName = instName; CurProtocolItem.Instruction.MethodName = methName; object[] Parameters; if (tblMethodParameterView.DataSource != null) { DataTable DT = (DataTable)tblMethodParameterView.DataSource; DataRow DR = DT.Rows[0]; Parameters = DR.ItemArray;//actual inputted values, as strings //Now we will find out what this value is, to make sure it is all agreeable string MethodName = curSelection.Text.ToString().Split('(')[0]; string InstrumentName = curSelection.Parent.Text; var Instrument = GetInstrumentClass(InstrumentName); var Meth = ReturnMethodInfoFromObject(MethodName, Instrument); ParameterInfo[] ParametersinMeth=Meth.GetParameters(); bool RequiresDynamicParameter = false; if (ParametersinMeth[ParametersinMeth.Length - 1].ParameterType == typeof(AdditionalMethodArguments)) RequiresDynamicParameter = true; int pCount = RequiresDynamicParameter ? ParametersinMeth.Length - 1 : ParametersinMeth.Length; Type[] ParameterTypes = new Type[pCount]; for (int j = 0; j < pCount; j++) {ParameterTypes[j]=ParametersinMeth[j].ParameterType;} if (ParameterTypes.Length != DR.ItemArray.Length) { MessageBox.Show("Inputted parameters and expected parameters do not have an equal count, contact Nigel"); return; } for (int i = 0; i < ParameterTypes.Length;i++ ) { object o = Parameters[i];//actual inputted value, currently as a string if (o.ToString() == "") { MessageBox.Show("You need to supply more parameters before this method can be added", "Parameters Not Supplied", MessageBoxButtons.OK, MessageBoxIcon.Hand); return; } if (ParameterTypes[i] == System.Type.GetType("System.String")) { //check that there are real variables in the if (!ProtocolManager.CheckStringForValidVariables((string)Parameters[i], (IList)lstCurrentVariables.Items)) { MessageBox.Show("You have entered a variable that does not exist","Bad Data Entry",MessageBoxButtons.OK,MessageBoxIcon.Error); } continue;//string types always get cleared } else//need to check if this is a variable, and if it is not, I neet to check that the method works out okay { Type ExpectedType = ParameterTypes[i]; //two cases, either they have entered a variable, or they have a value, to find out which string VariableName; bool ParameterIsVariable = ProtocolManager.GetVariableNameFromText((string)o, out VariableName); if (ParameterIsVariable) { //first to check that such a variable exists ReferenceToProtocolVariable Variable = new ReferenceToProtocolVariable(VariableName); if (!ProtocolManager.IsVariableInList(VariableName, (IList)lstCurrentVariables.Items)) { MessageBox.Show("You are using a variable name that does not exist in your protocol", "Bad Variable Name", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } else { ProtocolVariable VariableInList = new ProtocolVariable(); foreach (object Varia in lstCurrentVariables.Items) { if (Varia.ToString() == VariableName) { VariableInList = (ProtocolVariable)Varia; break; } } //make sure the variable type and parameter type match if (VariableInList.DataType != ParameterTypes[i]) { MessageBox.Show("You are using a variable that does not have the appropriate type for this parameter", "Bad Variable Type", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } else { //replace the "parameter" with the reference to the variable Parameters[i] = Variable; } } } else { //make sure I can actually convert the damn thing to what it should be eg "A"->"a" try { Parameters[i]=Convert.ChangeType((string)o, ParameterTypes[i]); } catch { MessageBox.Show("Could not convert your entry to the proper type, please make sure that numbers are used where they should be", "Bad Entry", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } } } } else { Parameters = new object[0]; } CurProtocolItem.Instruction.Parameters = Parameters; CurProtocolItem.InstructionNumber = GetNextInstructionNumber(); lstProtocol.Items.Add(CurProtocolItem); } }
public static Protocol XMLFileToProtocol(string Filename) { //Starting to approach the point where I need to break this up.... //7-27-2012 now trying to select nodes. Protocol NewProtocol = new Protocol(); XmlDocument XmlDoc = new XmlDocument(); XmlTextReader XReader = new XmlTextReader(Filename);//http://dn.codegear.com/article/32384 XmlDoc.Load(XReader); //first node is xml, second is the protocol, this is assumed and should be the case //XmlNode ProtocolXML = XmlDoc.ChildNodes[1]; XmlNode ProtocolXML = XmlDoc.SelectSingleNode("/Protocol"); XmlNode name=ProtocolXML.SelectSingleNode("/ProtocolName"); NewProtocol.ProtocolName = ProtocolXML.SelectSingleNode("ProtocolName").InnerText; NewProtocol.ErrorPhoneNumber = ProtocolXML.SelectSingleNode("ErrorPhoneNumber").InnerText; NewProtocol.ErrorEmailAddress = ProtocolXML.SelectSingleNode("ErrorEmailAddress").InnerText; Assembly assembly = Assembly.GetExecutingAssembly(); XmlNode VariablesNode = ProtocolXML.SelectSingleNode("Variables"); if (VariablesNode != null) { foreach (XmlNode variable in VariablesNode.ChildNodes) { string Name = variable.ChildNodes[0].InnerText; string variableTypeString = variable.ChildNodes[1].Attributes[0].Value; Type VariableType = System.Type.GetType(variableTypeString); object valueAsString = variable.ChildNodes[1].InnerText; var Value = Convert.ChangeType(valueAsString, VariableType); ProtocolVariable PV = new ProtocolVariable(Name, VariableType, Value); NewProtocol.Variables.Add(PV.ToString(), PV); } } XmlNode instructionsNode = ProtocolXML.SelectSingleNode("Instructions"); foreach (XmlNode instruct in instructionsNode.ChildNodes) { string DataType = instruct.Attributes[0].Value; Type InstructionType = assembly.GetType(DataType); object ProtocolInstruction = Activator.CreateInstance(InstructionType); string ValueType, Value; foreach (XmlNode Property in instruct.ChildNodes) { if (Property.Name == "Parameters")//this means I have a static protocol item { ArrayList AL = new ArrayList(); object[] ParameterArray; if (Property.ChildNodes.Count == 0) { ParameterArray = new object[0]; } else { ParameterArray = new object[Property.ChildNodes.Count]; int ParameterIndex = 0; foreach (XmlNode Parameter in Property.ChildNodes) { ValueType = Parameter.Attributes[0].Value; Value = Parameter.InnerText; ParameterArray[ParameterIndex] = ReturnValue(ValueType, Value); ParameterIndex++; } } StaticProtocolItem ProtocolItem = (StaticProtocolItem)ProtocolInstruction; ProtocolItem.Parameters = ParameterArray; ProtocolItem.ContainingProtocol = NewProtocol; } else { FieldInfo FI = ProtocolInstruction.GetType().GetField(Property.Name); ValueType = Property.Attributes[0].Value; Value = Property.InnerText; object ValuetoSet = ReturnValue(ValueType, Value); FI.SetValue(ProtocolInstruction, ValuetoSet); } } NewProtocol.Instructions.Add(ProtocolInstruction as ProtocolInstruction); } XReader.Close(); return NewProtocol; }
private void btrnAddVariable_Click(object sender, EventArgs e) { try { ProtocolVariable PV = new ProtocolVariable(); if (dataViewVariable.DataSource != null) { DataTable DT = (DataTable)dataViewVariable.DataSource; DataRow DR = DT.Rows[0]; foreach (object o in DR.ItemArray) { if (o.ToString() == "") { MessageBox.Show("You need to supply more information before this variable can be added", "Parameters not Supplied", MessageBoxButtons.OK, MessageBoxIcon.Hand); return; } } PV.Name = DR[0] as string; PV.Value = DR[1]; PV.DataType = DR[1].GetType(); if (lstVariableTypes.SelectedIndex != -1)//this is a new variable { foreach (object o in lstCurrentVariables.Items) { if (o.Equals(PV)) { MessageBox.Show("The variable name you have choosen is not unique"); return; } } } else //we are changing the parameter of our old variable { foreach (object o in lstCurrentVariables.Items) { if (o.Equals(PV)) { PV = (ProtocolVariable)o;//change the reference to ensure that the items in the protocol all point to same object, and we don't use a new one PV.Value = DR[1]; lstCurrentVariables.Items.Remove(o); break; } } } lstCurrentVariables.Items.Add(PV); } } catch { ShowGenericError(); } }