Fix calling constructors with ref and in parameters (#1728)

This commit is contained in:
James Newton-King
2018-05-29 10:46:51 +12:00
committed by GitHub
parent 42ee730f3a
commit 6dd352c490
7 changed files with 245 additions and 2 deletions
@@ -1156,12 +1156,14 @@ namespace Newtonsoft.Json.Serialization
/// <returns>A <see cref="JsonContract"/> for the given type.</returns>
protected virtual JsonContract CreateContract(Type objectType)
{
if (IsJsonPrimitiveType(objectType))
Type t = ReflectionUtils.EnsureNotByRefType(objectType);
if (IsJsonPrimitiveType(t))
{
return CreatePrimitiveContract(objectType);
}
Type t = ReflectionUtils.EnsureNotNullableType(objectType);
t = ReflectionUtils.EnsureNotNullableType(t);
JsonContainerAttribute containerAttribute = JsonTypeReflector.GetCachedAttribute<JsonContainerAttribute>(t);
if (containerAttribute is JsonObjectAttribute)
@@ -238,7 +238,12 @@ namespace Newtonsoft.Json.Serialization
UnderlyingType = underlyingType;
// resolve ByRef types
// typically comes from in and ref parameters on methods/ctors
underlyingType = ReflectionUtils.EnsureNotByRefType(underlyingType);
IsNullable = ReflectionUtils.IsNullable(underlyingType);
NonNullableUnderlyingType = (IsNullable && ReflectionUtils.IsNullableType(underlyingType)) ? Nullable.GetUnderlyingType(underlyingType) : underlyingType;
CreatedType = NonNullableUnderlyingType;
@@ -275,6 +275,13 @@ namespace Newtonsoft.Json.Utilities
: t;
}
public static Type EnsureNotByRefType(Type t)
{
return (t.IsByRef && t.HasElementType)
? t.GetElementType()
: t;
}
public static bool IsGenericDefinition(Type type, Type genericInterfaceDefinition)
{
if (!type.IsGenericType())