提交 c54e5b56 编写于 作者: V vosen

Match constructor parameters properly.

上级 8da756e9
......@@ -1487,18 +1487,10 @@ static List<FieldInfo> GetSettableFields(Type t)
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;
ConstructorInfo specializedConstructor = null;
ParameterInfo[] specializedParameters = null;
if (type.IsValueType)
{
il.Emit(OpCodes.Ldloca_S, (byte)1);
......@@ -1531,6 +1523,7 @@ static List<FieldInfo> GetSettableFields(Type t)
if (i == ctorParameters.Length)
{
specializedConstructor = ctor;
specializedParameters = ctorParameters;
break;
}
}
......@@ -1555,6 +1548,17 @@ static List<FieldInfo> GetSettableFields(Type t)
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]
bool first = true;
......@@ -1679,13 +1683,12 @@ static List<FieldInfo> GetSettableFields(Type t)
if (specializedConstructor != null)
{
il.Emit(OpCodes.Pop);
Type itemType = item.Property != null ? item.Property.Type : item.Field.FieldType;
if (itemType.IsValueType)
if (item.Property.Type.IsValueType)
{
il.DeclareLocal(itemType);
il.DeclareLocal(item.Property.Type);
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));
}
else
......
......@@ -35,13 +35,13 @@ public class ConcreteOrder : Order
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;
B = b;
F = f;
E = e;
N = n;
A = a1;
B = b1;
F = f1;
E = e1;
N = n1;
}
public int A { get; set; }
public int? B { get; set; }
......@@ -52,7 +52,7 @@ public NoDefaultConstructor(int a, int? b, float f, ShortEnum e, ShortEnum? n)
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.B.IsEqualTo(null);
nodef.F.IsEqualTo(0);
......@@ -63,9 +63,10 @@ public void TestNoDefaultConstructor()
class NoDefaultConstructorWithBinary
{
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()
byte[] orig = new byte[20];
new Random(123456).NextBytes(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);
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册