- 优化 实体基类的属性位置,优先排在最前面; #164

- 整理 实体类 Ctor 有构造函数的映射处理;#164 [wiki](https://github.com/2881099/FreeSql/wiki/%e8%bf%94%e5%9b%9e%e6%95%b0%e6%8d%ae#dto-%E6%98%A0%E5%B0%84%E6%9F%A5%E8%AF%A2)
- 优化 实体属性,支持 protected set 属性;#164
This commit is contained in:
28810 2019-12-22 20:10:21 +08:00
parent 738eeb81a8
commit d5ed1c8a30
19 changed files with 226 additions and 175 deletions

View File

@ -110,13 +110,6 @@
清空状态数据
</summary>
</member>
<member name="M:FreeSql.DbSet`1.RemoveAsync(System.Linq.Expressions.Expression{System.Func{`0,System.Boolean}})">
<summary>
根据 lambda 条件删除数据
</summary>
<param name="predicate"></param>
<returns></returns>
</member>
<member name="M:FreeSql.DbSet`1.Add(`0)">
<summary>
添加

View File

@ -1056,6 +1056,23 @@ WHERE (((cast(a.`Id` as char)) in (SELECT b.`Title`
a.Id,
a.Clicks
});
Assert.Equal(@"SELECT a.`Id` as1, a.`Clicks` as2
FROM (SELECT * from (SELECT a.`Id` Id, a.`Clicks` Clicks
FROM `tb_topic_1` a) ftb
UNION ALL
SELECT * from (SELECT a.`Id` Id, a.`Clicks` Clicks
FROM `tb_topic_2` a) ftb) a
limit 0,20", select
.AsTable((type, old) => type == typeof(Topic) ? $"({sqlsss})" : null)
.Page(1, 20)
.ToSql(a => new
{
a.Id,
a.Clicks
}));
}
public class TestInclude_OneToManyModel1

View File

@ -17,11 +17,7 @@ namespace FreeSql.Tests.MySql
var sql = g.mysql.CodeFirst.GetComparisonDDLStatements<2>();
g.mysql.CodeFirst.SyncStructure<2>();
var item = new 2
{
= "测试标题",
= DateTime.Now
};
var item = 2.Create("测试标题", DateTime.Now);
Assert.Equal(1, g.mysql.Insert<2>().AppendData(item).ExecuteAffrows());
Assert.NotEqual(Guid.Empty, item.);
var item2 = g.mysql.Select<2>().Where(a => a. == item.).First();
@ -32,12 +28,17 @@ namespace FreeSql.Tests.MySql
class 2
{
[Column(IsPrimary = true)]
public Guid { get; set; }
public Guid { get; protected set; }
public string { get; set; }
public string { get; protected set; }
[Column(ServerTime = DateTimeKind.Local)]
public DateTime { get; set; }
public DateTime { get; protected set; }
public static 2 Create(string title, DateTime ctm)
{
return new 2 { = title, = ctm };
}
}
[Fact]

View File

