From d5f88915fd31dbcff9728428538475faa2bf958b Mon Sep 17 00:00:00 2001 From: yanqi <1584329729@qq.com> Date: Fri, 7 Apr 2023 20:01:50 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=9B=86=E5=90=88=E7=9A=84?= =?UTF-8?q?=E6=B3=9B=E5=9E=8B=E7=B1=BB=E5=9E=8B=E4=B8=BA=E5=8C=BF=E5=90=8D?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E6=97=B6=E7=B1=BB=E5=9E=8B=E8=BD=AC=E6=8D=A2?= =?UTF-8?q?bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Furion/App/Extensions/ObjectExtensions.cs | 13 +++++++ .../Internal/AnonymousTypeWrapper.cs | 3 +- tests/Furion.TestProject/ViewEngineTests.cs | 34 +++++++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/framework/Furion/App/Extensions/ObjectExtensions.cs b/framework/Furion/App/Extensions/ObjectExtensions.cs index 32303cb806..6296adc56a 100644 --- a/framework/Furion/App/Extensions/ObjectExtensions.cs +++ b/framework/Furion/App/Extensions/ObjectExtensions.cs @@ -225,6 +225,19 @@ public static class ObjectExtensions && type.Attributes.HasFlag(TypeAttributes.NotPublic); } + /// + /// 判断是否是匿名类型 + /// + /// + /// + internal static bool CheckAnonymous(this Type type) + { + return Attribute.IsDefined(type, typeof(CompilerGeneratedAttribute), false) + && type.IsGenericType && type.Name.Contains("AnonymousType") + && (type.Name.StartsWith("<>") || type.Name.StartsWith("VB$")) + && type.Attributes.HasFlag(TypeAttributes.NotPublic); + } + /// /// 获取所有祖先类型 /// diff --git a/framework/Furion/ViewEngine/Internal/AnonymousTypeWrapper.cs b/framework/Furion/ViewEngine/Internal/AnonymousTypeWrapper.cs index 23c279952d..cbad1e3211 100644 --- a/framework/Furion/ViewEngine/Internal/AnonymousTypeWrapper.cs +++ b/framework/Furion/ViewEngine/Internal/AnonymousTypeWrapper.cs @@ -71,7 +71,8 @@ public class AnonymousTypeWrapper : DynamicObject if (isEnumerable && result is not string) { var actType = type.IsArray ? type.GetElementType() : type.GenericTypeArguments[0]; - var list = Activator.CreateInstance(typeof(List<>).MakeGenericType(actType)); + var genericType = actType.CheckAnonymous() ? typeof(List) : typeof(List<>).MakeGenericType(actType); + var list = Activator.CreateInstance(genericType); var addMethod = list.GetType().GetMethod("Add"); var data = result as IEnumerable; diff --git a/tests/Furion.TestProject/ViewEngineTests.cs b/tests/Furion.TestProject/ViewEngineTests.cs index 1ddbee6882..7a24c6f17c 100644 --- a/tests/Furion.TestProject/ViewEngineTests.cs +++ b/tests/Furion.TestProject/ViewEngineTests.cs @@ -2,8 +2,10 @@ using Furion.DynamicApiController; using Furion.ViewEngine; using Furion.ViewEngine.Extensions; + using System; using System.IO; +using System.Linq; using System.Threading.Tasks; namespace Furion.TestProject; @@ -20,6 +22,38 @@ public class ViewEngineTests : IDynamicApiController _viewEngine = viewEngine; } + /// + /// 集合的泛型类型为匿名类型 + /// + /// + /// + public async Task TestRunCompile() + { + var str1 = await _viewEngine.RunCompileAsync(@" + Hello @Model.Name + @foreach(var item in Model.Items) + { +

@item

+ } + @foreach(var item in Model.list) + { +

@item.a

+ } + ", new + { + Name = "Furion", + Items = new[] { 3, 1, 2 }, + ////////////////这里这里 + list = Enumerable.Range(0, 10).Select(x => new + { + a = x.ToString(), + b = x.ToString(), + }).ToList() + }); + + return str1; + } + /// /// 测试 RunCompile 方法(弱类型模型) /// -- Gitee