Skip to content

Commit f5b665b

Browse files
authored
feat(Table): support interface TItem (#6619)
* refactor: 直接接口 * test: 更新单元测试
1 parent 2040c95 commit f5b665b

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

src/BootstrapBlazor/Extensions/ObjectExtensions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,11 @@ internal static void Clone<TModel>(this TModel source, TModel item)
246246
/// <returns>An instance of the specified type with initialized properties.</returns>
247247
public static TItem? CreateInstance<TItem>(bool isAutoInitializeModelProperty = false)
248248
{
249+
if(typeof(TItem).IsInterface)
250+
{
251+
return default;
252+
}
253+
249254
var instance = Activator.CreateInstance<TItem>();
250255
if (isAutoInitializeModelProperty)
251256
{

test/UnitTest/Extensions/ObjectExtensionsTest.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,14 @@ public void CreateInstance_Ok()
315315
var instance = ObjectExtensions.CreateInstance<MockComplexObject>(false);
316316
Assert.NotNull(instance);
317317
Assert.Null(instance.Test);
318+
319+
// 接口类型不报错
320+
Assert.Null(ObjectExtensions.CreateInstance<MockInterface>(true));
321+
}
322+
323+
private interface MockInterface
324+
{
325+
string? Name { get; set; }
318326
}
319327

320328
private class MockComplexObject

0 commit comments

Comments
 (0)