#if UNITY_IOS || ENABLE_IL2CPP #define AOT #endif using System; using System.Collections.Generic; using System.ComponentModel; using System.Reflection; using System.Runtime.Serialization; namespace FlexFramework.Excel { /// /// Converter class for string data parsing /// public static class ValueConverter { private static readonly List converters = new List(); static ValueConverter() { Reset(); } /// /// Restore default converters /// public static void Reset() { converters.Clear(); Register(); Register(); Register(); Register(); Register(); Register(); Register(); #if AOT Register(System.Convert.ToBoolean); Register(System.Convert.ToByte); Register(System.Convert.ToChar); Register(System.Convert.ToDateTime); Register(System.Convert.ToDecimal); Register(System.Convert.ToDouble); Register(System.Convert.ToInt16); Register(System.Convert.ToInt32); Register(System.Convert.ToInt64); Register(System.Convert.ToSByte); Register(System.Convert.ToSingle); Register(System.Convert.ToString); Register(System.Convert.ToUInt16); Register(System.Convert.ToUInt32); Register(System.Convert.ToUInt64); Register(new ArrayConverter()); Register(new ArrayConverter()); Register(new ArrayConverter()); Register(new ListConverter()); Register(new ListConverter()); Register(new ListConverter()); Register(new DictionaryConverter()); Register(new DictionaryConverter()); Register(new DictionaryConverter()); Register(new DictionaryConverter()); #else Register>(); Register>(); Register>(); Register>(); Register>(); Register>(); Register>(); Register>(); Register>(); Register>(); #endif } /// /// Clear all registered converters /// public static void Empty() { converters.Clear(); } /// /// Convert input string to target type /// /// Target type /// input value /// Parsed value /// /// Registered global converters() will be applied if available /// /// Type is null /// Converter not found for target type /// public static object Convert(Type type, string value) { if (type == null) throw new ArgumentNullException("type"); var converter = converters.Find(c => c.Type == type); if (converter == null) { #if !AOT var @default = TypeDescriptor.GetConverter(type); if (@default == null) { throw new NotSupportedException("Converter not found for target type: " + type.Name); } converter = new Converter(i => @default.ConvertFromString(i), type); Register(converter); #else throw new NotSupportedException("Converter not found for target type: " + type.Name); #endif } try { return converter.Convert(value); } catch (FormatException) { throw; } catch (Exception ex) { throw new FormatException(ex.Message, ex); } } /// /// Convert input string to target type /// /// Target type /// input value /// Parsed value /// /// Registered global converters() will be applied if available /// public static T Convert(string value) { return (T)Convert(typeof(T), value); } /// /// Register a converter /// /// /// This overrides all converters of the same output type /// /// Converter instance /// true if registration succeeded public static bool Register(IConverter converter) { if (converter == null) { return false; } converters.RemoveAll(c => c.Type == converter.Type); converters.Add(converter); return true; } /// /// Register a converter /// /// /// This overrides all converters of the same output type /// /// Converter type /// true if registration succeeded public static bool Register() where T : IConverter, new() { return Register(new T()); } /// /// Register global delegate converter /// /// /// This overrides all converters of the same output type /// /// Output type /// Converter delegate /// true if registration succeeded public static bool Register(Converter converter) { return Register(new Converter(converter)); } /// /// Register global delegate converter /// /// /// This overrides all converters of the same output type /// /// Converter delegate /// Output type /// true if registration succeeded public static bool Register(Converter converter, Type type) { return Register(new Converter(converter, type)); } /// /// Unregister global converter of target output type /// /// Output type /// true if unregistration succeeded public static bool Unregister(Type type) { return converters.RemoveAll(c => c.Type == type) > 0; } /// /// Unregister global converter of target output type /// /// Output type /// true if unregistration succeeded public static bool Unregister() { return Unregister(typeof(T)); } private class Converter : IConverter { private Converter _converter; public Converter(Converter converter) { if (converter == null) { throw new ArgumentNullException(); } _converter = converter; } Type IConverter.Type { get { return typeof(T); } } object IConverter.Convert(string input) { return _converter(input); } } private class Converter : IConverter { private Converter _converter; private Type _type; public Converter(Converter converter, Type type) { if (converter == null || type == null) { throw new ArgumentNullException(); } _converter = converter; _type = type; } Type IConverter.Type { get { return _type; } } object IConverter.Convert(string input) { return _converter(input); } } } }