private static void DeserializeObject(object obj, ProtoObject proto) { var type = obj.GetType(); var props = type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); foreach (var pprop in proto.Properties) { var oprop = GetBestMatchingProperty(props, pprop.Name); if (oprop == null) { continue; } var value = DeserializeProperty(pprop, oprop); if (value == null) { continue; } var ptype = oprop.PropertyType; if (!ptype.IsGenericType) { oprop.SetValue(obj, ConvertValue(value, ptype)); } //todo: handle array properties similarly to List properties else { var gtype = typeof(ICollection <>); var args = ptype.GetGenericArguments(); var ctype = gtype.MakeGenericType(args); if (ctype.IsAssignableFrom(ptype)) { var existing = oprop.GetValue(obj); if (existing == null) { existing = Activator.CreateInstance(ptype); oprop.SetValue(obj, existing); } var add = ptype.GetMethod("Add"); if (add == null) { throw new Exception("Could not get Add method on type " + ptype); } var parm = add.GetParameters()[0]; add.Invoke(existing, new[] { ConvertValue(value, parm.ParameterType) }); } else { throw new Exception("Unexpected type " + ptype); } } } }
private static ProtoObject LexObject(IEnumerator<Token> enumerator, bool sub, ref string source) { var obj = new ProtoObject(); while (enumerator.MoveNext()) { var token = enumerator.Current; if (token.TokenType == typeof(NameToken)) { var propName = source.Substring(token.Position, token.Length); obj.Properties.Add(LexProp(propName, enumerator, ref source)); } else if (token.TokenType == typeof (Cbr) && sub) return obj; else throw UnexpectedTokenType(token, ref source); } return obj; }
private static ProtoObject LexObject(IEnumerator <Token> enumerator, bool sub, ref string source) { var obj = new ProtoObject(); while (enumerator.MoveNext()) { var token = enumerator.Current; if (token.TokenType == typeof(NameToken)) { var propName = source.Substring(token.Position, token.Length); obj.Properties.Add(LexProp(propName, enumerator, ref source)); } else if (token.TokenType == typeof(Cbr) && sub) { return(obj); } else { throw UnexpectedTokenType(token, ref source); } } return(obj); }