diff --git a/src/BootstrapBlazor/Components/Table/Dynamic/DataTableDynamicObject.cs b/src/BootstrapBlazor/Components/Table/Dynamic/DataTableDynamicObject.cs index 72f3cd6b9d001b6158f4216d93a7c8c5c711d7d8..6a35f284e4e99db9dc755088095aae9e1a549dc4 100644 --- a/src/BootstrapBlazor/Components/Table/Dynamic/DataTableDynamicObject.cs +++ b/src/BootstrapBlazor/Components/Table/Dynamic/DataTableDynamicObject.cs @@ -16,15 +16,6 @@ namespace BootstrapBlazor.Components /// public DataRow? Row { get; set; } - /// - /// Clone 方法 - /// - /// - public override object Clone() - { - throw new System.NotImplementedException(); - } - /// /// /// diff --git a/src/BootstrapBlazor/Components/Table/Dynamic/DynamicObject.cs b/src/BootstrapBlazor/Components/Table/Dynamic/DynamicObject.cs index d576bed745bab550ae2ef4b9e355fa17b456139e..c2058d40dd528fe696b29a4cc2b6c093d52ec6b6 100644 --- a/src/BootstrapBlazor/Components/Table/Dynamic/DynamicObject.cs +++ b/src/BootstrapBlazor/Components/Table/Dynamic/DynamicObject.cs @@ -2,31 +2,31 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Website: https://www.blazor.zone or https://argozhang.github.io/ +using System.Linq; + namespace BootstrapBlazor.Components { /// /// 动态类型实体类 实例 /// - public class DynamicObject : IDynamicObject + public class DynamicObject { - /// - /// - /// - /// - public virtual object Clone() => new DynamicObject(); - /// /// /// /// /// - public virtual object? GetValue(string propertyName) => null; + public virtual object? GetValue(string propertyName) => LambdaExtensions.GetPropertyValue(this, propertyName); /// /// /// /// /// - public virtual void SetValue(string propertyName, object? value) { } + public virtual void SetValue(string propertyName, object? value) + { + var invoker = LambdaExtensions.SetPropertyValueLambda(this, propertyName).Compile(); + invoker(this, value); + } } } diff --git a/src/BootstrapBlazor/Components/Validate/ValidateBase.cs b/src/BootstrapBlazor/Components/Validate/ValidateBase.cs index b078e2b9bff4f6e667c9c82cc7edd990d13adbb4..17e32d3efa422a512d87ce6a81111f0c5c158423 100644 --- a/src/BootstrapBlazor/Components/Validate/ValidateBase.cs +++ b/src/BootstrapBlazor/Components/Validate/ValidateBase.cs @@ -270,7 +270,6 @@ namespace BootstrapBlazor.Components { // This is the first run // Could put this logic in OnInit, but its nice to avoid forcing people who override OnInit to call base.OnInit() - if (CascadedEditContext != null) { EditContext = CascadedEditContext; diff --git a/src/BootstrapBlazor/Components/ValidateForm/ValidateForm.razor.cs b/src/BootstrapBlazor/Components/ValidateForm/ValidateForm.razor.cs index 2bc178dbc69913ff4df233f718828f004b0f1455..d5e93aaa0e5b57ac895797eea02444ceaa9087e7 100644 --- a/src/BootstrapBlazor/Components/ValidateForm/ValidateForm.razor.cs +++ b/src/BootstrapBlazor/Components/ValidateForm/ValidateForm.razor.cs @@ -264,7 +264,7 @@ namespace BootstrapBlazor.Components var rules = propertyInfo.GetCustomAttributes(true).OfType(); var displayName = context.DisplayName; memberName ??= propertyInfo.Name; - var attributeSpan = "Attribute".AsSpan(); + var attributeSpan = nameof(Attribute).AsSpan(); foreach (var rule in rules) { var result = rule.GetValidationResult(value, context); diff --git a/src/BootstrapBlazor/Extensions/ObjectExtensions.cs b/src/BootstrapBlazor/Extensions/ObjectExtensions.cs index e4a7202d4caa908903b791f62de48da0a312fdb0..e39f6864e138e1115d592e831d2b6c3b6d77301c 100644 --- a/src/BootstrapBlazor/Extensions/ObjectExtensions.cs +++ b/src/BootstrapBlazor/Extensions/ObjectExtensions.cs @@ -4,9 +4,6 @@ using Microsoft.AspNetCore.Components; using System; -using System.Collections.Concurrent; -using System.Collections.Generic; -using System.Data; using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.Linq; diff --git a/src/BootstrapBlazor/Utils/Utility.cs b/src/BootstrapBlazor/Utils/Utility.cs index 30a2d8da35b7b997620e35d5560843ddb1e7fe1f..89dafe21a6360652291dadabca7c4f5acce02f2d 100644 --- a/src/BootstrapBlazor/Utils/Utility.cs +++ b/src/BootstrapBlazor/Utils/Utility.cs @@ -49,6 +49,8 @@ namespace BootstrapBlazor.Components /// public static string GetDisplayName(Type modelType, string fieldName) { + if (modelType.Assembly.IsDynamic) return fieldName; + var cacheKey = (CultureInfoName: CultureInfo.CurrentUICulture.Name, Type: modelType, FieldName: fieldName); if (!DisplayNameCache.TryGetValue(cacheKey, out var dn)) { @@ -109,27 +111,31 @@ namespace BootstrapBlazor.Components /// public static string? GetPlaceHolder(Type modelType, string fieldName) { - var cacheKey = (Type: modelType, FieldName: fieldName); - if (!PlaceHolderCache.TryGetValue(cacheKey, out var placeHolder)) + string? placeHolder = null; + if (!modelType.Assembly.IsDynamic) { - // 通过资源文件查找 FieldName 项 - var localizer = JsonStringLocalizerFactory.CreateLocalizer(cacheKey.Type); - var stringLocalizer = localizer?[$"{fieldName}.PlaceHolder"]; - if (stringLocalizer != null && !stringLocalizer.ResourceNotFound) + var cacheKey = (Type: modelType, FieldName: fieldName); + if (!PlaceHolderCache.TryGetValue(cacheKey, out placeHolder)) { - placeHolder = stringLocalizer.Value; - } - else if (Utility.TryGetProperty(cacheKey.Type, cacheKey.FieldName, out var propertyInfo)) - { - var placeHolderAttribute = propertyInfo.GetCustomAttribute(); - if (placeHolderAttribute != null) + // 通过资源文件查找 FieldName 项 + var localizer = JsonStringLocalizerFactory.CreateLocalizer(cacheKey.Type); + var stringLocalizer = localizer?[$"{fieldName}.PlaceHolder"]; + if (stringLocalizer != null && !stringLocalizer.ResourceNotFound) { - placeHolder = placeHolderAttribute.Text; + placeHolder = stringLocalizer.Value; } - if (!string.IsNullOrEmpty(placeHolder)) + else if (Utility.TryGetProperty(cacheKey.Type, cacheKey.FieldName, out var propertyInfo)) { - // add display name into cache - PlaceHolderCache.GetOrAdd(cacheKey, key => placeHolder); + var placeHolderAttribute = propertyInfo.GetCustomAttribute(); + if (placeHolderAttribute != null) + { + placeHolder = placeHolderAttribute.Text; + } + if (!string.IsNullOrEmpty(placeHolder)) + { + // add display name into cache + PlaceHolderCache.GetOrAdd(cacheKey, key => placeHolder); + } } } } @@ -189,24 +195,28 @@ namespace BootstrapBlazor.Components var type = item.GetType(); if (type.IsClass) { - ret = Activator.CreateInstance(); - var valType = ret?.GetType(); - if (valType != null) + var instance = Activator.CreateInstance(type); + if (instance != null) { - // 20200608 tian_teng@outlook.com 支持字段和只读属性 - foreach (var f in type.GetFields()) - { - var v = f.GetValue(item); - valType.GetField(f.Name)?.SetValue(ret, v); - }; - foreach (var p in type.GetProperties()) + ret = (TModel)instance; + var valType = ret?.GetType(); + if (valType != null) { - if (p.CanWrite) + // 20200608 tian_teng@outlook.com 支持字段和只读属性 + foreach (var f in type.GetFields()) { - var v = p.GetValue(item); - valType.GetProperty(p.Name)?.SetValue(ret, v); - } - }; + var v = f.GetValue(item); + valType.GetField(f.Name)?.SetValue(ret, v); + }; + foreach (var p in type.GetProperties()) + { + if (p.CanWrite) + { + var v = p.GetValue(item); + valType.GetProperty(p.Name)?.SetValue(ret, v); + } + }; + } } } } @@ -246,7 +256,7 @@ namespace BootstrapBlazor.Components } } - #region + #region GenerateColumns /// /// 通过指定 Model 获得 IEditorItem 集合方法 ///