internal ConfigObjectNode(dynamic n, KeyType key, ConfigNode parent) : base(key, parent) { _children = new Dictionary<string, ConfigNode>(); var expando = n as IDictionary<string, object>; foreach (var kvp in expando) { ConfigNode valueNode; var newKey = new StringKey(kvp.Key); if (kvp.Value is ExpandoObject) { valueNode = ConfigNodeFactory.CreateConfigObjectNode(kvp.Value, newKey, this); } else if (kvp.Value is IList<object>) { valueNode = ConfigNodeFactory.CreateConfigListNode(kvp.Value, newKey, this); } else { valueNode = ConfigNodeFactory.CreateConfigLeafNode(kvp.Value, newKey, this); } _children.Add(kvp.Key, valueNode); } }
public override bool TrySetMember(SetMemberBinder binder, object value) { if (_children.ContainsKey(binder.Name)) //replacing existing leaf node { ConfigNode thisNode = _children[binder.Name]; if (value is ConfigNode) { var strKey = new StringKey(binder.Name); ReplaceChild((ConfigNode)value, strKey); } else if (PrimitiveTypeHelper.IsPrimitiveOrWrapper(value.GetType())) { if (thisNode.NodeType == ConfigNodeType.LEAF) { var n = (ConfigLeafNode) thisNode; if (n.IsEncrypted) { n.ReplaceEncryptedContent(((string)value)); } else { n.RawValue = value; } } else { throw new NotSupportedException("Cannot set values to object or array node, " + _key + ": " + binder.Name); } } else { throw new ArgumentException("Cannot recognize the type of new value: " + value.GetType().Name); } } else //set new properties { if (value is ConfigNode) { AppendChild((ConfigNode)value); } else if (PrimitiveTypeHelper.IsPrimitiveOrWrapper(value.GetType())) { //var s = String.Format("{0}", value); var n = ConfigNodeFactory.CreateConfigLeafNode(value, new StringKey(binder.Name), this); AppendChild(n); } else { throw new ArgumentException("Cannot recognize the type of new value: " + value.GetType().Name); } } return true; }