private object Deserialize()
{
object val = null;
//////////////////////////////////////////////
/// Step 1: Try creating from Serailized value
if (SerializedValue != null)
{
try {
if (SerializedValue is string) {
val = GetObjectFromString(Property.PropertyType, Property.SerializeAs, (string)SerializedValue);
} else {
MemoryStream ms = new System.IO.MemoryStream((byte[])SerializedValue);
try {
val = (new BinaryFormatter()).Deserialize(ms);
} finally {
ms.Close();
}
}
}
catch (Exception exception) {
}
if (val != null && !Property.PropertyType.IsAssignableFrom(val.GetType())) // is it the correct type
val = null;
}
//////////////////////////////////////////////
/// Step 2: Try creating from default value
if (val == null)
{
_UsingDefaultValue = true;
if (Property.DefaultValue == null || Property.DefaultValue.ToString() == "[null]") {
if (Property.PropertyType.IsValueType)
return Activator.CreateInstance(Property.PropertyType);
else
return null;
}
if (!(Property.DefaultValue is string)) {
val = Property.DefaultValue;
} else {
try {
val = GetObjectFromString(Property.PropertyType, Property.SerializeAs, (string)Property.DefaultValue);
} catch(Exception e) {
throw new ArgumentException(SR.GetString(SR.Could_not_create_from_default_value, Property.Name, e.Message));
}
}
if (val != null && !Property.PropertyType.IsAssignableFrom(val.GetType())) // is it the correct type
throw new ArgumentException(SR.GetString(SR.Could_not_create_from_default_value_2, Property.Name));
}
//////////////////////////////////////////////
/// Step 3: Create a new one by calling the parameterless constructor
if (val == null)
{
if (Property.PropertyType == typeof(string)) {
val = "";
} else {
try {
val = Activator.CreateInstance(Property.PropertyType);
} catch {}
}
}
return val;
}