Skip to content

Commit dd729e2

Browse files
authored
feat(EnsureInitialized): add CanWrite filter (#6665)
* refactor: 增加 CanWrite 过滤 * test: 更新单元测试 * chore: bump version 9.9.3
1 parent 7d225d9 commit dd729e2

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

src/BootstrapBlazor/BootstrapBlazor.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Razor">
22

33
<PropertyGroup>
4-
<Version>9.9.3-beta03</Version>
4+
<Version>9.9.3</Version>
55
</PropertyGroup>
66

77
<ItemGroup>

src/BootstrapBlazor/Extensions/ObjectExtensions.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,9 @@ private static void EnsureInitialized(this object? instance, bool isAutoInitiali
282282
}
283283

284284
// Reflection performance needs to be optimized here
285-
foreach (var propertyInfo in instance.GetType().GetProperties().Where(p => p.PropertyType.IsClass && p.PropertyType != typeof(string)))
285+
foreach (var propertyInfo in instance.GetType().GetProperties().Where(p => p.PropertyType.IsClass
286+
&& p.PropertyType != typeof(string)
287+
&& p.CanWrite))
286288
{
287289
var type = propertyInfo.PropertyType;
288290
var value = propertyInfo.GetValue(instance, null);

test/UnitTest/Extensions/ObjectExtensionsTest.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,11 @@ public void CreateInstance_Ok()
318318

319319
// 接口类型不报错
320320
Assert.Null(ObjectExtensions.CreateInstance<MockInterface>(true));
321+
322+
var bar = ObjectExtensions.CreateInstance<MockObject>(true);
323+
Assert.NotNull(bar);
324+
Assert.NotNull(bar.Foo);
325+
Assert.Null(bar.Bar);
321326
}
322327

323328
private interface MockInterface
@@ -332,6 +337,15 @@ private class MockComplexObject
332337
public (string Name, int Count)[]? Test { get; set; }
333338
}
334339

340+
private class MockObject
341+
{
342+
public string? Name { get; set; }
343+
344+
public Foo? Foo { get; set; }
345+
346+
public Foo? Bar { get; }
347+
}
348+
335349
private class MockStatic
336350
{
337351
private static int _test;

0 commit comments

Comments
 (0)