Ejemplo n.º 1
0
        public override void SaveToXML(object targetObject, XMLScribe scribe)
        {
            //Log.Message($"[Object] SaveToXML='{targetObject?.ToString()}'");
            if (targetObject == null)
            {
                return;
            }

            Type type = targetObject.GetType();

            //Custom behavior.
            if (targetObject is IExposeData exposed)
            {
                exposed.ExposeData(scribe);
                return;
            }

            //Default behavior.
            if (type.IsClass)
            {
                foreach (FieldInfo field in type.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance))
                {
                    //Log.Message($"Field='{field.Name}'");
                    XMLTypeConverter converter = XMLTools.GetXMLProperTypeConverter(field.FieldType);
                    if (converter == null)
                    {
                        Log.Error($"Found no suitable converter for Node='{(scribe.Current.Name + '/' + field.Name)}' in Document='{scribe.DocumentPath}'");
                    }
                    else
                    {
                        object  targetObjectField = field.GetValue(targetObject);
                        XmlNode targetNode        = scribe.EnterNode(field.Name);
                        converter.SaveToXML(targetObjectField, scribe);
                        scribe.ExitNode();
                    }
                }
            }
        }