From 415f3cce324a46ef5bf64d3d1eb4d093cd58d299 Mon Sep 17 00:00:00 2001 From: Argo-Tianyi Date: Sun, 4 Jul 2021 10:52:53 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E6=9B=B4=E6=96=B0=20EmitHelper=20?= =?UTF-8?q?=E5=B8=AE=E5=8A=A9=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/BootstrapBlazor/Utils/EmitHelper.cs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/BootstrapBlazor/Utils/EmitHelper.cs b/src/BootstrapBlazor/Utils/EmitHelper.cs index 4f4225be2..355b5b6ea 100644 --- a/src/BootstrapBlazor/Utils/EmitHelper.cs +++ b/src/BootstrapBlazor/Utils/EmitHelper.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Reflection; using System.Reflection.Emit; @@ -17,16 +18,17 @@ namespace BootstrapBlazor.Components /// /// 通过 ITableColumn 创建动态类 /// - public static Type? CreateTypeByName(string typeName, IEnumerable cols, Type? parent = null) + public static Type CreateTypeByName(string typeName, IEnumerable cols, Type? parent = null, Func>? creatingCallback = null) { var typeBuilder = CreateTypeBuilderByName(typeName, parent); foreach (var col in cols) { - typeBuilder.CreateProperty(col); + var attributeBuilds = creatingCallback?.Invoke(col); + typeBuilder.CreateProperty(col, attributeBuilds); } - return typeBuilder.CreateType(); + return typeBuilder.CreateType()!; } private static TypeBuilder CreateTypeBuilderByName(string typeName, Type? parent = null) @@ -38,7 +40,7 @@ namespace BootstrapBlazor.Components return typeBuilder; } - private static void CreateProperty(this TypeBuilder typeBuilder, IEditorItem col) + private static void CreateProperty(this TypeBuilder typeBuilder, IEditorItem col, IEnumerable? attributeBuilds) { var fieldName = col.GetFieldName(); var field = typeBuilder.DefineField($"_{fieldName}", col.PropertyType, FieldAttributes.Private); @@ -61,6 +63,11 @@ namespace BootstrapBlazor.Components var propertyId = typeBuilder.DefineProperty(fieldName, PropertyAttributes.None, col.PropertyType, null); propertyId.SetGetMethod(methodGetField); propertyId.SetSetMethod(methodSetField); + + foreach (var cab in attributeBuilds ?? Enumerable.Empty()) + { + propertyId.SetCustomAttribute(cab); + } } } } -- Gitee