public void AddChild(IMwxObject child) { if (child is MwxTestObject) _children.Add(child as MwxTestObject); else throw new Exception("Children must be of type MwxTestObject."); }
/// <exception cref="NotImplementedException"></exception> public void AddChild(IMwxObject child) { throw new System.NotImplementedException(); }
/// <summary> /// Adds an object to the writer. /// </summary> public void Add(IMwxObject obj) { _objects.Add(obj); }
/// <summary> /// Writes the given object to the xml stream. /// </summary> private void WriteObject(XmlWriter writer, IMwxObject obj) { // replace generic declarations var typeName = GetLocalName(obj.GetType()); if (typeName.Contains("`")) { var genericType = typeName.Split('[').Last(); genericType = genericType.Substring(0, genericType.Length-1); typeName = typeName.Split('`')[0]; writer.WriteStartElement(typeName); writer.WriteAttributeString("GenericType", genericType); } else // non-generic { writer.WriteStartElement(typeName); } // retrieve the mwx properties var mwxProps = obj.GetMwxProperties(); // write the attribute properties var attrProps = from prop in mwxProps where prop.Type == MwxPropertyType.Attribute select prop; foreach (var prop in attrProps) { var val = prop.PropertyInfo.GetValue(obj, new object[] { }); var valString = ""; if (val != null) valString = val.ToString(); writer.WriteAttributeString(prop.Name, valString); } // write the reference properties var refProps = from prop in mwxProps where prop.Type == MwxPropertyType.Reference select prop; foreach (var prop in refProps) { var val = prop.PropertyInfo.GetValue(obj, new object[] { }); if (val is IMwxObject) writer.WriteAttributeString(prop.Name, (val as IMwxObject).Name); else throw new NotImplementedException("Don't know how to reference non-mwx objects like " + prop.PropertyInfo.PropertyType); } // write the child properties var excludeChildren = new List<IMwxObject>(); var childProps = from prop in mwxProps where prop.Type == MwxPropertyType.Child select prop; foreach (var prop in childProps) { var val = prop.PropertyInfo.GetValue(obj, new object[] { }); if (val is IMwxObject) { writer.WriteStartElement("MwxProperty"); writer.WriteAttributeString("Name", prop.Name); WriteObject(writer, val as IMwxObject); writer.WriteEndElement(); excludeChildren.Add(val as IMwxObject); } else throw new NotImplementedException("Child properties need to implement IMwxObject, unlike " + prop.PropertyInfo.PropertyType); } // write the mwx children foreach (var child in obj.GetMwxChildren()) { if (child is IMwxObject) { var mwxChild = child as IMwxObject; if (excludeChildren.Contains(mwxChild)) continue; WriteObject(writer, child); } else throw new NotImplementedException("Can't write children that don't implement IMwxObject, like " + child); } writer.WriteEndElement(); }
/// <summary> /// This must be implemented by subclasses if they plan on being able to support mwx children. /// </summary> /// <remarks>Subclasses should not call this method, as it throws a NotImplementedException.</remarks> public virtual void AddChild(IMwxObject child) { throw new NotImplementedException(String.Format("Type {0} does not support adding children.", GetType())); }