- 优化 实体类重写属性 new 如果类型与基类不一致,无法使用的问题;

This commit is contained in:
28810
2019-11-10 12:18:16 +08:00
parent 5ce037d316
commit 8a9a50ecb7
8 changed files with 48 additions and 20 deletions

View File

@ -23,7 +23,6 @@ namespace FreeSql.Internal
_common = common;
}
static ConcurrentDictionary<Type, PropertyInfo[]> _dicReadAnonymousFieldDtoPropertys = new ConcurrentDictionary<Type, PropertyInfo[]>();
public bool ReadAnonymousField(List<SelectTableInfo> _tables, StringBuilder field, ReadAnonymousTypeInfo parent, ref int index, Expression exp, Func<Expression[], string> getSelectGroupingMapString, List<LambdaExpression> whereCascadeExpression)
{
Func<ExpTSC> getTSC = () => new ExpTSC { _tables = _tables, getSelectGroupingMapString = getSelectGroupingMapString, tbtype = SelectTableInfoType.From, isQuoteName = true, isDisableDiyParse = false, style = ExpressionStyle.Where, whereCascadeExpression = whereCascadeExpression };
@ -110,7 +109,7 @@ namespace FreeSql.Internal
parent.ConsturctorType = ReadAnonymousTypeInfoConsturctorType.Properties;
//dto 映射
var dtoProps = _dicReadAnonymousFieldDtoPropertys.GetOrAdd(initExp.NewExpression.Type, dtoType => dtoType.GetProperties());
var dtoProps = initExp.NewExpression.Type.GetPropertiesDictIgnoreCase().Values;
foreach (var dtoProp in dtoProps)
{
foreach (var dtTb in _tables)
@ -180,7 +179,7 @@ namespace FreeSql.Internal
{
//dto 映射
parent.ConsturctorType = ReadAnonymousTypeInfoConsturctorType.Properties;
var dtoProps2 = _dicReadAnonymousFieldDtoPropertys.GetOrAdd(newExp.Type, dtoType => dtoType.GetProperties());
var dtoProps2 = newExp.Type.GetPropertiesDictIgnoreCase().Values;
foreach (var dtoProp in dtoProps2)
{
foreach (var dtTb in _tables)

View File

@ -79,11 +79,10 @@ namespace FreeSql.Internal.CommonProvider
if (isThrowException) throw e;
}
internal static ConcurrentDictionary<Type, Dictionary<string, PropertyInfo>> dicQueryTypeGetProperties = new ConcurrentDictionary<Type, Dictionary<string, PropertyInfo>>();
internal Dictionary<string, PropertyInfo> GetQueryTypeProperties(Type type)
{
var tb = _util.GetTableByEntity(type);
var props = tb?.Properties ?? dicQueryTypeGetProperties.GetOrAdd(type, k => type.GetProperties().ToDictionary(a => a.Name, a => a, StringComparer.CurrentCultureIgnoreCase));
var props = tb?.Properties ?? type.GetPropertiesDictIgnoreCase();
return props;
}
public List<T> Query<T>(string cmdText, object parms = null) => Query<T>(null, null, CommandType.Text, cmdText, GetDbParamtersByObject(cmdText, parms));

View File

@ -262,7 +262,7 @@ namespace FreeSql.Internal
else
{
var sb = new StringBuilder();
var ps = type.GetProperties();
var ps = type.GetPropertiesDictIgnoreCase().Values;
var psidx = 0;
foreach (var p in ps)
{
@ -360,7 +360,7 @@ namespace FreeSql.Internal
}
var xmlNav = xpath.CreateNavigator();
var props = type.GetProperties();
var props = type.GetPropertiesDictIgnoreCase().Values;
foreach (var prop in props)
{
var className = (prop.DeclaringType.IsNested ? $"{prop.DeclaringType.Namespace}.{prop.DeclaringType.DeclaringType.Name}.{prop.DeclaringType.Name}" : $"{prop.DeclaringType.Namespace}.{prop.DeclaringType.Name}").Trim('.');

View File

@ -51,7 +51,7 @@ namespace FreeSql.Internal
var tbattr = common.GetEntityTableAttribute(entity);
trytb = new TableInfo();
trytb.Type = entity;
trytb.Properties = entity.GetProperties().ToDictionary(a => a.Name, a => a, StringComparer.CurrentCultureIgnoreCase);
trytb.Properties = entity.GetPropertiesDictIgnoreCase();
trytb.CsName = entity.Name;
trytb.DbName = (tbattr?.Name ?? entity.Name);
trytb.DbOldName = tbattr?.OldName;
@ -72,7 +72,7 @@ namespace FreeSql.Internal
var columnsList = new List<ColumnInfo>();
foreach (var p in trytb.Properties.Values)
{
var setMethod = trytb.Type.GetMethod($"set_{p.Name}");
var setMethod = p.GetSetMethod(); //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自动忽略
@ -391,7 +391,7 @@ namespace FreeSql.Internal
{
if (midType != null)
{
var midTypeProps = midType.GetProperties();
var midTypeProps = midType.GetPropertiesDictIgnoreCase().Values;
var midTypePropsTrytb = midTypeProps.Where(a => a.PropertyType == trytb.Type).Count();
var midTypePropsTbref = midTypeProps.Where(a => a.PropertyType == tbref.Type).Count();
if (midTypePropsTrytb != 1 || midTypePropsTbref != 1) midType = null;
@ -1024,7 +1024,7 @@ namespace FreeSql.Internal
var type = obj.GetType();
if (type == ttype) return new[] { (T)Convert.ChangeType(obj, type) };
var ret = new List<T>();
var ps = type.GetProperties();
var ps = type.GetPropertiesDictIgnoreCase().Values;
foreach (var p in ps)
{
if (string.IsNullOrEmpty(paramPrefix) == false && sql.IndexOf($"{paramPrefix}{p.Name}", StringComparison.CurrentCultureIgnoreCase) == -1) continue;
@ -1346,7 +1346,7 @@ namespace FreeSql.Internal
Expression.Assign(readpknullExp, Expression.Constant(false))
});
var props = type.GetProperties();//.ToDictionary(a => a.Name, a => a, StringComparer.CurrentCultureIgnoreCase);
var props = type.GetPropertiesDictIgnoreCase().Values;
var propIndex = 0;
foreach (var prop in props)
{