static Configuration() { mNumberFormat = CultureInfo.InvariantCulture.NumberFormat; mDateTimeFormat = CultureInfo.InvariantCulture.DateTimeFormat; mValidCommentChars = new[] { '#', ';', '\'' }; mArrayElementSeparator = ','; mFallbackConverter = new FallbackStringConverter(); mTypeStringConverters = new Dictionary <Type, ITypeStringConverter>() { { typeof(bool), new BoolStringConverter() }, { typeof(byte), new ByteStringConverter() }, { typeof(char), new CharStringConverter() }, { typeof(DateTime), new DateTimeStringConverter() }, { typeof(decimal), new DecimalStringConverter() }, { typeof(double), new DoubleStringConverter() }, { typeof(Enum), new EnumStringConverter() }, { typeof(short), new Int16StringConverter() }, { typeof(int), new Int32StringConverter() }, { typeof(long), new Int64StringConverter() }, { typeof(sbyte), new SByteStringConverter() }, { typeof(float), new SingleStringConverter() }, { typeof(string), new StringStringConverter() }, { typeof(ushort), new UInt16StringConverter() }, { typeof(uint), new UInt32StringConverter() }, { typeof(ulong), new UInt64StringConverter() } }; IgnoreInlineComments = false; IgnorePreComments = false; }
/// <summary> /// 增加转换器 /// </summary> /// <param name="type">类型对应的转换器</param> /// <param name="converter">转换器</param> public void AddConverter(Type type, ITypeStringConverter converter) { Guard.NotNull(type, "type"); Guard.NotNull(converter, "converter"); typeStringConverters.Remove(type); typeStringConverters.Add(type, converter); }
internal static ITypeStringConverter FindTypeStringConverter(Type type) { ITypeStringConverter converter = null; if (!mTypeStringConverters.TryGetValue(type, out converter)) { converter = mFallbackConverter; } return(converter); }
/// <summary> /// 获取类型所需的转换器 /// </summary> /// <param name="type">类型</param> /// <param name="converter">转换器</param> /// <returns></returns> private bool GetCoverter(Type type, out ITypeStringConverter converter) { bool status; do { status = typeStringConverters.TryGetValue(type, out converter); type = type.BaseType; } while (!status && type != null); return(status); }
/// <summary> /// Registers a type converter to be used for setting value conversions. /// </summary> /// <param name="converter">The converter to register.</param> /// /// <exception cref="ArgumentNullException">When <paramref name="converter"/> is null.</exception> /// <exception cref="InvalidOperationException">When a converter for the converter's type is already registered.</exception> public static void RegisterTypeStringConverter(ITypeStringConverter converter) { if (converter == null) { throw new ArgumentNullException("converter"); } var type = converter.ConvertibleType; if (mTypeStringConverters.ContainsKey(type)) { throw new InvalidOperationException($"A converter for type '{type.FullName}' is already registered."); } mTypeStringConverters.Add(type, converter); }
static Configuration() { // For now, clone the invariant culture so that the // deprecated DateTimeFormat/NumberFormat properties still work, // but without modifying the real invariant culture instance. mCultureInfo = (CultureInfo)CultureInfo.InvariantCulture.Clone(); ValidCommentChars = new[] { '#', ';' }; mPreferredCommentChar = '#'; mArrayElementSeparator = ','; mFallbackConverter = new FallbackStringConverter(); // Add all stock converters. mTypeStringConverters = new Dictionary <Type, ITypeStringConverter>() { { typeof(bool), new BoolStringConverter() }, { typeof(byte), new ByteStringConverter() }, { typeof(char), new CharStringConverter() }, { typeof(DateTime), new DateTimeStringConverter() }, { typeof(decimal), new DecimalStringConverter() }, { typeof(double), new DoubleStringConverter() }, { typeof(Enum), new EnumStringConverter() }, { typeof(short), new Int16StringConverter() }, { typeof(int), new Int32StringConverter() }, { typeof(long), new Int64StringConverter() }, { typeof(sbyte), new SByteStringConverter() }, { typeof(float), new SingleStringConverter() }, { typeof(string), new StringStringConverter() }, { typeof(ushort), new UInt16StringConverter() }, { typeof(uint), new UInt32StringConverter() }, { typeof(ulong), new UInt64StringConverter() } }; IgnoreInlineComments = false; IgnorePreComments = false; SpaceBetweenEquals = false; OutputRawStringValues = false; }