public bool TryParseGenericList(ParseEntry parseEntry, ObjectInstance newObjectInstance, ObjectInstance objectInstance) { var types = objectInstance.PropertyInfos.Where( d => d.PropertyType.IsGenericType && d.PropertyType.GetGenericArguments().Length == 1 && d.PropertyType.GetGenericArguments()[0] == parseEntry.Type).ToArray(); foreach (var propertyInfo in types) { var typeArgs = new[] { propertyInfo.PropertyType.GetGenericArguments()[0] }; var listType = typeof(List<>).MakeGenericType(typeArgs); if (listType == propertyInfo.PropertyType) { if (!string.IsNullOrEmpty(parseEntry.ParentAttributeName) && parseEntry.ParentAttributeName != propertyInfo.Name) continue; var propInfo = objectInstance.Instance.GetType().GetProperties().First(d => d.PropertyType == listType); var listInstance = propInfo.GetValue(objectInstance.Instance, null); if (listInstance == null) { listInstance = Activator.CreateInstance(listType); propInfo.SetValue(objectInstance.Instance, listInstance, null); } var addMethod = listType.GetMethod("Add"); addMethod.Invoke(listInstance, new[] { newObjectInstance.Instance }); return true; } } return false; }
public ObjectInstance ParseLine(ParseEntry parseEntry, string line) { if (parseEntry == null) { throw new ArgumentException("ParseEntry parameter needed to parse line", "parseEntry"); } if (line == null) { throw new ArgumentException("Line to parse needs to be specified", "line"); } var match = parseEntry.ParseFormat.Match(line); if (match.Groups.Count == 1) { return null; } if (parseEntry.PropertyInfos == null) { parseEntry.PropertyInfos = parseEntry.Type.GetProperties(BindingFlags.Public | BindingFlags.Instance); } //Create instance var inst = Activator.CreateInstance(parseEntry.Type); foreach (var propertyInfo in parseEntry.PropertyInfos) { SetPropertyValue(match.Groups[propertyInfo.Name].Value, inst, propertyInfo); } return new ObjectInstance { Instance = inst, PropertyInfos = parseEntry.PropertyInfos}; }
public bool TryParseGenericList(ParseEntry parseEntry, ObjectInstance newObjectInstance, ObjectInstance objectInstance) { var types = objectInstance.PropertyInfos.Where( d => d.PropertyType.IsGenericType && d.PropertyType.GetGenericArguments().Length == 1 && d.PropertyType.GetGenericArguments()[0] == parseEntry.Type).ToArray(); foreach (var propertyInfo in types) { var typeArgs = new[] { propertyInfo.PropertyType.GetGenericArguments()[0] }; var listType = typeof(List <>).MakeGenericType(typeArgs); if (listType == propertyInfo.PropertyType) { if (!string.IsNullOrEmpty(parseEntry.ParentAttributeName) && parseEntry.ParentAttributeName != propertyInfo.Name) { continue; } var propInfo = objectInstance.Instance.GetType().GetProperties().First(d => d.PropertyType == listType); var listInstance = propInfo.GetValue(objectInstance.Instance, null); if (listInstance == null) { listInstance = Activator.CreateInstance(listType); propInfo.SetValue(objectInstance.Instance, listInstance, null); } var addMethod = listType.GetMethod("Add"); addMethod.Invoke(listInstance, new[] { newObjectInstance.Instance }); return(true); } } return(false); }
public ObjectInstance ParseLine(ParseEntry parseEntry, string line) { if (parseEntry == null) { throw new ArgumentException("ParseEntry parameter needed to parse line", "parseEntry"); } if (line == null) { throw new ArgumentException("Line to parse needs to be specified", "line"); } var match = parseEntry.ParseFormat.Match(line); if (match.Groups.Count == 1) { return(null); } if (parseEntry.PropertyInfos == null) { parseEntry.PropertyInfos = parseEntry.Type.GetProperties(BindingFlags.Public | BindingFlags.Instance); } //Create instance var inst = Activator.CreateInstance(parseEntry.Type); foreach (var propertyInfo in parseEntry.PropertyInfos) { SetPropertyValue(match.Groups[propertyInfo.Name].Value, inst, propertyInfo); } return(new ObjectInstance { Instance = inst, PropertyInfos = parseEntry.PropertyInfos }); }
public void ParseLine_ParsingAddressParseEntry_ReturnsAddressInstance() { var parser = new Parser(); var parseEntry = new ParseEntry { ParseFormat = new Regex("^.+;(?<Name>.+);(?<StreetAddress>.+);(?<PoBox>.+)$"), Type = typeof (Address) }; var parseType = parser.ParseLine(parseEntry, "03;AddressRow1;AddressXX;PoBoxYY"); Assert.IsInstanceOfType(parseType.Instance, typeof(Address)); }
private bool ParseLineAndSetNode(ParseEntry parseEntry, string line) { var newObjectInstance = _parser.ParseLine(parseEntry, line); if (newObjectInstance == null) { return(false); } while (_objectInstanceStack.Count >= 1) { var objectInstanceFromStack = _objectInstanceStack.Peek(); var propInfos = objectInstanceFromStack.PropertyInfos.Where(d => d.PropertyType == parseEntry.Type).ToArray(); foreach (var propInfo in propInfos) { if (!string.IsNullOrEmpty(parseEntry.ParentAttributeName) && parseEntry.ParentAttributeName != propInfo.Name) { continue; } //Already set then throw if (propInfo.GetValue(objectInstanceFromStack.Instance, null) != null) { throw new InvalidOperationException("Already set"); } propInfo.SetValue(objectInstanceFromStack.Instance, newObjectInstance.Instance, null); _objectInstanceStack.Push(newObjectInstance); return(true); } if (_parser.TryParseGenericList(parseEntry, newObjectInstance, objectInstanceFromStack)) { if (objectInstanceFromStack != newObjectInstance) { _objectInstanceStack.Push(newObjectInstance); } return(true); } if (_objectInstanceStack.Count == 1) { return(false); } //Just remove it _objectInstanceStack.Pop(); } return(false); }
public void ParseLine_ParsingAddressParseEntry_SetStringPropertiesOnInstance() { var parser = new Parser(); var parseEntry = new ParseEntry { ParseFormat = new Regex("^.+;(?<Name>.+);(?<StreetAddress>.+);(?<PoBox>.+)$"), Type = typeof(Address) }; var parseType = parser.ParseLine(parseEntry, "03;TheName;AddressXX;PoBoxYY"); var address = parseType.Instance as Address; Assert.IsNotNull(address); Assert.AreEqual(address.Name, "TheName"); Assert.AreEqual(address.StreetAddress, "AddressXX"); Assert.AreEqual(address.PoBox, "PoBoxYY"); }
public IList <T> ParseArray(Stream stream) { var list = new List <T>(); var type = typeof(T); var attr = type.GetCustomAttributes(typeof(ParseFormatAttribute), false)[0] as ParseFormatAttribute; if (attr == null) { AddError(string.Format("Object of type {0} doesn't have any ParseFormatAttributes", typeof(T).Name), 0); return(null); } var parseEntry = new ParseEntry { Type = type, ParseFormat = new Regex(attr.Format), ParentAttributeName = attr.ParentAttributeName }; using (var sr = new StreamReader(stream)) { string line; int row = 1; while ((line = sr.ReadLine()) != null) { var parseType = _parser.ParseLine(parseEntry, line); if (parseType == null) { AddError(string.Format("Problem adding node for line: {0}", line), row); continue; } list.Add(parseType.Instance as T); row++; } } return(list); }