/// <summary> /// Create record object from serialized record string /// </summary> /// <param name="recordString">serialized record string</param> /// <param name="recordfType">record type to create [WorkFlowTrackRecord or ActivityTrackRecord]</param> /// <returns></returns> public static BaseTrackRecord CreateObjectFromString(string recordString, Type recordfType) { BaseTrackRecord newRecord = (BaseTrackRecord)Activator.CreateInstance(recordfType); PropertyInfo[] pInfoArr = newRecord.GetType().GetProperties(BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Public); foreach (var propInfo in pInfoArr) { Regex rx = new Regex(@"" + propInfo.Name + @"(\s*)=(\s*)""([^""]*)""", RegexOptions.IgnoreCase); Match m = rx.Match(recordString); if (m.Success && m.Groups.Count > 0) { string val = m.Groups[m.Groups.Count - 1].ToString().Trim(); //get prop type var targetType = TConverter.GetPropType(propInfo); //Convert value to property type object convertedValue = TConverter.ChangeType(targetType, val); //set object value propInfo.SetValue(newRecord, convertedValue, null); } } return(newRecord); }
private static void ConvertXmlNodeAttribValueToObjProp(XmlAttribute att, ref BaseTrackRecord trackRecord) { PropertyInfo propInfo = trackRecord.GetType().GetProperty(att.Name, BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Public); if (propInfo != null) { //get prop type var targetType = TConverter.GetPropType(propInfo); //Convert value to property type object convertedValue = TConverter.ChangeType(targetType, att.Value); //set object value propInfo.SetValue(trackRecord, convertedValue, null); } }
/// <summary> /// Build record objects from record strings /// </summary> /// <param name="recordStrings">record strings</param> /// <returns></returns> public IList <BaseTrackRecord> GetTrackRecordsFromRecordString(string[] recordStrings) { IList <BaseTrackRecord> trackRecords = new List <BaseTrackRecord>(); foreach (var recordString in recordStrings) { BaseTrackRecord record = null; if (recordString.StartsWith("<" + WorkFlowTrackRecord.XMLTagName)) { record = BaseTrackRecord.CreateObjectFromString(recordString, typeof(WorkFlowTrackRecord)); } else if (recordString.StartsWith("<" + ActivityTrackRecord.XMLTagName)) { record = BaseTrackRecord.CreateObjectFromString(recordString, typeof(ActivityTrackRecord)); } if (record != null) { trackRecords.Add(record); } } return(trackRecords); }