diff --git a/framework/Furion/App/Extensions/ObjectExtensions.cs b/framework/Furion/App/Extensions/ObjectExtensions.cs index 32303cb80681e634ce03a25885d20981cf8542ad..6296adc56a47c5b9bed941119fc86e1ac619ab4b 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 23c279952db926ab620363350c3c26e3214c9ee3..cbad1e3211541d8374734c253b23a877470eb556 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 1ddbee688298a20bae5f6e4e2ad0042392a52dde..7a24c6f17ca2dbcf18c514aa4680912bdf79679c 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 方法(弱类型模型) ///