protected override object CreateInstance(Type actualType)
{
string message;
if (actualType.IsInterface)
{
// in the case of an interface pick a reasonable class that implements that interface
if (actualType == typeof(IEnumerable) || actualType == typeof(ICollection) || actualType == typeof(IList))
{
return new ArrayList();
}
}
else
{
if (actualType == typeof(ArrayList))
{
return new ArrayList();
}
else if (typeof(IEnumerable).IsAssignableFrom(actualType))
{
var instance = Activator.CreateInstance(actualType);
var list = instance as IList;
if (list == null)
{
message = string.Format("Enumerable class {0} does not implement IList so it can't be deserialized.", BsonUtils.GetFriendlyTypeName(actualType));
throw new BsonSerializationException(message);
}
return list;
}
}
message = string.Format("EnumerableSerializer can't be used with type {0}.", BsonUtils.GetFriendlyTypeName(actualType));
throw new BsonSerializationException(message);
}