-Fixed DynamicReflectionDelegateFactory creating typed value type constructors
This commit is contained in:
@@ -193,6 +193,29 @@ namespace Newtonsoft.Json.Tests.Utilities
|
|||||||
|
|
||||||
Assert.AreEqual("Name!", (string)result);
|
Assert.AreEqual("Name!", (string)result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void ConstructorStruct()
|
||||||
|
{
|
||||||
|
Func<object> creator1 = DynamicReflectionDelegateFactory.Instance.CreateDefaultConstructor<object>(typeof(MyStruct));
|
||||||
|
MyStruct myStruct1 = (MyStruct)creator1.Invoke();
|
||||||
|
Assert.AreEqual(0, myStruct1.IntProperty);
|
||||||
|
|
||||||
|
Func<MyStruct> creator2 = DynamicReflectionDelegateFactory.Instance.CreateDefaultConstructor<MyStruct>(typeof(MyStruct));
|
||||||
|
MyStruct myStruct2 = creator2.Invoke();
|
||||||
|
Assert.AreEqual(0, myStruct2.IntProperty);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public struct MyStruct
|
||||||
|
{
|
||||||
|
private int _intProperty;
|
||||||
|
|
||||||
|
public int IntProperty
|
||||||
|
{
|
||||||
|
get { return _intProperty; }
|
||||||
|
set { _intProperty = value; }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -362,6 +362,18 @@ namespace Newtonsoft.Json.Tests.Utilities
|
|||||||
JsonSerializerTest.DictionaryKey key = (JsonSerializerTest.DictionaryKey)result;
|
JsonSerializerTest.DictionaryKey key = (JsonSerializerTest.DictionaryKey)result;
|
||||||
Assert.AreEqual("First!", key.Value);
|
Assert.AreEqual("First!", key.Value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void ConstructorStruct()
|
||||||
|
{
|
||||||
|
Func<object> creator1 = ExpressionReflectionDelegateFactory.Instance.CreateDefaultConstructor<object>(typeof(MyStruct));
|
||||||
|
MyStruct myStruct1 = (MyStruct)creator1.Invoke();
|
||||||
|
Assert.AreEqual(0, myStruct1.IntProperty);
|
||||||
|
|
||||||
|
Func<MyStruct> creator2 = ExpressionReflectionDelegateFactory.Instance.CreateDefaultConstructor<MyStruct>(typeof(MyStruct));
|
||||||
|
MyStruct myStruct2 = creator2.Invoke();
|
||||||
|
Assert.AreEqual(0, myStruct2.IntProperty);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -208,24 +208,28 @@ namespace Newtonsoft.Json.Utilities
|
|||||||
dynamicMethod.InitLocals = true;
|
dynamicMethod.InitLocals = true;
|
||||||
ILGenerator generator = dynamicMethod.GetILGenerator();
|
ILGenerator generator = dynamicMethod.GetILGenerator();
|
||||||
|
|
||||||
GenerateCreateDefaultConstructorIL(type, generator);
|
GenerateCreateDefaultConstructorIL(type, generator, typeof(T));
|
||||||
|
|
||||||
return (Func<T>)dynamicMethod.CreateDelegate(typeof(Func<T>));
|
return (Func<T>)dynamicMethod.CreateDelegate(typeof(Func<T>));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void GenerateCreateDefaultConstructorIL(Type type, ILGenerator generator)
|
private void GenerateCreateDefaultConstructorIL(Type type, ILGenerator generator, Type delegateType)
|
||||||
{
|
{
|
||||||
if (type.IsValueType())
|
if (type.IsValueType())
|
||||||
{
|
{
|
||||||
generator.DeclareLocal(type);
|
generator.DeclareLocal(type);
|
||||||
generator.Emit(OpCodes.Ldloc_0);
|
generator.Emit(OpCodes.Ldloc_0);
|
||||||
generator.Emit(OpCodes.Box, type);
|
|
||||||
|
// only need to box if the delegate isn't returning the value type
|
||||||
|
if (type != delegateType)
|
||||||
|
{
|
||||||
|
generator.Emit(OpCodes.Box, type);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
ConstructorInfo constructorInfo =
|
ConstructorInfo constructorInfo =
|
||||||
type.GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null,
|
type.GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, ReflectionUtils.EmptyTypes, null);
|
||||||
ReflectionUtils.EmptyTypes, null);
|
|
||||||
|
|
||||||
if (constructorInfo == null)
|
if (constructorInfo == null)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user