//Convert list of strings to ResultsDataCommon public ResultsDataCommon GenerateRDC(string[] inputData) { var common = new ResultsDataCommon(); //blank common data BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static; FieldInfo[] commonFields = typeof(ResultsDataCommon).GetFields(bindingFlags); for (int cf = 0; cf < commonFields.Length; cf++) { string value = inputData[cf]; FieldInfo fieldInfo = common.GetType().GetField(commonFields[cf].Name); fieldInfo.SetValue(common, Convert.ChangeType(value, fieldInfo.FieldType)); } return(common); }
// ------------------------------------------------------------------------------------ // Serialization // TODO - Clean and comment (DANGER) // Deserialize ResultsIOData(string arrays) to ResultsData(class) public override object DeserializeResults(ResultsIOData resultsIOData) { //var resultData = Convert.ChangeType(activeBaselineData, results); // Create instance (Old - Used from base class) ResultsBase resultData = (R)Activator.CreateInstance(results); // Create instance var common = new ResultsDataCommon(); //blank common data BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static; FieldInfo[] commonFields = typeof(ResultsDataCommon).GetFields(bindingFlags); FieldInfo[] customFields = results.GetFields(bindingFlags); List <string> commonDataRaw = resultsIOData.resultsRow[0].resultsColumn.GetRange(0, commonFields.Length); List <string> resultsDataRaw = resultsIOData.resultsRow[0].resultsColumn.GetRange(commonFields.Length, resultsIOData.resultsRow[0].resultsColumn.Count - (commonFields.Length)); for (int f = 0; f < customFields.Length; f++) { if (f == 0) { //do the common class for (int cf = 0; cf < commonFields.Length; cf++) { string value = commonDataRaw[cf]; FieldInfo fieldInfo = common.GetType().GetField(commonFields[cf].Name); fieldInfo.SetValue(common, Convert.ChangeType(value, fieldInfo.FieldType)); } } else { var value = resultsDataRaw[(f) - 1]; FieldInfo fieldInfo = resultData.GetType().GetField(customFields[0].Name); // TODO - Why did this become 0? if (fieldInfo.FieldType.IsArray) // This handles arrays { Type type = resultData.GetType().GetField(customFields[f].Name).FieldType.GetElementType(); GenerateGenericArray(fieldInfo, resultData.GetType(), resultData, type, value); } else // Non array types { fieldInfo.SetValue(resultData, Convert.ChangeType(value, fieldInfo.FieldType)); } } } resultData.common = common; // Assign common return(resultData); }
// ------------------------------------------------------------------------------------ // Serialization // TODO - Clean and comment (DANGER) // Serialize ResultsData(class) to ResultsIOData(string arrays) public ResultsIOData SerializeResults() { ResultsIOData output = new ResultsIOData(); output.resultsRow.Add(new ResultsIORow()); BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static; FieldInfo[] commonFields = typeof(ResultsDataCommon).GetFields(bindingFlags); FieldInfo[] customFields = results.GetFields(bindingFlags); //Store field names into the ResultsIOData for (int f = 0; f < commonFields.Length; f++) { output.fieldNames.Add(commonFields[f].Name); } for (int f = 0; f < customFields.Length - 1; f++) { output.fieldNames.Add(customFields[f].Name); } output.resultsRow[0].resultsColumn = new List <string>(); ResultsDataCommon resultsCommonTemplate = new ResultsDataCommon(); for (int f = 0; f < commonFields.Length; f++) { var typedResult = Convert.ChangeType(activeResultData, results); // TODO - Why does this work... FieldInfo typedCommonField = typedResult.GetType().GetField("common"); // TODO - Why does this work... var typedCommonValue = Convert.ChangeType(typedCommonField.GetValue(typedResult), resultsCommonTemplate.GetType()); // TODO - Why does this work... var commonResult = typedCommonValue.GetType().GetField(commonFields[f].Name).GetValue(typedCommonValue); output.resultsRow[0].resultsColumn.Add(commonResult.ToString()); } for (int f = 0; f < customFields.Length - 1; f++) { var customResult = activeResultData.GetType().GetField(customFields[f].Name).GetValue(activeResultData); if (activeResultData.GetType().GetField(customFields[f].Name).FieldType.IsArray) //If its an array (tough to handle) { Array a = (Array)activeResultData.GetType().GetField(customFields[f].Name).GetValue(activeResultData); if (a != null) // Null check incase custom results werent set on an array { string[] stringArray = new string[a.Length]; for (int i = 0; i < a.Length; i++) { stringArray[i] = a.GetValue(i).ToString(); } customResult = Common.ConvertStringArrayToString(stringArray); output.resultsRow[0].resultsColumn.Add(customResult.ToString()); } else // Write blank when custom results werent set on an array { customResult = ""; } } else if (customResult != null) //If its a non-array type that has had values set { output.resultsRow[0].resultsColumn.Add(customResult.ToString()); } else //If its a non-array type that has not had values set { output.resultsRow[0].resultsColumn.Add(""); } } Console.Instance.Write(DebugLevel.Full, MessageLevel.Log, GetType().Name + " generated resultsIO data"); // Write to console return(output); }