From a89334a205f0648b6a68a1a6501fa7cc7a0e4f9a Mon Sep 17 00:00:00 2001 From: Argo-Tianyi Date: Mon, 5 Jul 2021 11:19:47 +0800 Subject: [PATCH 1/4] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E6=89=A9?= =?UTF-8?q?=E5=B1=95=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Table/Dynamic/DynamicObjectContext.cs | 4 +-- .../DynamicObjectContextExtensions.cs | 36 +++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/BootstrapBlazor/Components/Table/Dynamic/DynamicObjectContext.cs b/src/BootstrapBlazor/Components/Table/Dynamic/DynamicObjectContext.cs index f55f625fe..76340b6d8 100644 --- a/src/BootstrapBlazor/Components/Table/Dynamic/DynamicObjectContext.cs +++ b/src/BootstrapBlazor/Components/Table/Dynamic/DynamicObjectContext.cs @@ -42,12 +42,12 @@ namespace BootstrapBlazor.Components /// /// /// - public void AddAttribute(string columnName, Type attributeType, Type[] types, object?[] constructorArgs, PropertyInfo[] propertyInfos, object?[] propertyValues) + public void AddAttribute(string columnName, Type attributeType, Type[] types, object?[] constructorArgs, PropertyInfo[]? propertyInfos = null, object?[]? propertyValues = null) { var attr = attributeType.GetConstructor(types); if (attr != null) { - var cab = new CustomAttributeBuilder(attr, constructorArgs, propertyInfos, propertyValues); + var cab = new CustomAttributeBuilder(attr, constructorArgs, propertyInfos ?? Array.Empty(), propertyValues ?? Array.Empty()); CustomerAttributeBuilderCache.AddOrUpdate(columnName, key => new List { cab }, (key, builders) => diff --git a/src/BootstrapBlazor/Extensions/DynamicObjectContextExtensions.cs b/src/BootstrapBlazor/Extensions/DynamicObjectContextExtensions.cs index 0dbadadef..ee877f5ea 100644 --- a/src/BootstrapBlazor/Extensions/DynamicObjectContextExtensions.cs +++ b/src/BootstrapBlazor/Extensions/DynamicObjectContextExtensions.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; +using System.ComponentModel; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Reflection; @@ -62,5 +63,40 @@ namespace BootstrapBlazor.Components } context.AddAttribute(columnName, type, Type.EmptyTypes, Array.Empty(), propertyInfos.ToArray(), propertyValues.ToArray()); } + + /// + /// 增加 AutoGenerateColumnAttribute 扩展方法 + /// + /// + /// + /// + public static void AddDisplayAttribute(this DynamicObjectContext context, string columnName, IEnumerable> parameters) + { + var type = typeof(DisplayAttribute); + var propertyInfos = new List(); + var propertyValues = new List(); + foreach (var kv in parameters) + { + var pInfo = type.GetProperty(kv.Key); + if (pInfo != null) + { + propertyInfos.Add(pInfo); + propertyValues.Add(kv.Value); + } + } + context.AddAttribute(columnName, type, Type.EmptyTypes, Array.Empty(), propertyInfos.ToArray(), propertyValues.ToArray()); + } + + /// + /// 增加 AutoGenerateColumnAttribute 扩展方法 + /// + /// + /// + /// + public static void AddDisplayNameAttribute(this DynamicObjectContext context, string columnName, string displayName) + { + var type = typeof(DisplayNameAttribute); + context.AddAttribute(columnName, type, new Type[] { typeof(string) }, new object?[] { displayName }); + } } } -- Gitee From 464c3109d6807ba1f418bb670e648bce4fab2d8b Mon Sep 17 00:00:00 2001 From: Argo-Tianyi Date: Mon, 5 Jul 2021 11:20:17 +0800 Subject: [PATCH 2/4] =?UTF-8?q?doc:=20=E6=9B=B4=E6=96=B0=E6=89=A9=E5=B1=95?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E7=A4=BA=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Pages/Samples/Table/TablesDynamic.razor.cs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/BootstrapBlazor.Shared/Pages/Samples/Table/TablesDynamic.razor.cs b/src/BootstrapBlazor.Shared/Pages/Samples/Table/TablesDynamic.razor.cs index 40842590f..50497cbb1 100644 --- a/src/BootstrapBlazor.Shared/Pages/Samples/Table/TablesDynamic.razor.cs +++ b/src/BootstrapBlazor.Shared/Pages/Samples/Table/TablesDynamic.razor.cs @@ -8,6 +8,7 @@ using Microsoft.AspNetCore.Components; using Microsoft.Extensions.Localization; using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; using System.Data; using System.Diagnostics.CodeAnalysis; using System.Threading.Tasks; @@ -59,22 +60,30 @@ namespace BootstrapBlazor.Shared.Pages.Table if (propertyName == nameof(Foo.DateTime)) { context.AddRequiredAttribute(nameof(Foo.DateTime)); - col.Text = Localizer[nameof(Foo.DateTime)]; + // 使用 AutoGenerateColumnAttribute 设置显示名称示例 + context.AddAutoGenerateColumnAttribute(nameof(Foo.DateTime), new KeyValuePair[] { + new(nameof(AutoGenerateColumnAttribute.Text), Localizer[nameof(Foo.DateTime)].Value) + }); } else if (propertyName == nameof(Foo.Name)) { - context.AddRequiredAttribute(nameof(Foo.Name), "测试姓名不能为空"); + context.AddRequiredAttribute(nameof(Foo.Name), Localizer["Name.Required"]); + // 使用 Text 设置显示名称示例 col.Text = Localizer[nameof(Foo.Name)]; } else if (propertyName == nameof(Foo.Count)) { context.AddRequiredAttribute(nameof(Foo.Count)); - col.Text = Localizer[nameof(Foo.Count)]; + // 使用 DisplayNameAttribute 设置显示名称示例 + context.AddDisplayNameAttribute(nameof(Foo.Count), Localizer[nameof(Foo.Count)].Value); } else if (propertyName == nameof(Foo.Complete)) { col.ComponentType = typeof(Switch); - col.Text = Localizer[nameof(Foo.Complete)]; + // 使用 DisplayAttribute 设置显示名称示例 + context.AddDisplayAttribute(nameof(Foo.Complete), new KeyValuePair[] { + new(nameof(DisplayAttribute.Name), Localizer[nameof(Foo.Complete)].Value) + }); } }); } -- Gitee From 0b63d6c0fc1f358c83f921e75ede4c01e73579b9 Mon Sep 17 00:00:00 2001 From: Argo-Tianyi Date: Mon, 5 Jul 2021 11:25:31 +0800 Subject: [PATCH 3/4] =?UTF-8?q?refactor:=20=E5=A2=9E=E5=8A=A0=E5=8F=82?= =?UTF-8?q?=E6=95=B0=E6=A0=87=E8=AF=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Components/Table/Dynamic/DynamicObjectContext.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/BootstrapBlazor/Components/Table/Dynamic/DynamicObjectContext.cs b/src/BootstrapBlazor/Components/Table/Dynamic/DynamicObjectContext.cs index 76340b6d8..e79fe0506 100644 --- a/src/BootstrapBlazor/Components/Table/Dynamic/DynamicObjectContext.cs +++ b/src/BootstrapBlazor/Components/Table/Dynamic/DynamicObjectContext.cs @@ -47,7 +47,9 @@ namespace BootstrapBlazor.Components var attr = attributeType.GetConstructor(types); if (attr != null) { - var cab = new CustomAttributeBuilder(attr, constructorArgs, propertyInfos ?? Array.Empty(), propertyValues ?? Array.Empty()); + var cab = new CustomAttributeBuilder(attr, constructorArgs, + namedProperties: propertyInfos ?? Array.Empty(), + propertyValues: propertyValues ?? Array.Empty()); CustomerAttributeBuilderCache.AddOrUpdate(columnName, key => new List { cab }, (key, builders) => -- Gitee From 503402bb4fc854671da3e53218c70f33e1836252 Mon Sep 17 00:00:00 2001 From: Argo-Tianyi Date: Mon, 5 Jul 2021 11:25:50 +0800 Subject: [PATCH 4/4] =?UTF-8?q?refactor:=20=E5=A2=9E=E5=8A=A0=E6=89=A9?= =?UTF-8?q?=E5=B1=95=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DynamicObjectContextExtensions.cs | 58 ++++++++----------- 1 file changed, 24 insertions(+), 34 deletions(-) diff --git a/src/BootstrapBlazor/Extensions/DynamicObjectContextExtensions.cs b/src/BootstrapBlazor/Extensions/DynamicObjectContextExtensions.cs index ee877f5ea..673fa2be7 100644 --- a/src/BootstrapBlazor/Extensions/DynamicObjectContextExtensions.cs +++ b/src/BootstrapBlazor/Extensions/DynamicObjectContextExtensions.cs @@ -6,7 +6,6 @@ using System; using System.Collections.Generic; using System.ComponentModel; using System.ComponentModel.DataAnnotations; -using System.Linq; using System.Reflection; namespace BootstrapBlazor.Components @@ -25,20 +24,12 @@ namespace BootstrapBlazor.Components /// public static void AddRequiredAttribute(this DynamicObjectContext context, string columnName, string? errorMessage = null, bool allowEmptyStrings = false) { - var type = typeof(RequiredAttribute); - var propertyInfos = new List(); - var propertyValues = new List(); - if (!string.IsNullOrEmpty(errorMessage)) - { - propertyInfos.Add(type.GetProperty(nameof(RequiredAttribute.ErrorMessage))!); - propertyValues.Add(errorMessage); - } - if (allowEmptyStrings) + var parameters = new KeyValuePair[] { - propertyInfos.Add(type.GetProperty(nameof(RequiredAttribute.AllowEmptyStrings))!); - propertyValues.Add(true); - } - context.AddAttribute(columnName, type, Type.EmptyTypes, Array.Empty(), propertyInfos.ToArray(), propertyValues.ToArray()); + new(nameof(RequiredAttribute.ErrorMessage), errorMessage), + new(nameof(RequiredAttribute.AllowEmptyStrings), allowEmptyStrings) + }; + context.AddMultipleParameterAttribute(columnName, parameters); } /// @@ -47,32 +38,19 @@ namespace BootstrapBlazor.Components /// /// /// - public static void AddAutoGenerateColumnAttribute(this DynamicObjectContext context, string columnName, IEnumerable> parameters) - { - var type = typeof(AutoGenerateColumnAttribute); - var propertyInfos = new List(); - var propertyValues = new List(); - foreach (var kv in parameters) - { - var pInfo = type.GetProperty(kv.Key); - if (pInfo != null) - { - propertyInfos.Add(pInfo); - propertyValues.Add(kv.Value); - } - } - context.AddAttribute(columnName, type, Type.EmptyTypes, Array.Empty(), propertyInfos.ToArray(), propertyValues.ToArray()); - } + public static void AddAutoGenerateColumnAttribute(this DynamicObjectContext context, string columnName, IEnumerable> parameters) => context.AddMultipleParameterAttribute(columnName, parameters); /// - /// 增加 AutoGenerateColumnAttribute 扩展方法 + /// 增加 DisplayAttribute 扩展方法 /// /// /// /// - public static void AddDisplayAttribute(this DynamicObjectContext context, string columnName, IEnumerable> parameters) + public static void AddDisplayAttribute(this DynamicObjectContext context, string columnName, IEnumerable> parameters) => context.AddMultipleParameterAttribute(columnName, parameters); + + private static void AddMultipleParameterAttribute(this DynamicObjectContext context, string columnName, IEnumerable> parameters) where TAttribute : Attribute { - var type = typeof(DisplayAttribute); + var type = typeof(TAttribute); var propertyInfos = new List(); var propertyValues = new List(); foreach (var kv in parameters) @@ -88,7 +66,7 @@ namespace BootstrapBlazor.Components } /// - /// 增加 AutoGenerateColumnAttribute 扩展方法 + /// 增加 DisplayNameAttribute 扩展方法 /// /// /// @@ -98,5 +76,17 @@ namespace BootstrapBlazor.Components var type = typeof(DisplayNameAttribute); context.AddAttribute(columnName, type, new Type[] { typeof(string) }, new object?[] { displayName }); } + + /// + /// 增加 DescriptionAttribute 扩展方法 + /// + /// + /// + /// + public static void AddDescriptionAttribute(this DynamicObjectContext context, string columnName, string description) + { + var type = typeof(DescriptionAttribute); + context.AddAttribute(columnName, type, new Type[] { typeof(string) }, new object?[] { description }); + } } } -- Gitee