mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-13 11:44:57 +00:00
feat: DefaultXmlConverter knows type it is serializing to/from
This commit is contained in:
@@ -1,9 +1,31 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace ICD.Common.Utils.Xml
|
||||
{
|
||||
public sealed class DefaultXmlConverter : AbstractXmlConverter
|
||||
public sealed class DefaultXmlConverter : AbstractXmlConverter
|
||||
{
|
||||
private static readonly Dictionary<Type, DefaultXmlConverter> s_Instances;
|
||||
|
||||
private readonly Type m_SerializeType;
|
||||
|
||||
/// <summary>
|
||||
/// Static constructor.
|
||||
/// </summary>
|
||||
static DefaultXmlConverter()
|
||||
{
|
||||
s_Instances = new Dictionary<Type, DefaultXmlConverter>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Constructor.
|
||||
/// </summary>
|
||||
/// <param name="serializeType"></param>
|
||||
private DefaultXmlConverter(Type serializeType)
|
||||
{
|
||||
m_SerializeType = serializeType;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes the XML representation of the object.
|
||||
/// </summary>
|
||||
@@ -28,5 +50,23 @@ namespace ICD.Common.Utils.Xml
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the converter instance for the given serialization type.
|
||||
/// </summary>
|
||||
/// <param name="type"></param>
|
||||
/// <returns></returns>
|
||||
public static IXmlConverter Instance(Type type)
|
||||
{
|
||||
DefaultXmlConverter converter;
|
||||
|
||||
if (!s_Instances.TryGetValue(type, out converter))
|
||||
{
|
||||
converter = new DefaultXmlConverter(type);
|
||||
s_Instances[type] = converter;
|
||||
}
|
||||
|
||||
return converter;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,7 +42,7 @@ namespace ICD.Common.Utils.Xml
|
||||
/// <returns></returns>
|
||||
public static IXmlConverter GetConverterForInstance(object value)
|
||||
{
|
||||
return value == null ? LazyLoadConverter(typeof(DefaultXmlConverter)) : GetConverterForType(value.GetType());
|
||||
return value == null ? DefaultXmlConverter.Instance(typeof(object)) : GetConverterForType(value.GetType());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -59,9 +59,9 @@ namespace ICD.Common.Utils.Xml
|
||||
if (!s_InstanceTypeToConverter.TryGetValue(type, out converter))
|
||||
{
|
||||
XmlConverterAttribute attribute = AttributeUtils.GetClassAttribute<XmlConverterAttribute>(type);
|
||||
Type converterType = attribute == null ? typeof(DefaultXmlConverter) : attribute.ConverterType;
|
||||
Type converterType = attribute == null ? null : attribute.ConverterType;
|
||||
|
||||
converter = LazyLoadConverter(converterType);
|
||||
converter = converterType == null ? DefaultXmlConverter.Instance(type) : LazyLoadConverter(converterType);
|
||||
s_InstanceTypeToConverter[type] = converter;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user