- 优化 指定 Dto 查询对 c# 字段的支持;

This commit is contained in:
2881099
2021-01-12 12:40:07 +08:00
parent 1e6c6fc9a8
commit fcfeacdc95
3 changed files with 45 additions and 3 deletions

View File

@ -280,6 +280,7 @@ namespace FreeSql.Internal
CsType = initAssignExp.Expression.Type,
MapType = initAssignExp.Expression.Type
};
if (child.Property == null) child.ReflectionField = initExp.Type.GetField(initExp.Bindings[a].Member.Name, BindingFlags.Public | BindingFlags.Instance);
parent.Childs.Add(child);
ReadAnonymousField(_tables, field, child, ref index, initAssignExp.Expression, select, diymemexp, whereGlobalFilter, findIncludeMany, false);
}
@ -399,13 +400,17 @@ namespace FreeSql.Internal
var isnull = notRead;
for (var b = ctorParmsLength; b < parent.Childs.Count; b++)
{
var prop = parent.Childs[b].Property;
var dbval = parent.IsEntity ? new ReadAnonymousDbValueRef() : null;
var objval = ReadAnonymous(parent.Childs[b], dr, ref index, notRead, dbval, rowIndex, fillIncludeMany);
if (isnull == false && parent.IsEntity && dbval.DbValue == null && parent.Table != null && parent.Table.ColumnsByCs.TryGetValue(parent.Childs[b].CsName, out var trycol) && trycol.Attribute.IsPrimary)
isnull = true;
if (isnull == false && prop.CanWrite)
prop.SetValue(ret, objval, null);
if (isnull == false)
{
var prop = parent.Childs[b].Property;
if (prop?.CanWrite == true) prop.SetValue(ret, objval, null);
else if (prop == null) parent.Childs[b].ReflectionField?.SetValue(ret, objval);
}
}
return isnull ? null : ret;
}

View File

@ -9,6 +9,7 @@ namespace FreeSql.Internal.Model
public class ReadAnonymousTypeInfo
{
public PropertyInfo Property { get; set; }
public FieldInfo ReflectionField { get; set; }
public string CsName { get; set; }
public Type CsType { get; set; }
public Type MapType { get; set; }
@ -23,6 +24,7 @@ namespace FreeSql.Internal.Model
public void CopyTo(ReadAnonymousTypeInfo target)
{
target.Property = Property;
target.ReflectionField = ReflectionField;
target.CsName = CsName;
target.CsType = CsType;
target.MapType = MapType;