Fix calling constructors with ref and in parameters (#1728)
This commit is contained in:
@@ -0,0 +1,131 @@
|
||||
#region License
|
||||
// Copyright (c) 2007 James Newton-King
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person
|
||||
// obtaining a copy of this software and associated documentation
|
||||
// files (the "Software"), to deal in the Software without
|
||||
// restriction, including without limitation the rights to use,
|
||||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the
|
||||
// Software is furnished to do so, subject to the following
|
||||
// conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
// OTHER DEALINGS IN THE SOFTWARE.
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using System.Reflection.Emit;
|
||||
using System.Runtime.Serialization;
|
||||
using Newtonsoft.Json.Converters;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
using Newtonsoft.Json.Utilities;
|
||||
#if DNXCORE50
|
||||
using Xunit;
|
||||
using Test = Xunit.FactAttribute;
|
||||
using Assert = Newtonsoft.Json.Tests.XUnitAssert;
|
||||
#else
|
||||
using NUnit.Framework;
|
||||
#endif
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Issues
|
||||
{
|
||||
[TestFixture]
|
||||
public class Issue1725 : TestFixtureBase
|
||||
{
|
||||
[Test]
|
||||
public void Test_In()
|
||||
{
|
||||
var p1 = new InPerson("some name");
|
||||
string json = JsonConvert.SerializeObject(p1);
|
||||
|
||||
var p2 = JsonConvert.DeserializeObject<InPerson>(json);
|
||||
Assert.AreEqual("some name", p2.Name);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test_Ref()
|
||||
{
|
||||
string value = "some name";
|
||||
var p1 = new RefPerson(ref value);
|
||||
string json = JsonConvert.SerializeObject(p1);
|
||||
|
||||
var p2 = JsonConvert.DeserializeObject<RefPerson>(json);
|
||||
Assert.AreEqual("some name", p2.Name);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test_InNullable()
|
||||
{
|
||||
var p1 = new InNullablePerson(1);
|
||||
string json = JsonConvert.SerializeObject(p1);
|
||||
|
||||
var p2 = JsonConvert.DeserializeObject<InNullablePerson>(json);
|
||||
Assert.AreEqual(1, p2.Age);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test_RefNullable()
|
||||
{
|
||||
int? value = 1;
|
||||
var p1 = new RefNullablePerson(ref value);
|
||||
string json = JsonConvert.SerializeObject(p1);
|
||||
|
||||
var p2 = JsonConvert.DeserializeObject<RefNullablePerson>(json);
|
||||
Assert.AreEqual(1, p2.Age);
|
||||
}
|
||||
|
||||
public class InPerson
|
||||
{
|
||||
public InPerson(in string name)
|
||||
{
|
||||
Name = name;
|
||||
}
|
||||
|
||||
public string Name { get; }
|
||||
}
|
||||
|
||||
public class RefPerson
|
||||
{
|
||||
public RefPerson(ref string name)
|
||||
{
|
||||
Name = name;
|
||||
}
|
||||
|
||||
public string Name { get; }
|
||||
}
|
||||
|
||||
public class InNullablePerson
|
||||
{
|
||||
public InNullablePerson(in int? age)
|
||||
{
|
||||
Age = age;
|
||||
}
|
||||
|
||||
public int? Age { get; }
|
||||
}
|
||||
|
||||
public class RefNullablePerson
|
||||
{
|
||||
public RefNullablePerson(ref int? age)
|
||||
{
|
||||
Age = age;
|
||||
}
|
||||
|
||||
public int? Age { get; }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -50,6 +50,33 @@ namespace Newtonsoft.Json.Tests.Utilities
|
||||
[TestFixture]
|
||||
public class DynamicReflectionDelegateFactoryTests : TestFixtureBase
|
||||
{
|
||||
[Test]
|
||||
public void ConstructorWithInString()
|
||||
{
|
||||
ConstructorInfo constructor = TestReflectionUtils.GetConstructors(typeof(InTestClass)).Single(c => c.GetParameters().Count() == 1);
|
||||
|
||||
var creator = DynamicReflectionDelegateFactory.Instance.CreateParameterizedConstructor(constructor);
|
||||
|
||||
object[] args = new object[] { "Value" };
|
||||
InTestClass o = (InTestClass)creator(args);
|
||||
Assert.IsNotNull(o);
|
||||
Assert.AreEqual("Value", o.Value);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ConstructorWithInStringAndBool()
|
||||
{
|
||||
ConstructorInfo constructor = TestReflectionUtils.GetConstructors(typeof(InTestClass)).Single(c => c.GetParameters().Count() == 2);
|
||||
|
||||
var creator = DynamicReflectionDelegateFactory.Instance.CreateParameterizedConstructor(constructor);
|
||||
|
||||
object[] args = new object[] { "Value", true };
|
||||
InTestClass o = (InTestClass)creator(args);
|
||||
Assert.IsNotNull(o);
|
||||
Assert.AreEqual("Value", o.Value);
|
||||
Assert.AreEqual(true, o.B1);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ConstructorWithRefString()
|
||||
{
|
||||
|
||||
@@ -47,6 +47,33 @@ namespace Newtonsoft.Json.Tests.Utilities
|
||||
[TestFixture]
|
||||
public class ExpressionReflectionDelegateFactoryTests : TestFixtureBase
|
||||
{
|
||||
[Test]
|
||||
public void ConstructorWithInString()
|
||||
{
|
||||
ConstructorInfo constructor = TestReflectionUtils.GetConstructors(typeof(InTestClass)).Single(c => c.GetParameters().Count() == 1);
|
||||
|
||||
var creator = ExpressionReflectionDelegateFactory.Instance.CreateParameterizedConstructor(constructor);
|
||||
|
||||
object[] args = new object[] { "Value" };
|
||||
InTestClass o = (InTestClass)creator(args);
|
||||
Assert.IsNotNull(o);
|
||||
Assert.AreEqual("Value", o.Value);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ConstructorWithInStringAndBool()
|
||||
{
|
||||
ConstructorInfo constructor = TestReflectionUtils.GetConstructors(typeof(InTestClass)).Single(c => c.GetParameters().Count() == 2);
|
||||
|
||||
var creator = ExpressionReflectionDelegateFactory.Instance.CreateParameterizedConstructor(constructor);
|
||||
|
||||
object[] args = new object[] { "Value", true };
|
||||
InTestClass o = (InTestClass)creator(args);
|
||||
Assert.IsNotNull(o);
|
||||
Assert.AreEqual("Value", o.Value);
|
||||
Assert.AreEqual(true, o.B1);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ConstructorWithRefString()
|
||||
{
|
||||
|
||||
@@ -44,9 +44,53 @@ namespace Newtonsoft.Json.Tests.Utilities
|
||||
}
|
||||
}
|
||||
|
||||
public class InTestClass
|
||||
{
|
||||
public string Value { get; }
|
||||
public bool B1 { get; }
|
||||
|
||||
public InTestClass(in string value)
|
||||
{
|
||||
Value = value;
|
||||
}
|
||||
|
||||
public InTestClass(in string value, in bool b1)
|
||||
: this(in value)
|
||||
{
|
||||
B1 = b1;
|
||||
}
|
||||
}
|
||||
|
||||
[TestFixture]
|
||||
public class LateboundReflectionDelegateFactoryTests : TestFixtureBase
|
||||
{
|
||||
[Test]
|
||||
public void ConstructorWithInString()
|
||||
{
|
||||
ConstructorInfo constructor = TestReflectionUtils.GetConstructors(typeof(InTestClass)).Single(c => c.GetParameters().Count() == 1);
|
||||
|
||||
var creator = LateBoundReflectionDelegateFactory.Instance.CreateParameterizedConstructor(constructor);
|
||||
|
||||
object[] args = new object[] { "Value" };
|
||||
InTestClass o = (InTestClass)creator(args);
|
||||
Assert.IsNotNull(o);
|
||||
Assert.AreEqual("Value", o.Value);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ConstructorWithInStringAndBool()
|
||||
{
|
||||
ConstructorInfo constructor = TestReflectionUtils.GetConstructors(typeof(InTestClass)).Single(c => c.GetParameters().Count() == 2);
|
||||
|
||||
var creator = LateBoundReflectionDelegateFactory.Instance.CreateParameterizedConstructor(constructor);
|
||||
|
||||
object[] args = new object[] { "Value", true };
|
||||
InTestClass o = (InTestClass)creator(args);
|
||||
Assert.IsNotNull(o);
|
||||
Assert.AreEqual("Value", o.Value);
|
||||
Assert.AreEqual(true, o.B1);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ConstructorWithRefString()
|
||||
{
|
||||
|
||||
@@ -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())
|
||||
|
||||
Reference in New Issue
Block a user