private static bool TryTypeConverterConversion(Type type, string value, out object newValue) { #if !SILVERLIGHT var converter = TypeDescriptor.GetConverter(type); if (converter.CanConvertFrom(typeof(string))) { newValue = converter.ConvertFromInvariantString(value); return(true); } #else if (type == typeof(LineEndingMode)) { newValue = LineEndingMode.FromString(value); return(true); } else if (type == typeof(Uri)) { newValue = new Uri(value); return(true); } #endif newValue = null; return(false); }
#pragma warning restore S1144 // Unused private types or members should be removed private static Dictionary <Type, Func <string, ConfigurationItemFactory, object> > BuildPropertyConversionMapper() { return(new Dictionary <Type, Func <string, ConfigurationItemFactory, object> >() { { typeof(Layout), TryParseLayoutValue }, { typeof(SimpleLayout), TryParseLayoutValue }, { typeof(ConditionExpression), TryParseConditionValue }, { typeof(Encoding), TryParseEncodingValue }, { typeof(string), (stringvalue, factory) => stringvalue }, { typeof(int), (stringvalue, factory) => Convert.ChangeType(stringvalue.Trim(), TypeCode.Int32, CultureInfo.InvariantCulture) }, { typeof(bool), (stringvalue, factory) => Convert.ChangeType(stringvalue.Trim(), TypeCode.Boolean, CultureInfo.InvariantCulture) }, { typeof(CultureInfo), (stringvalue, factory) => new CultureInfo(stringvalue.Trim()) }, { typeof(Type), (stringvalue, factory) => Type.GetType(stringvalue.Trim(), true) }, { typeof(LineEndingMode), (stringvalue, factory) => LineEndingMode.FromString(stringvalue.Trim()) }, { typeof(Uri), (stringvalue, factory) => new Uri(stringvalue.Trim()) } }); }
private static bool TrySpecialConversion(Type type, string value, out object newValue) { if (type == typeof(Encoding)) { value = value.Trim(); if (string.Equals(value, nameof(Encoding.UTF8), StringComparison.OrdinalIgnoreCase)) { value = Encoding.UTF8.WebName; // Support utf8 without hyphen (And not just Utf-8) } newValue = Encoding.GetEncoding(value); return(true); } if (type == typeof(CultureInfo)) { value = value.Trim(); newValue = new CultureInfo(value); return(true); } if (type == typeof(Type)) { value = value.Trim(); newValue = Type.GetType(value, true); return(true); } if (type == typeof(LineEndingMode)) { newValue = LineEndingMode.FromString(value); return(true); } if (type == typeof(Uri)) { newValue = new Uri(value); return(true); } newValue = null; return(false); }