private static object GetObjectFromString(Type type, SettingsSerializeAs serializeAs, string attValue)
{
// Deal with string types
if (type == typeof(string) && (attValue == null || attValue.Length < 1 || serializeAs == SettingsSerializeAs.String))
return attValue;
// Return null if there is nothing to convert
if (attValue == null || attValue.Length < 1)
return null;
// Convert based on the serialized type
switch (serializeAs)
{
case SettingsSerializeAs.Binary:
byte[] buf = Convert.FromBase64String(attValue);
MemoryStream ms = null;
try {
ms = new System.IO.MemoryStream(buf);
return (new BinaryFormatter()).Deserialize(ms);
} finally {
if (ms != null)
ms.Close();
}
case SettingsSerializeAs.Xml:
StringReader sr = new StringReader(attValue);
XmlSerializer xs = new XmlSerializer(type);
return xs.Deserialize(sr);
case SettingsSerializeAs.String:
TypeConverter converter = TypeDescriptor.GetConverter(type);
if (converter != null && converter.CanConvertTo(typeof(String)) && converter.CanConvertFrom(typeof(String)))
return converter.ConvertFromInvariantString(attValue);
throw new ArgumentException(SR.GetString(SR.Unable_to_convert_type_from_string, type.ToString()), "type");
default:
return null;
}
}