From ec7dec161af9497baf2d79764b47ad3a58830660 Mon Sep 17 00:00:00 2001 From: 28810 <28810@YEXIANGQIN> Date: Sat, 21 Mar 2020 19:19:53 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E5=A2=9E=E5=8A=A0=20=E5=86=85=E9=83=A8?= =?UTF-8?q?=E6=96=B9=E6=B3=95=20DisplayCsharp=EF=BC=8C=E7=BB=9F=E4=B8=80?= =?UTF-8?q?=20LazyLoading=20=E5=8A=A8=E6=80=81=E4=BB=A3=E7=A0=81=E7=B1=BB?= =?UTF-8?q?=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FreeSql.DbContext/FreeSql.DbContext.xml | 7 --- FreeSql/Extensions/FreeSqlGlobalExtensions.cs | 53 +++++++++++++++++-- FreeSql/FreeSql.xml | 7 +++ FreeSql/Internal/UtilsExpressionTree.cs | 18 +++---- 4 files changed, 65 insertions(+), 20 deletions(-) diff --git a/FreeSql.DbContext/FreeSql.DbContext.xml b/FreeSql.DbContext/FreeSql.DbContext.xml index ca7c357b..2735cefb 100644 --- a/FreeSql.DbContext/FreeSql.DbContext.xml +++ b/FreeSql.DbContext/FreeSql.DbContext.xml @@ -110,13 +110,6 @@ 清空状态数据 - - - 根据 lambda 条件删除数据 - - - - 添加 diff --git a/FreeSql/Extensions/FreeSqlGlobalExtensions.cs b/FreeSql/Extensions/FreeSqlGlobalExtensions.cs index 9ea49e7f..5aded13e 100644 --- a/FreeSql/Extensions/FreeSqlGlobalExtensions.cs +++ b/FreeSql/Extensions/FreeSqlGlobalExtensions.cs @@ -16,7 +16,7 @@ using System.Threading; public static partial class FreeSqlGlobalExtensions { - static Lazy> dicIsNumberType = new Lazy>(() => new Dictionary + static Lazy> _dicIsNumberType = new Lazy>(() => new Dictionary { [typeof(sbyte)] = true, [typeof(sbyte?)] = true, @@ -41,13 +41,60 @@ public static partial class FreeSqlGlobalExtensions [typeof(decimal)] = false, [typeof(decimal?)] = false }); - public static bool IsIntegerType(this Type that) => that == null ? false : (dicIsNumberType.Value.TryGetValue(that, out var tryval) ? tryval : false); - public static bool IsNumberType(this Type that) => that == null ? false : dicIsNumberType.Value.ContainsKey(that); + public static bool IsIntegerType(this Type that) => that == null ? false : (_dicIsNumberType.Value.TryGetValue(that, out var tryval) ? tryval : false); + public static bool IsNumberType(this Type that) => that == null ? false : _dicIsNumberType.Value.ContainsKey(that); public static bool IsNullableType(this Type that) => that.IsArray == false && that?.FullName.StartsWith("System.Nullable`1[") == true; public static bool IsAnonymousType(this Type that) => that?.FullName.StartsWith("<>f__AnonymousType") == true; 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)); + /// + /// 获取 Type 的原始 c# 文本表示 + /// + /// + /// + public static string DisplayCsharp(this Type that) + { + return DisplayCsharp(that, true); + } + static string DisplayCsharp(this Type that, bool isNameSpace) + { + if (that == typeof(void)) return "void"; + if (that.IsGenericParameter) return that.Name; + var sb = new StringBuilder(); + var nestedType = that; + while (nestedType.IsNested) + { + sb.Insert(0, ".").Insert(0, DisplayCsharp(nestedType.DeclaringType, false)); + nestedType = nestedType.DeclaringType; + } + if (isNameSpace && string.IsNullOrEmpty(nestedType.Namespace) == false) + sb.Insert(0, ".").Insert(0, nestedType.Namespace); + + if (that.IsGenericType == false) + return sb.Append(that.Name).ToString(); + + var genericParameters = that.GetGenericArguments(); + if (that.IsNested && that.DeclaringType.IsGenericType) + { + var dic = genericParameters.ToDictionary(a => a.Name); + foreach (var nestedGenericParameter in that.DeclaringType.GetGenericArguments()) + if (dic.ContainsKey(nestedGenericParameter.Name)) + dic.Remove(nestedGenericParameter.Name); + genericParameters = dic.Values.ToArray(); + } + if (genericParameters.Any() == false) + return sb.Append(that.Name).ToString(); + + sb.Append(that.Name.Remove(that.Name.IndexOf('`'))).Append("<"); + var genericTypeIndex = 0; + foreach (var genericType in genericParameters) + { + if (genericTypeIndex++ > 0) sb.Append(", "); + sb.Append(DisplayCsharp(genericType, true)); + } + return sb.Append(">").ToString(); + } public static object CreateInstanceGetDefaultValue(this Type that) { if (that == null) return null; diff --git a/FreeSql/FreeSql.xml b/FreeSql/FreeSql.xml index f312638c..cf900d20 100644 --- a/FreeSql/FreeSql.xml +++ b/FreeSql/FreeSql.xml @@ -3194,6 +3194,13 @@ + + + 获取 Type 的原始 c# 文本表示 + + + + 测量两个经纬度的距离,返回单位:米 diff --git a/FreeSql/Internal/UtilsExpressionTree.cs b/FreeSql/Internal/UtilsExpressionTree.cs index 1a65014b..cf78e9a3 100644 --- a/FreeSql/Internal/UtilsExpressionTree.cs +++ b/FreeSql/Internal/UtilsExpressionTree.cs @@ -397,7 +397,7 @@ namespace FreeSql.Internal tbc.AddOrUpdate(entity, trytb, (oldkey, oldval) => trytb); #region 查找导航属性的关系、virtual 属性延时加载,动态产生新的重写类 - var trytbTypeName = trytb.Type.IsNested ? $"{trytb.Type.DeclaringType.Namespace?.NotNullAndConcat(".")}{trytb.Type.DeclaringType.Name}.{trytb.Type.Name}" : $"{trytb.Type.Namespace?.NotNullAndConcat(".")}{trytb.Type.Name}"; + var trytbTypeName = trytb.Type.DisplayCsharp(); var trytbTypeLazyName = default(string); StringBuilder cscode = null; if (common.CodeFirst.IsLazyLoading && propsLazy.Any()) @@ -448,10 +448,8 @@ namespace FreeSql.Internal } public static void AddTableRef(CommonUtils common, TableInfo trytb, PropertyInfo pnv, bool isLazy, NaviteTuple vp, StringBuilder cscode) { - var trytbTypeName = trytb.Type.IsNested ? $"{trytb.Type.DeclaringType.Namespace?.NotNullAndConcat(".")}{trytb.Type.DeclaringType.Name}.{trytb.Type.Name}" : $"{trytb.Type.Namespace?.NotNullAndConcat(".")}{trytb.Type.Name}"; - var propTypeName = pnv.PropertyType.IsGenericType ? - $"{pnv.PropertyType.Namespace?.NotNullAndConcat(".")}{pnv.PropertyType.Name.Remove(pnv.PropertyType.Name.IndexOf('`'))}<{string.Join(", ", pnv.PropertyType.GetGenericArguments().Select(a => a.IsNested ? $"{a.DeclaringType.Namespace?.NotNullAndConcat(".")}{a.DeclaringType.Name}.{a.Name}" : $"{a.Namespace?.NotNullAndConcat(".")}{a.Name}"))}>" : - (pnv.PropertyType.IsNested ? $"{pnv.PropertyType.DeclaringType.Namespace?.NotNullAndConcat(".")}{pnv.PropertyType.DeclaringType.Name}.{pnv.PropertyType.Name}" : $"{pnv.PropertyType.Namespace?.NotNullAndConcat(".")}{pnv.PropertyType.Name}"); + var trytbTypeName = trytb.Type.DisplayCsharp(); + var propTypeName = pnv.PropertyType.DisplayCsharp(); var pnvAttr = common.GetEntityNavigateAttribute(trytb.Type, pnv); var pnvBind = pnvAttr?.Bind?.Split(',').Select(a => a.Trim()).Where(a => !string.IsNullOrEmpty(a)).ToArray(); @@ -474,7 +472,7 @@ namespace FreeSql.Internal var tbref = propElementType == trytb.Type ? trytb : GetTableByEntity(propElementType, common); //可能是父子关系 if (tbref == null) return; - var tbrefTypeName = tbref.Type.IsNested ? $"{tbref.Type.DeclaringType.Namespace?.NotNullAndConcat(".")}{tbref.Type.DeclaringType.Name}.{tbref.Type.Name}" : $"{tbref.Type.Namespace?.NotNullAndConcat(".")}{tbref.Type.Name}"; + var tbrefTypeName = tbref.Type.DisplayCsharp(); Type midType = null; var isManyToMany = false; @@ -797,8 +795,8 @@ namespace FreeSql.Internal .Append(" if (base.").Append(pnv.Name).Append(" == null && __lazy__").Append(pnv.Name).AppendLine(" == false) {"); if (nvref.Exception == null) - cscode.Append(" base.").Append(pnv.Name).Append(" = __fsql_orm__.Select<").Append(propElementType.IsNested ? $"{propElementType.DeclaringType.Namespace?.NotNullAndConcat(".")}{propElementType.DeclaringType.Name}.{propElementType.Name}" : $"{propElementType.Namespace?.NotNullAndConcat(".")}{propElementType.Name}") - .Append(">().Where(a => __fsql_orm__.Select<").Append(tbmid.Type.IsNested ? $"{tbmid.Type.DeclaringType.Namespace?.NotNullAndConcat(".")}{tbmid.Type.DeclaringType.Name}.{tbmid.Type.Name}" : $"{tbmid.Type.Namespace?.NotNullAndConcat(".")}{tbmid.Type.Name}") + cscode.Append(" base.").Append(pnv.Name).Append(" = __fsql_orm__.Select<").Append(propElementType.DisplayCsharp()) + .Append(">().Where(a => __fsql_orm__.Select<").Append(tbmid.Type.DisplayCsharp()) .Append(">().Where(b => ").Append(lmbdWhere.ToString()).AppendLine(").Any()).ToList();") .Append(" __lazy__").Append(pnv.Name).AppendLine(" = true;"); else @@ -933,7 +931,7 @@ namespace FreeSql.Internal if (nvref.Exception == null) { - cscode.Append(" base.").Append(pnv.Name).Append(" = __fsql_orm__.Select<").Append(propElementType.IsNested ? $"{propElementType.DeclaringType.Namespace?.NotNullAndConcat(".")}{propElementType.DeclaringType.Name}.{propElementType.Name}" : $"{propElementType.Namespace?.NotNullAndConcat(".")}{propElementType.Name}").Append(">().Where(a => ").Append(lmbdWhere.ToString()).AppendLine(").ToList();"); + cscode.Append(" base.").Append(pnv.Name).Append(" = __fsql_orm__.Select<").Append(propElementType.DisplayCsharp()).Append(">().Where(a => ").Append(lmbdWhere.ToString()).AppendLine(").ToList();"); if (refprop != null) { cscode.Append(" foreach (var loc1 in base.").Append(pnv.Name).AppendLine(")") @@ -969,7 +967,7 @@ namespace FreeSql.Internal trytb.AddOrUpdateTableRef(pnv.Name, nvref); //if (isLazy) throw nvref.Exception; } - var tbrefTypeName = tbref.Type.IsNested ? $"{tbref.Type.DeclaringType.Namespace?.NotNullAndConcat(".")}{tbref.Type.DeclaringType.Name}.{tbref.Type.Name}" : $"{tbref.Type.Namespace?.NotNullAndConcat(".")}{tbref.Type.Name}"; + var tbrefTypeName = tbref.Type.DisplayCsharp(); var isOnoToOne = pnv.PropertyType != trytb.Type && tbref.Properties.Where(z => z.Value.PropertyType == trytb.Type).Any() && tbref.Primarys.Length == trytb.Primarys.Length &&