-Fixed bad performance with dynamic

-Added support for creating collections with an IEnumerable<T> constructor
This commit is contained in:
James Newton-King
2013-03-11 17:20:15 +13:00
parent 92de187a74
commit 0e753888d0
10 changed files with 189 additions and 105 deletions
@@ -226,6 +226,32 @@ namespace Newtonsoft.Json.Utilities
else
list = null;
}
else if (ReflectionUtils.ImplementsGenericDefinition(listType, typeof (IEnumerable<>), out collectionType))
{
bool suitableConstructor = false;
foreach (ConstructorInfo constructor in listType.GetConstructors())
{
IList<ParameterInfo> parameters = constructor.GetParameters();
if (parameters.Count == 1)
{
if (collectionType.IsAssignableFrom(parameters[0].ParameterType))
{
suitableConstructor = true;
break;
}
}
}
if (!suitableConstructor)
throw new Exception("{0} does not have a public constructor that takes a type that implements {1}.".FormatWith(CultureInfo.InvariantCulture, listType, collectionType));
Type collectionContentsType = collectionType.GetGenericArguments()[0];
list = CreateGenericList(collectionContentsType);
isReadOnlyOrFixedSize = true;
}
else
{
list = null;