System.Configuration.SettingsPropertyValue.ConvertObjectToString C# (CSharp) Метод

ConvertObjectToString() приватный статический Метод

private static ConvertObjectToString ( object propValue, Type type, SettingsSerializeAs serializeAs, bool throwOnError ) : string
propValue object
type Type
serializeAs SettingsSerializeAs
throwOnError bool
Результат string
        private static string ConvertObjectToString(object propValue, Type type, SettingsSerializeAs serializeAs, bool throwOnError)
        {
            if (serializeAs == SettingsSerializeAs.ProviderSpecific) {
                if (type == typeof(string) || type.IsPrimitive)
                    serializeAs = SettingsSerializeAs.String;
                else
                    serializeAs = SettingsSerializeAs.Xml;
            }

            try {
                switch (serializeAs) {
                case SettingsSerializeAs.String:
                    TypeConverter converter = TypeDescriptor.GetConverter(type);
                    if (converter != null && converter.CanConvertTo(typeof(String)) && converter.CanConvertFrom(typeof(String)))
                        return converter.ConvertToInvariantString(propValue);
                    throw new ArgumentException(SR.GetString(SR.Unable_to_convert_type_to_string, type.ToString()), "type");
                case SettingsSerializeAs.Binary :
                    MemoryStream ms = new System.IO.MemoryStream();
                    try {
                        BinaryFormatter bf = new BinaryFormatter();
                        bf.Serialize(ms, propValue);
                        byte[] buffer = ms.ToArray();
                        return Convert.ToBase64String(buffer);
                    } finally {
                        ms.Close();
                    }

                case SettingsSerializeAs.Xml :
                    XmlSerializer xs = new XmlSerializer(type);
                    StringWriter sw = new StringWriter(CultureInfo.InvariantCulture);

                    xs.Serialize(sw, propValue);
                    return sw.ToString();
                }
            } catch (Exception) {
                if (throwOnError)
                    throw;
            }
            return null;
        }