コード例 #1
0
        public void LookupValue <T>(ref T value, string key, T defaultValue = default(T), bool saveNull = false)
        {
            switch (mode)
            {
            case ScribeMode.Saving:
            {
                XmlNode valueNode = EnterNode(key);
                SaveValueIntoNode(value, saveNull);
                ExitNode();
            }
            break;

            case ScribeMode.Loading:
            {
                if (Current.SelectSingleNode(key) is XmlNode valueNode)
                {
                    EnterNode(valueNode);

                    Type             type      = typeof(T);
                    XMLTypeConverter converter = XMLTools.GetXMLProperTypeConverter(type);
                    if (converter == null)
                    {
                        Log.Error($"Found no suitable converter for Node='{(Current.Name)}' in Document='{DocumentPath}'");
                    }
                    else
                    {
                        Log.Message($"Converting with '{converter.GetType().Name}' Node='{valueNode.Name}' with Type='{type.Name}' with Value='{valueNode.InnerXml}'");
                        object objectValue = converter.ConvertFromXML(type, Current);
                        if (objectValue != null && objectValue.GetType() != value.GetType())
                        {
                            Log.Error($"Type mismatch when converting. Value='{objectValue}' in Node='{valueNode.Name}'");
                        }
                        else
                        {
                            value = (T)objectValue;
                        }
                    }

                    ExitNode();
                }
            }
            break;
            }
        }
コード例 #2
0
        public void SaveValueIntoNode(object value, bool saveNull = false)
        {
            if (value == null && !saveNull)
            {
                return;
            }

            Type type = value.GetType();
            //Log.Message($"Saving value='{value?.ToString()}'");
            XMLTypeConverter converter = XMLTools.GetXMLProperTypeConverter(type);

            if (converter == null)
            {
                Log.Error($"Found no suitable converter for Node='{(Current.Name)}' in Document='{DocumentPath}'");
            }
            else
            {
                converter.SaveToXML(value, this);
            }
        }