diff --git a/src/BootstrapBlazor/Utils/Utility.cs b/src/BootstrapBlazor/Utils/Utility.cs
index e33efed13b27cd4e0d7f02aadc12a009f6842edc..e6073bafa67949087edc55718f8e46818226332a 100644
--- a/src/BootstrapBlazor/Utils/Utility.cs
+++ b/src/BootstrapBlazor/Utils/Utility.cs
@@ -155,21 +155,30 @@ namespace BootstrapBlazor.Components
if (instance != null)
{
ret = (TModel)instance;
- var valType = ret?.GetType();
- if (valType != null)
+ if (ret != null)
{
+ var valType = ret.GetType();
+
// 20200608 tian_teng@outlook.com 支持字段和只读属性
foreach (var f in type.GetFields())
{
var v = f.GetValue(item);
- valType.GetField(f.Name)?.SetValue(ret, v);
+ var field = valType.GetField(f.Name);
+ if (field != null)
+ {
+ field.SetValue(ret, v);
+ }
};
foreach (var p in type.GetProperties())
{
if (p.CanWrite)
{
var v = p.GetValue(item);
- valType.GetProperty(p.Name)?.SetValue(ret, v);
+ var property = valType.GetProperty(p.Name);
+ if (property != null)
+ {
+ property.SetValue(ret, v);
+ }
}
};
}
@@ -189,26 +198,23 @@ namespace BootstrapBlazor.Components
///
public static void Copy(TModel source, TModel destination) where TModel : class
{
- if (source != null && destination != null)
+ var type = source.GetType();
+ var valType = destination.GetType();
+ if (valType != null)
{
- var type = source.GetType();
- var valType = destination.GetType();
- if (valType != null)
+ type.GetFields().ToList().ForEach(f =>
{
- type.GetFields().ToList().ForEach(f =>
- {
- var v = f.GetValue(source);
- valType.GetField(f.Name)?.SetValue(destination, v);
- });
- type.GetProperties().ToList().ForEach(p =>
+ var v = f.GetValue(source);
+ valType.GetField(f.Name)!.SetValue(destination, v);
+ });
+ type.GetProperties().ToList().ForEach(p =>
+ {
+ if (p.CanWrite)
{
- if (p.CanWrite)
- {
- var v = p.GetValue(source);
- valType.GetProperty(p.Name)?.SetValue(destination, v);
- }
- });
- }
+ var v = p.GetValue(source);
+ valType.GetProperty(p.Name)!.SetValue(destination, v);
+ }
+ });
}
}
@@ -218,15 +224,7 @@ namespace BootstrapBlazor.Components
///
///
///
- public static IEnumerable GenerateColumns(Func? predicate = null)
- {
- if (predicate == null)
- {
- predicate = p => true;
- }
-
- return InternalTableColumn.GetProperties().Where(predicate);
- }
+ public static IEnumerable GenerateColumns(Func predicate) => InternalTableColumn.GetProperties().Where(predicate);
///
///
diff --git a/test/UnitTest/Emit/TypeTest.cs b/test/UnitTest/Emit/TypeTest.cs
index 381c37d16fb9a6f0991cb66be9b62bc942b8812a..949b9bef7858cce82c913620b76c1481309529c0 100644
--- a/test/UnitTest/Emit/TypeTest.cs
+++ b/test/UnitTest/Emit/TypeTest.cs
@@ -3,11 +3,7 @@
// Website: https://www.blazor.zone or https://argozhang.github.io/
using BootstrapBlazor.Components;
-using Microsoft.AspNetCore.Components;
-using System;
-using System.Collections.Generic;
using System.Linq;
-using System.Threading.Tasks;
using Xunit;
namespace UnitTest.Emit
@@ -17,128 +13,21 @@ namespace UnitTest.Emit
[Fact]
public void CreateType_Ok()
{
- var cols = new Foo[]
- {
- new("Id", typeof(int)),
- new("Name", typeof(string))
- };
-
// 创建动态类型基类是 DynamicObject
- var instanceType = EmitHelper.CreateTypeByName("Test", cols, typeof(DynamicObject));
+ var instanceType = DynamicObjectHelper.CreateDynamicType();
Assert.NotNull(instanceType);
+ Assert.Equal(typeof(DynamicObject), instanceType.BaseType);
- if (instanceType != null)
- {
- Assert.Equal(typeof(DynamicObject), instanceType.BaseType);
-
- // 创建动态类型实例
- var instance = Activator.CreateInstance(instanceType);
- Assert.NotNull(instance);
-
- if (instance != null)
- {
- var properties = instance.GetType().GetProperties().Select(p => p.Name);
- Assert.Contains(nameof(DynamicObject.DynamicObjectPrimaryKey), properties);
- }
- }
- }
-
- private class Foo : ITableColumn
- {
- public Foo(string fieldName, Type propertyType) => (FieldName, PropertyType) = (fieldName, propertyType);
-
- public string FieldName { get; }
-
- public Type PropertyType { get; }
-
- public bool Editable { get; set; }
-
- public bool Readonly { get; set; }
-
- ///
- /// 获得/设置 新建时此列只读 默认为 false
- ///
- public bool IsReadonlyWhenAdd { get; set; }
-
- ///
- /// 获得/设置 编辑时此列只读 默认为 false
- ///
- public bool IsReadonlyWhenEdit { get; set; }
-
- public bool SkipValidate { get; set; }
-
- public string? Text { get; set; }
-
- public IEnumerable? Items { get; set; }
-
- public object? Step { get; set; }
-
- public int Rows { get; set; }
-
- public RenderFragment