@ -47,21 +47,35 @@ public static partial class FreeSqlGlobalExtensions
public static bool IsArrayOrList(this Type that) => that == null ? false : (that.IsArray || typeof(IList).IsAssignableFrom(that));
public static Type NullableTypeOrThis(this Type that) => that?.IsNullableType() == true ? that.GetGenericArguments().First() : that;
internal static string NotNullAndConcat(this string that, params object[] args) => string.IsNullOrEmpty(that) ? null : string.Concat(new object[] { that }.Concat(args));
static ConcurrentDictionary<Type, ParameterInfo[]> _dicGetDefaultValueFirstConstructorsParameters = new ConcurrentDictionary<Type, ParameterInfo[]>();
public static object CreateInstanceGetDefaultValue(this Type that)
{
if (that == null) return null;
if (that == typeof(string)) return default(string);
if (that.IsArray) return Array.CreateInstance(that, 0);
var ctorParms = _dicGetDefaultValueFirstConstructorsParameters.GetOrAdd(that, tp => tp.GetConstructors().FirstOrDefault()?.GetParameters());
var ctorParms = that.InternalGetTypeConstructor0OrFirst(false)?.GetParameters();
if (ctorParms == null || ctorParms.Any() == false) return Activator.CreateInstance(that, null);
return Activator.CreateInstance(that, ctorParms.Select(a => Activator.CreateInstance(a.ParameterType, null)).ToArray());
}
internal static NewExpression InternalNewExpression(this Type that)
{
var ctor = that.InternalGetTypeConstructor0OrFirst();
return Expression.New(ctor, ctor.GetParameters().Select(a => Expression.Constant(a.ParameterType.CreateInstanceGetDefaultValue(), a.ParameterType)));
}
static ConcurrentDictionary<Type, ConstructorInfo> _dicInternalGetTypeConstructor0OrFirst = new ConcurrentDictionary<Type, ConstructorInfo>();
internal static ConstructorInfo InternalGetTypeConstructor0OrFirst(this Type that, bool isThrow = true)
{
var ret = _dicInternalGetTypeConstructor0OrFirst.GetOrAdd(that, tp =>
tp.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new Type[0], null) ??
tp.GetConstructors(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).FirstOrDefault());
if (ret == null && isThrow) throw new ArgumentException($"{that.FullName} 类型无方法访问构造函数");
return ret;
}
static ConcurrentDictionary<Type, Dictionary<string, PropertyInfo>> _dicGetPropertiesDictIgnoreCase = new ConcurrentDictionary<Type, Dictionary<string, PropertyInfo>>();
public static Dictionary<string, PropertyInfo> GetPropertiesDictIgnoreCase(this Type that) => that == null ? null : _dicGetPropertiesDictIgnoreCase.GetOrAdd(that, tp =>
{
var props = that.GetProperties();
var props = that.GetProperties().GroupBy(p => p.DeclaringType).Reverse().SelectMany(p => p); //将基类的属性位置放在前面 #164
var dict = new Dictionary<string, PropertyInfo>(StringComparer.CurrentCultureIgnoreCase);
foreach (var prop in props)
{

View File

@ -52,7 +52,8 @@ namespace FreeSql.Internal
var constExpValue = constExp.Value?.ToString() ?? "NULL";
if (constExpValue == string.Empty) constExpValue = _common.FormatSql("{0}", "");
parent.DbField = constExpValue;
} else
}
else
parent.DbField = _common.FormatSql("{0}", constExp?.Value);
field.Append(", ").Append(parent.DbField);
if (index >= 0) field.Append(" as").Append(++index);
@ -82,8 +83,8 @@ namespace FreeSql.Internal
var map = new List<SelectColumnInfo>();
ExpressionSelectColumn_MemberAccess(_tables, map, SelectTableInfoType.From, exp, true, getSelectGroupingMapString);
var tb = parent.Table = map.First().Table.Table;
parent.Consturctor = tb.Type.GetConstructor(new Type[0]);
parent.ConsturctorType = ReadAnonymousTypeInfoConsturctorType.Properties;
parent.CsType = tb.Type;
parent.Consturctor = tb.Type.InternalGetTypeConstructor0OrFirst();
parent.IsEntity = true;
for (var idx = 0; idx < map.Count; idx++)
{
@ -113,10 +114,25 @@ namespace FreeSql.Internal
return false;
case ExpressionType.MemberInit:
var initExp = exp as MemberInitExpression;
parent.Consturctor = initExp.NewExpression.Type.GetConstructors()[0];
parent.ConsturctorType = ReadAnonymousTypeInfoConsturctorType.Properties;
if (isAllDtoMap && _tables != null && _tables.Any() && initExp.NewExpression.Type != _tables.FirstOrDefault().Table.Type)
parent.CsType = initExp.Type;
parent.Consturctor = initExp.NewExpression.Constructor;
if (initExp.NewExpression?.Arguments.Count > 0)
{
//处理构造参数
for (var a = 0; a < initExp.NewExpression.Arguments.Count; a++)
{
var child = new ReadAnonymousTypeInfo
{
Property = null,
CsName = initExp.NewExpression.Members[a].Name,
CsType = initExp.NewExpression.Arguments[a].Type,
MapType = initExp.NewExpression.Arguments[a].Type
};
parent.Childs.Add(child);
ReadAnonymousField(_tables, field, child, ref index, initExp.NewExpression.Arguments[a], getSelectGroupingMapString, whereCascadeExpression, false);
}
}
else if (isAllDtoMap && _tables != null && _tables.Any() && initExp.NewExpression.Type != _tables.FirstOrDefault().Table.Type)
{
//dto 映射
var dtoProps = initExp.NewExpression.Type.GetPropertiesDictIgnoreCase().Values;
@ -124,8 +140,9 @@ namespace FreeSql.Internal
{
foreach (var dtTb in _tables)
{
if (dtTb.Table.Columns.TryGetValue(dtoProp.Name, out var trydtocol))
{
if (dtTb.Table.Columns.TryGetValue(dtoProp.Name, out var trydtocol) == false) continue;
if (trydtocol.Attribute.IsIgnore == true) continue;
var child = new ReadAnonymousTypeInfo
{
Property = dtoProp,
@ -146,7 +163,6 @@ namespace FreeSql.Internal
}
}
}
}
if (initExp.Bindings?.Count > 0)
{
//指定 dto映射
@ -169,15 +185,27 @@ namespace FreeSql.Internal
return true;
case ExpressionType.New:
var newExp = exp as NewExpression;
parent.Consturctor = newExp.Type.GetConstructors()[0];
parent.ConsturctorType = ReadAnonymousTypeInfoConsturctorType.Arguments;
if (newExp.Members?.Count > 0)
parent.CsType = newExp.Type;
parent.Consturctor = newExp.Constructor;
if (newExp.Arguments?.Count > 0 &&
(
newExp.Type.IsAnonymousType() ||
newExp.Arguments.Any(a =>
{
for (var a = 0; a < newExp.Members.Count; a++)
if (a.NodeType != ExpressionType.Constant) return true;
var constVal = (a as ConstantExpression)?.Value;
if (constVal == null) return true;
if (object.Equals(constVal, a.Type.CreateInstanceGetDefaultValue()) == false) return true;
return false;
})
))
{
//处理构造参数
for (var a = 0; a < newExp.Arguments.Count; a++)
{
var child = new ReadAnonymousTypeInfo
{
Property = newExp.Type.GetProperty(newExp.Members[a].Name, BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.Instance),
Property = null,
CsName = newExp.Members[a].Name,
CsType = newExp.Arguments[a].Type,
MapType = newExp.Arguments[a].Type
@ -188,15 +216,16 @@ namespace FreeSql.Internal
}
else
{
parent.IsDefaultCtor = true;
//dto 映射
parent.ConsturctorType = ReadAnonymousTypeInfoConsturctorType.Properties;
var dtoProps2 = newExp.Type.GetPropertiesDictIgnoreCase().Values;
foreach (var dtoProp in dtoProps2)
{
foreach (var dtTb in _tables)
{
if (dtTb.Table.ColumnsByCs.TryGetValue(dtoProp.Name, out var trydtocol))
{
if (dtTb.Table.ColumnsByCs.TryGetValue(dtoProp.Name, out var trydtocol) == false) continue;
if (trydtocol.Attribute.IsIgnore == true) continue;
var child = new ReadAnonymousTypeInfo
{
Property = dtoProp,
@ -218,7 +247,6 @@ namespace FreeSql.Internal
}
}
if (parent.Childs.Any() == false) throw new Exception($"映射异常:{newExp.Type.Name} 没有一个属性名相同");
}
return true;
}
parent.DbField = $"({ExpressionLambdaToSql(exp, getTSC())})";
@ -247,21 +275,20 @@ namespace FreeSql.Internal
objval = Utils.GetDataReaderValue(parent.Property.PropertyType, objval);
return objval;
}
switch (parent.ConsturctorType)
var ctorParmsLength = 0;
object ret;
if (parent.IsDefaultCtor || parent.IsEntity || (ctorParmsLength = parent.Consturctor.GetParameters()?.Length ?? 0) == 0)
ret = parent.CsType?.CreateInstanceGetDefaultValue() ?? parent.Consturctor.Invoke(null);
else
{
case ReadAnonymousTypeInfoConsturctorType.Arguments:
var args = new object[parent.Childs.Count];
for (var a = 0; a < parent.Childs.Count; a++)
{
var objval = ReadAnonymous(parent.Childs[a], dr, ref index, notRead, null);
if (notRead == false)
args[a] = objval;
var ctorParms = new object[ctorParmsLength];
for (var c = 0; c < ctorParmsLength; c++)
ctorParms[c] = ReadAnonymous(parent.Childs[c], dr, ref index, notRead, null);
ret = parent.Consturctor.Invoke(ctorParms);
}
return parent.Consturctor.Invoke(args);
case ReadAnonymousTypeInfoConsturctorType.Properties:
var ret = parent.Consturctor.Invoke(null);
var isnull = notRead;
for (var b = 0; b < parent.Childs.Count; b++)
for (var b = ctorParmsLength; b < parent.Childs.Count; b++)
{
var prop = parent.Childs[b].Property;
var dbval = parent.IsEntity ? new ReadAnonymousDbValueRef() : null;
@ -273,8 +300,6 @@ namespace FreeSql.Internal
}
return isnull ? null : ret;
}
return null;
}
public class ReadAnonymousDbValueRef
{
public object DbValue { get; set; }

View File

@ -546,7 +546,6 @@ namespace FreeSql.Internal.CommonProvider
_commonExpression.ReadAnonymousField(_tables, field, map, ref index, newexp, null, _whereCascadeExpression, true);
return (map, field.Length > 0 ? field.Remove(0, 2).ToString() : null);
}
static ConcurrentDictionary<Type, ConstructorInfo> _dicConstructor = new ConcurrentDictionary<Type, ConstructorInfo>();
static ConcurrentDictionary<string, GetAllFieldExpressionTreeInfo> _dicGetAllFieldExpressionTree = new ConcurrentDictionary<string, GetAllFieldExpressionTreeInfo>();
public class GetAllFieldExpressionTreeInfo
{
@ -568,9 +567,8 @@ namespace FreeSql.Internal.CommonProvider
var readExpValue = Expression.MakeMemberAccess(readExp, Utils.RowInfo.PropertyValue);
var readExpDataIndex = Expression.MakeMemberAccess(readExp, Utils.RowInfo.PropertyDataIndex);
var blockExp = new List<Expression>();
var ctor = type.GetConstructor(new Type[0]) ?? type.GetConstructors().First();
blockExp.AddRange(new Expression[] {
Expression.Assign(retExp, Expression.New(ctor, ctor.GetParameters().Select(a => Expression.Default(a.ParameterType)))),
Expression.Assign(retExp, type.InternalNewExpression()),
Expression.Assign(dataIndexExp, Expression.Constant(0))
});
//typeof(Topic).GetMethod("get_Type").IsVirtual
@ -711,9 +709,8 @@ namespace FreeSql.Internal.CommonProvider
var readExpValue = Expression.MakeMemberAccess(readExp, Utils.RowInfo.PropertyValue);
var readExpDataIndex = Expression.MakeMemberAccess(readExp, Utils.RowInfo.PropertyDataIndex);
var blockExp = new List<Expression>();
var ctor = type.GetConstructor(new Type[0]) ?? type.GetConstructors().First();
blockExp.AddRange(new Expression[] {
Expression.Assign(retExp, Expression.New(ctor, ctor.GetParameters().Select(a => Expression.Default(a.ParameterType)))),
Expression.Assign(retExp, type.InternalNewExpression()),
Expression.Assign(dataIndexExp, Expression.Constant(0))
});
//typeof(Topic).GetMethod("get_Type").IsVirtual
@ -762,7 +759,7 @@ namespace FreeSql.Internal.CommonProvider
}
}
//只读到二级属性
var propGetSetMethod = prop.GetSetMethod();
var propGetSetMethod = prop.GetSetMethod(true);
Expression readExpAssign = null; //加速缓存
if (prop.PropertyType.IsArray) readExpAssign = Expression.New(Utils.RowInfo.Constructor,
Utils.GetDataReaderValueBlockExpression(prop.PropertyType, Expression.Call(rowExp, Utils.MethodDataReaderGetValue, dataIndexExp)),
@ -836,9 +833,9 @@ namespace FreeSql.Internal.CommonProvider
protected (ReadAnonymousTypeInfo map, string field) GetAllFieldReflection()
{
var tb1 = _tables.First().Table;
var type = tb1.Type;
var constructor = _dicConstructor.GetOrAdd(type, s => type.GetConstructor(new Type[0]));
var map = new ReadAnonymousTypeInfo { Consturctor = constructor, ConsturctorType = ReadAnonymousTypeInfoConsturctorType.Properties };
var type = tb1.TypeLazy ?? tb1.Type;
var constructor = type.InternalGetTypeConstructor0OrFirst();
var map = new ReadAnonymousTypeInfo { CsType = type, Consturctor = constructor, IsEntity = true };
var field = new StringBuilder();
var dicfield = new Dictionary<string, bool>();
@ -862,8 +859,9 @@ namespace FreeSql.Internal.CommonProvider
var tb2 = _tables.Where(a => a.Table.Type == p.PropertyType && a.Alias.Contains(p.Name)).FirstOrDefault();
if (tb2 == null && ps.Where(pw => pw.Value.PropertyType == p.PropertyType).Count() == 1) tb2 = _tables.Where(a => a.Table.Type == p.PropertyType).FirstOrDefault();
if (tb2 == null) continue;
child.Consturctor = tb2.Table.Type.GetConstructor(new Type[0]);
child.ConsturctorType = ReadAnonymousTypeInfoConsturctorType.Properties;
child.CsType = (tb2.Table.TypeLazy ?? tb2.Table.Type);
child.Consturctor = child.CsType.InternalGetTypeConstructor0OrFirst();
child.IsEntity = true;
foreach (var col2 in tb2.Table.Columns.Values)
{
if (index > 0) field.Append(", ");

View File

@ -101,8 +101,8 @@ namespace FreeSql.Internal.CommonProvider
List<TDto> ISelect<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>.ToList<TDto>() => (this as ISelect<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>).ToList(GetToListDtoSelector<TDto>());
Expression<Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, TDto>> GetToListDtoSelector<TDto>()
{
var ctor = typeof(TDto).GetConstructor(new Type[0]);
return Expression.Lambda<Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, TDto>>(Expression.New(ctor),
return Expression.Lambda<Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, TDto>>(
typeof(TDto).InternalNewExpression(),
_tables[0].Parameter ?? Expression.Parameter(typeof(T1), "a"),
Expression.Parameter(typeof(T2), "b"),
Expression.Parameter(typeof(T3), "c"),

View File

@ -172,8 +172,8 @@ namespace FreeSql.Internal.CommonProvider
public List<TDto> ToList<TDto>() => ToList(GetToListDtoSelector<TDto>());
Expression<Func<T1, TDto>> GetToListDtoSelector<TDto>()
{
var ctor = typeof(TDto).GetConstructor(new Type[0]);
return Expression.Lambda<Func<T1, TDto>>(Expression.New(ctor),
return Expression.Lambda<Func<T1, TDto>>(
typeof(TDto).InternalNewExpression(),
_tables[0].Parameter ?? Expression.Parameter(typeof(T1), "a"));
}
@ -884,8 +884,9 @@ namespace FreeSql.Internal.CommonProvider
{
var field = new StringBuilder();
var read = new ReadAnonymousTypeInfo();
read.ConsturctorType = ReadAnonymousTypeInfoConsturctorType.Properties;
read.Consturctor = (tbrefMid.TypeLazy ?? tbrefMid.Type).GetConstructor(new Type[0]);
read.CsType = (tbrefMid.TypeLazy ?? tbrefMid.Type);
read.Consturctor = read.CsType.InternalGetTypeConstructor0OrFirst();
read.IsEntity = true;
read.Table = tbrefMid;
foreach (var col in tbrefMid.Columns.Values)
{

View File

@ -86,8 +86,8 @@ namespace FreeSql.Internal.CommonProvider
List<TDto> ISelect<T1, T2>.ToList<TDto>() => (this as ISelect<T1, T2>).ToList(GetToListDtoSelector<TDto>());
Expression<Func<T1, T2, TDto>> GetToListDtoSelector<TDto>()
{
var ctor = typeof(TDto).GetConstructor(new Type[0]);
return Expression.Lambda<Func<T1, T2, TDto>>(Expression.New(ctor),
return Expression.Lambda<Func<T1, T2, TDto>>(
typeof(TDto).InternalNewExpression(),
_tables[0].Parameter ?? Expression.Parameter(typeof(T1), "a"),
Expression.Parameter(typeof(T2), "b"));
}

View File

@ -88,8 +88,8 @@ namespace FreeSql.Internal.CommonProvider
List<TDto> ISelect<T1, T2, T3>.ToList<TDto>() => (this as ISelect<T1, T2, T3>).ToList(GetToListDtoSelector<TDto>());
Expression<Func<T1, T2, T3, TDto>> GetToListDtoSelector<TDto>()
{
var ctor = typeof(TDto).GetConstructor(new Type[0]);
return Expression.Lambda<Func<T1, T2, T3, TDto>>(Expression.New(ctor),
return Expression.Lambda<Func<T1, T2, T3, TDto>>(
typeof(TDto).InternalNewExpression(),
_tables[0].Parameter ?? Expression.Parameter(typeof(T1), "a"),
Expression.Parameter(typeof(T2), "b"),
Expression.Parameter(typeof(T3), "c"));

View File

@ -90,8 +90,8 @@ namespace FreeSql.Internal.CommonProvider
List<TDto> ISelect<T1, T2, T3, T4>.ToList<TDto>() => (this as ISelect<T1, T2, T3, T4>).ToList(GetToListDtoSelector<TDto>());
Expression<Func<T1, T2, T3, T4, TDto>> GetToListDtoSelector<TDto>()
{
var ctor = typeof(TDto).GetConstructor(new Type[0]);
return Expression.Lambda<Func<T1, T2, T3, T4, TDto>>(Expression.New(ctor),
return Expression.Lambda<Func<T1, T2, T3, T4, TDto>>(
typeof(TDto).InternalNewExpression(),
_tables[0].Parameter ?? Expression.Parameter(typeof(T1), "a"),
Expression.Parameter(typeof(T2), "b"),
Expression.Parameter(typeof(T3), "c"),

View File

@ -92,8 +92,8 @@ namespace FreeSql.Internal.CommonProvider
List<TDto> ISelect<T1, T2, T3, T4, T5>.ToList<TDto>() => (this as ISelect<T1, T2, T3, T4, T5>).ToList(GetToListDtoSelector<TDto>());
Expression<Func<T1, T2, T3, T4, T5, TDto>> GetToListDtoSelector<TDto>()
{
var ctor = typeof(TDto).GetConstructor(new Type[0]);
return Expression.Lambda<Func<T1, T2, T3, T4, T5, TDto>>(Expression.New(ctor),
return Expression.Lambda<Func<T1, T2, T3, T4, T5, TDto>>(
typeof(TDto).InternalNewExpression(),
_tables[0].Parameter ?? Expression.Parameter(typeof(T1), "a"),
Expression.Parameter(typeof(T2), "b"),
Expression.Parameter(typeof(T3), "c"),

View File

@ -94,8 +94,8 @@ namespace FreeSql.Internal.CommonProvider
List<TDto> ISelect<T1, T2, T3, T4, T5, T6>.ToList<TDto>() => (this as ISelect<T1, T2, T3, T4, T5, T6>).ToList(GetToListDtoSelector<TDto>());
Expression<Func<T1, T2, T3, T4, T5, T6, TDto>> GetToListDtoSelector<TDto>()
{
var ctor = typeof(TDto).GetConstructor(new Type[0]);
return Expression.Lambda<Func<T1, T2, T3, T4, T5, T6, TDto>>(Expression.New(ctor),
return Expression.Lambda<Func<T1, T2, T3, T4, T5, T6, TDto>>(
typeof(TDto).InternalNewExpression(),
_tables[0].Parameter ?? Expression.Parameter(typeof(T1), "a"),
Expression.Parameter(typeof(T2), "b"),
Expression.Parameter(typeof(T3), "c"),

View File

@ -96,8 +96,8 @@ namespace FreeSql.Internal.CommonProvider
List<TDto> ISelect<T1, T2, T3, T4, T5, T6, T7>.ToList<TDto>() => (this as ISelect<T1, T2, T3, T4, T5, T6, T7>).ToList(GetToListDtoSelector<TDto>());
Expression<Func<T1, T2, T3, T4, T5, T6, T7, TDto>> GetToListDtoSelector<TDto>()
{
var ctor = typeof(TDto).GetConstructor(new Type[0]);
return Expression.Lambda<Func<T1, T2, T3, T4, T5, T6, T7, TDto>>(Expression.New(ctor),
return Expression.Lambda<Func<T1, T2, T3, T4, T5, T6, T7, TDto>>(
typeof(TDto).InternalNewExpression(),
_tables[0].Parameter ?? Expression.Parameter(typeof(T1), "a"),
Expression.Parameter(typeof(T2), "b"),
Expression.Parameter(typeof(T3), "c"),

View File

@ -99,8 +99,8 @@ namespace FreeSql.Internal.CommonProvider
Expression<Func<T1, T2, T3, T4, T5, T6, T7, T8, TDto>> GetToListDtoSelector<TDto>()
{
var ctor = typeof(TDto).GetConstructor(new Type[0]);
return Expression.Lambda<Func<T1, T2, T3, T4, T5, T6, T7, T8, TDto>>(Expression.New(ctor),
return Expression.Lambda<Func<T1, T2, T3, T4, T5, T6, T7, T8, TDto>>(
typeof(TDto).InternalNewExpression(),
_tables[0].Parameter ?? Expression.Parameter(typeof(T1), "a"),
Expression.Parameter(typeof(T2), "b"),
Expression.Parameter(typeof(T3), "c"),

View File

@ -99,8 +99,8 @@ namespace FreeSql.Internal.CommonProvider
List<TDto> ISelect<T1, T2, T3, T4, T5, T6, T7, T8, T9>.ToList<TDto>() => (this as ISelect<T1, T2, T3, T4, T5, T6, T7, T8, T9>).ToList(GetToListDtoSelector<TDto>());
Expression<Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, TDto>> GetToListDtoSelector<TDto>()
{
var ctor = typeof(TDto).GetConstructor(new Type[0]);
return Expression.Lambda<Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, TDto>>(Expression.New(ctor),
return Expression.Lambda<Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, TDto>>(
typeof(TDto).InternalNewExpression(),
_tables[0].Parameter ?? Expression.Parameter(typeof(T1), "a"),
Expression.Parameter(typeof(T2), "b"),
Expression.Parameter(typeof(T3), "c"),

View File

@ -13,10 +13,9 @@ namespace FreeSql.Internal.Model
public Type MapType { get; set; }
public string DbField { get; set; }
public ConstructorInfo Consturctor { get; set; }
public ReadAnonymousTypeInfoConsturctorType ConsturctorType { get; set; }
public List<ReadAnonymousTypeInfo> Childs = new List<ReadAnonymousTypeInfo>();
public TableInfo Table { get; set; }
public bool IsEntity { get; set; }
public bool IsDefaultCtor { get; set; }
}
public enum ReadAnonymousTypeInfoConsturctorType { Arguments, Properties }
}

View File

@ -72,7 +72,7 @@ namespace FreeSql.Internal
var columnsList = new List<ColumnInfo>();
foreach (var p in trytb.Properties.Values)
{
var setMethod = p.GetSetMethod(); //trytb.Type.GetMethod($"set_{p.Name}");
var setMethod = p.GetSetMethod(true); //trytb.Type.GetMethod($"set_{p.Name}");
var colattr = common.GetEntityColumnAttribute(entity, p);
var tp = common.CodeFirst.GetDbInfo(colattr?.MapType ?? p.PropertyType);
if (setMethod == null || (tp == null && p.PropertyType.IsValueType)) // 属性没有 set自动忽略
@ -1342,14 +1342,15 @@ namespace FreeSql.Internal
var readpkvalExp = Expression.Variable(typeof(object), "isnull3val");
var indexesLengthExp = Expression.Variable(typeof(int), "indexesLength");
var blockExp = new List<Expression>();
var ctor = type.GetConstructor(new Type[0]) ?? type.GetConstructors().First();
var ctorParms = ctor.GetParameters();
if (ctorParms.Length > 0)
var newExp = type.InternalNewExpression();
if (false && newExp.Arguments.Count > 0)
{
#region
/*
blockExp.AddRange(new Expression[] {
Expression.Assign(readpknullExp, Expression.Constant(false))
});
foreach (var ctorParm in ctorParms)
foreach (var ctorParm in newExp.Constructor.GetParameters())
{
if (typetb.ColumnsByCsIgnore.ContainsKey(ctorParm.Name)) continue;
var readType = typetb.ColumnsByCs.TryGetValue(ctorParm.Name, out var trycol) ? trycol.Attribute.MapType : ctorParm.ParameterType;
@ -1431,14 +1432,16 @@ namespace FreeSql.Internal
blockExp.Add(
Expression.IfThen(
Expression.IsFalse(readpknullExp),
Expression.Assign(retExp, Expression.New(ctor, readExpValueParms))
Expression.Assign(retExp, Expression.New(newExp.Constructor, readExpValueParms))
)
);
*/
#endregion
}
else
{
blockExp.AddRange(new Expression[] {
Expression.Assign(retExp, Expression.New(ctor)),
Expression.Assign(retExp, newExp),
Expression.Assign(indexesLengthExp, Expression.Constant(0)),
Expression.IfThen(
Expression.NotEqual(indexesExp, Expression.Constant(null)),
@ -1459,7 +1462,7 @@ namespace FreeSql.Internal
var readType = typetb.ColumnsByCs.TryGetValue(prop.Name, out var trycol) ? trycol.Attribute.MapType : prop.PropertyType;
var ispkExp = new List<Expression>();
var propGetSetMethod = prop.GetSetMethod();
var propGetSetMethod = prop.GetSetMethod(true);
Expression readVal = Expression.Assign(readpkvalExp, Expression.Call(rowExp, MethodDataReaderGetValue, tryidxExp));
Expression readExpAssign = null; //加速缓存
if (readType.IsArray) readExpAssign = Expression.New(RowInfo.Constructor,