Commit c54e5b56 authored by vosen's avatar vosen

Match constructor parameters properly.

parent 8da756e9
...@@ -1487,18 +1487,10 @@ static List<FieldInfo> GetSettableFields(Type t) ...@@ -1487,18 +1487,10 @@ static List<FieldInfo> GetSettableFields(Type t)
names.Add(reader.GetName(i)); names.Add(reader.GetName(i));
} }
var setters = (
from n in names
let prop = properties.FirstOrDefault(p => string.Equals(p.Name, n, StringComparison.Ordinal)) // property case sensitive first
?? properties.FirstOrDefault(p => string.Equals(p.Name, n, StringComparison.OrdinalIgnoreCase)) // property case insensitive second
let field = prop != null ? null : (fields.FirstOrDefault(p => string.Equals(p.Name, n, StringComparison.Ordinal)) // field case sensitive third
?? fields.FirstOrDefault(p => string.Equals(p.Name, n, StringComparison.OrdinalIgnoreCase))) // field case insensitive fourth
select new { Name = n, Property = prop, Field = field }
).ToList();
int index = startBound; int index = startBound;
ConstructorInfo specializedConstructor = null; ConstructorInfo specializedConstructor = null;
ParameterInfo[] specializedParameters = null;
if (type.IsValueType) if (type.IsValueType)
{ {
il.Emit(OpCodes.Ldloca_S, (byte)1); il.Emit(OpCodes.Ldloca_S, (byte)1);
...@@ -1531,6 +1523,7 @@ static List<FieldInfo> GetSettableFields(Type t) ...@@ -1531,6 +1523,7 @@ static List<FieldInfo> GetSettableFields(Type t)
if (i == ctorParameters.Length) if (i == ctorParameters.Length)
{ {
specializedConstructor = ctor; specializedConstructor = ctor;
specializedParameters = ctorParameters;
break; break;
} }
} }
...@@ -1555,6 +1548,17 @@ static List<FieldInfo> GetSettableFields(Type t) ...@@ -1555,6 +1548,17 @@ static List<FieldInfo> GetSettableFields(Type t)
il.Emit(OpCodes.Ldloc_1);// [target] il.Emit(OpCodes.Ldloc_1);// [target]
} }
var setters = specializedConstructor != null ?
names.Select((n, i) => new { Name = n, Property = new PropInfo() { Type = specializedParameters[i].ParameterType }, Field = (FieldInfo)null }).ToList()
:
(from n in names
let prop = properties.FirstOrDefault(p => string.Equals(p.Name, n, StringComparison.Ordinal)) // property case sensitive first
?? properties.FirstOrDefault(p => string.Equals(p.Name, n, StringComparison.OrdinalIgnoreCase)) // property case insensitive second
let field = prop != null ? null : (fields.FirstOrDefault(p => string.Equals(p.Name, n, StringComparison.Ordinal)) // field case sensitive third
?? fields.FirstOrDefault(p => string.Equals(p.Name, n, StringComparison.OrdinalIgnoreCase))) // field case insensitive fourth
select new { Name = n, Property = prop, Field = field }
).ToList();
// stack is now [target] // stack is now [target]
bool first = true; bool first = true;
...@@ -1679,13 +1683,12 @@ static List<FieldInfo> GetSettableFields(Type t) ...@@ -1679,13 +1683,12 @@ static List<FieldInfo> GetSettableFields(Type t)
if (specializedConstructor != null) if (specializedConstructor != null)
{ {
il.Emit(OpCodes.Pop); il.Emit(OpCodes.Pop);
Type itemType = item.Property != null ? item.Property.Type : item.Field.FieldType; if (item.Property.Type.IsValueType)
if (itemType.IsValueType)
{ {
il.DeclareLocal(itemType); il.DeclareLocal(item.Property.Type);
declareLocal++; declareLocal++;
il.Emit(OpCodes.Ldloca, (short)(declareLocal)); il.Emit(OpCodes.Ldloca, (short)(declareLocal));
il.Emit(OpCodes.Initobj, itemType); il.Emit(OpCodes.Initobj, item.Property.Type);
il.Emit(OpCodes.Ldloc, (short)(declareLocal)); il.Emit(OpCodes.Ldloc, (short)(declareLocal));
} }
else else
......
...@@ -35,13 +35,13 @@ public class ConcreteOrder : Order ...@@ -35,13 +35,13 @@ public class ConcreteOrder : Order
class NoDefaultConstructor class NoDefaultConstructor
{ {
public NoDefaultConstructor(int a, int? b, float f, ShortEnum e, ShortEnum? n) public NoDefaultConstructor(int a1, int? b1, float f1, ShortEnum e1, ShortEnum? n1)
{ {
A = a; A = a1;
B = b; B = b1;
F = f; F = f1;
E = e; E = e1;
N = n; N = n1;
} }
public int A { get; set; } public int A { get; set; }
public int? B { get; set; } public int? B { get; set; }
...@@ -52,7 +52,7 @@ public NoDefaultConstructor(int a, int? b, float f, ShortEnum e, ShortEnum? n) ...@@ -52,7 +52,7 @@ public NoDefaultConstructor(int a, int? b, float f, ShortEnum e, ShortEnum? n)
public void TestNoDefaultConstructor() public void TestNoDefaultConstructor()
{ {
NoDefaultConstructor nodef = connection.Query<NoDefaultConstructor>("select CAST(NULL AS integer) A, CAST(NULL AS integer) b, CAST(NULL AS real) f, cast(2 as smallint) E, cast(null as smallint) n").First(); NoDefaultConstructor nodef = connection.Query<NoDefaultConstructor>("select CAST(NULL AS integer) A1, CAST(NULL AS integer) b1, CAST(NULL AS real) f1, cast(2 as smallint) E1, cast(null as smallint) n1").First();
nodef.A.IsEqualTo(0); nodef.A.IsEqualTo(0);
nodef.B.IsEqualTo(null); nodef.B.IsEqualTo(null);
nodef.F.IsEqualTo(0); nodef.F.IsEqualTo(0);
...@@ -63,9 +63,10 @@ public void TestNoDefaultConstructor() ...@@ -63,9 +63,10 @@ public void TestNoDefaultConstructor()
class NoDefaultConstructorWithBinary class NoDefaultConstructorWithBinary
{ {
public System.Data.Linq.Binary Value { get; set; } public System.Data.Linq.Binary Value { get; set; }
public NoDefaultConstructorWithBinary(System.Data.Linq.Binary value) public int Ynt { get; set; }
public NoDefaultConstructorWithBinary(System.Data.Linq.Binary val)
{ {
Value = value; Value = val;
} }
} }
...@@ -74,7 +75,7 @@ public void TestNoDefaultConstructorBinary() ...@@ -74,7 +75,7 @@ public void TestNoDefaultConstructorBinary()
byte[] orig = new byte[20]; byte[] orig = new byte[20];
new Random(123456).NextBytes(orig); new Random(123456).NextBytes(orig);
var input = new System.Data.Linq.Binary(orig); var input = new System.Data.Linq.Binary(orig);
var output = connection.Query<NoDefaultConstructorWithBinary>("select @input as [value]", new { input }).First().Value; var output = connection.Query<NoDefaultConstructorWithBinary>("select @input as val", new { input }).First().Value;
output.ToArray().IsSequenceEqualTo(orig); output.ToArray().IsSequenceEqualTo(orig);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment