From 61b0d0838990a4cb91684dcf500a2bfecedf4242 Mon Sep 17 00:00:00 2001 From: Argo-Tianyi Date: Fri, 24 Dec 2021 00:53:33 +0800 Subject: [PATCH 1/7] =?UTF-8?q?refactor:=20=E9=87=8D=E6=9E=84=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Components/Checkbox/CheckboxList.razor.cs | 3 +-- src/BootstrapBlazor/Components/Transfer/Transfer.razor.cs | 3 +-- .../Components/ValidateForm/ValidateForm.razor.cs | 7 +++---- src/BootstrapBlazor/Utils/Utility.cs | 2 +- 4 files changed, 6 insertions(+), 9 deletions(-) diff --git a/src/BootstrapBlazor/Components/Checkbox/CheckboxList.razor.cs b/src/BootstrapBlazor/Components/Checkbox/CheckboxList.razor.cs index 8ee038031..38f7bde1b 100644 --- a/src/BootstrapBlazor/Components/Checkbox/CheckboxList.razor.cs +++ b/src/BootstrapBlazor/Components/Checkbox/CheckboxList.razor.cs @@ -85,8 +85,7 @@ namespace BootstrapBlazor.Components { var pi = FieldIdentifier.Value.Model.GetType() .GetProperties() - .Where(p => p.Name == FieldIdentifier.Value.FieldName) - .FirstOrDefault(); + .FirstOrDefault(p => p.Name == FieldIdentifier.Value.FieldName); if (pi != null) { var required = pi.GetCustomAttribute(true); diff --git a/src/BootstrapBlazor/Components/Transfer/Transfer.razor.cs b/src/BootstrapBlazor/Components/Transfer/Transfer.razor.cs index 2aa4be26c..35dffedfe 100644 --- a/src/BootstrapBlazor/Components/Transfer/Transfer.razor.cs +++ b/src/BootstrapBlazor/Components/Transfer/Transfer.razor.cs @@ -139,8 +139,7 @@ namespace BootstrapBlazor.Components { var pi = FieldIdentifier.Value.Model.GetType() .GetProperties() - .Where(p => p.Name == FieldIdentifier.Value.FieldName) - .FirstOrDefault(); + .FirstOrDefault(p => p.Name == FieldIdentifier.Value.FieldName); if (pi != null) { var required = pi.GetCustomAttribute(true); diff --git a/src/BootstrapBlazor/Components/ValidateForm/ValidateForm.razor.cs b/src/BootstrapBlazor/Components/ValidateForm/ValidateForm.razor.cs index 116446971..c2d5f514b 100644 --- a/src/BootstrapBlazor/Components/ValidateForm/ValidateForm.razor.cs +++ b/src/BootstrapBlazor/Components/ValidateForm/ValidateForm.razor.cs @@ -167,8 +167,7 @@ namespace BootstrapBlazor.Components modelType = modelTypeInfo; fieldName = propName; var propertyInfo = modelType.GetProperties() - .Where(p => p.Name == propName) - .FirstOrDefault(); + .FirstOrDefault(p => p.Name == propName); if (propertyInfo == null) { break; @@ -211,7 +210,7 @@ namespace BootstrapBlazor.Components if (validator.IsNeedValidate) { var messages = new List(); - var pi = key.ModelType.GetProperties().Where(p => p.Name == key.FieldName).FirstOrDefault(); + var pi = key.ModelType.GetProperties().FirstOrDefault(p => p.Name == key.FieldName); if (pi != null) { var propertyValidateContext = new ValidationContext(fieldIdentifier.Model) @@ -247,7 +246,7 @@ namespace BootstrapBlazor.Components if (validator.IsNeedValidate) { var fieldName = fieldIdentifier.FieldName; - var pi = fieldIdentifier.Model.GetType().GetProperties().Where(p => p.Name == fieldName).FirstOrDefault(); + var pi = fieldIdentifier.Model.GetType().GetProperties().FirstOrDefault(p => p.Name == fieldName); if (pi != null) { var propertyValue = Utility.GetPropertyValue(fieldIdentifier.Model, fieldIdentifier.FieldName); diff --git a/src/BootstrapBlazor/Utils/Utility.cs b/src/BootstrapBlazor/Utils/Utility.cs index 4ae501174..aa8ec4bb7 100644 --- a/src/BootstrapBlazor/Utils/Utility.cs +++ b/src/BootstrapBlazor/Utils/Utility.cs @@ -123,7 +123,7 @@ namespace BootstrapBlazor.Components var v = new TModel(); foreach (var pi in source.GetType().GetProperties().Where(p => p.CanWrite)) { - var pinfo = v.GetType().GetProperties().Where(p => p.Name == pi.Name).FirstOrDefault(); + var pinfo = v.GetType().GetProperties().FirstOrDefault(p => p.Name == pi.Name); if (pinfo != null) { pi.SetValue(source, pinfo.GetValue(v)); -- Gitee From 2cdc4b7f5c138324471b160bb0d57d22471cad7a Mon Sep 17 00:00:00 2001 From: Argo-Tianyi Date: Fri, 24 Dec 2021 00:54:24 +0800 Subject: [PATCH 2/7] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=AD=90=E7=B1=BB?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=20new=20=E5=AF=BC=E8=87=B4=E5=B1=9E=E6=80=A7?= =?UTF-8?q?=E4=BA=8C=E4=B9=89=E6=80=A7=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/BootstrapBlazor/Components/Validate/ValidateBase.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/BootstrapBlazor/Components/Validate/ValidateBase.cs b/src/BootstrapBlazor/Components/Validate/ValidateBase.cs index 3596e40b9..15c2c2b12 100644 --- a/src/BootstrapBlazor/Components/Validate/ValidateBase.cs +++ b/src/BootstrapBlazor/Components/Validate/ValidateBase.cs @@ -245,7 +245,8 @@ namespace BootstrapBlazor.Components protected virtual string? FormatParsingErrorMessage() => ParsingErrorMessage; private bool IsRequired() => FieldIdentifier?.Model.GetType() - .GetProperty(FieldIdentifier.Value.FieldName)?.GetCustomAttribute(true) != null + .GetProperties() + .FirstOrDefault(p => p.Name == FieldIdentifier.Value.FieldName)?.GetCustomAttribute(true) != null || (ValidateRules?.OfType().Select(i => i.Validator).OfType().Any() ?? false); /// -- Gitee From e5a728cc22f919b1d866b1d7faf235d3ed77ecfd Mon Sep 17 00:00:00 2001 From: Argo-Tianyi Date: Fri, 24 Dec 2021 00:54:47 +0800 Subject: [PATCH 3/7] =?UTF-8?q?test:=20=E5=A2=9E=E5=8A=A0=E5=AD=90?= =?UTF-8?q?=E7=B1=BB=E4=BD=BF=E7=94=A8=20new=20=E9=87=8D=E5=86=99=E7=88=B6?= =?UTF-8?q?=E7=B1=BB=E5=B1=9E=E6=80=A7=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/UnitTest/Utils/ReflectionTest.cs | 60 +++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 test/UnitTest/Utils/ReflectionTest.cs diff --git a/test/UnitTest/Utils/ReflectionTest.cs b/test/UnitTest/Utils/ReflectionTest.cs new file mode 100644 index 000000000..fa879230b --- /dev/null +++ b/test/UnitTest/Utils/ReflectionTest.cs @@ -0,0 +1,60 @@ +// Copyright (c) Argo Zhang (argo@163.com). All rights reserved. +// 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; +using System.Reflection; +using Xunit; + +namespace UnitTest.Utils; + +public class ReflectionTest +{ + [Fact] + public void SubClass_Override() + { + var dog = new Dog() { Foo = "Test" }; + Assert.Equal("Test", dog.Foo); + + var p1 = dog.GetType().GetProperty("Foo"); + var p = dog.GetType().GetProperties().FirstOrDefault(p => p.Name == "Foo"); + + // 两种获取属性实例相等 + Assert.Equal(p1, p); + + // 反射获取值 + var v = p!.GetValue(dog); + Assert.Equal("Test", v); + } + + [Fact] + public void SubClass_New() + { + var cat = new Cat() { Foo = 1 }; + Assert.Equal(1, cat.Foo); + + // 由于使用 new 关键字导致报错混淆异常 + Assert.ThrowsAny(() => cat.GetType().GetProperty("Foo")); + + var p = cat.GetType().GetProperties().FirstOrDefault(p => p.Name == "Foo"); + + // 反射获取值 + var v = p!.GetValue(cat); + Assert.Equal(1, v); + } + + private class Dummy + { + public virtual string? Foo { get; set; } + } + + private class Dog : Dummy + { + public override string? Foo { get; set; } + } + + private class Cat : Dummy + { + public new int Foo { get; set; } + } +} -- Gitee From 6bc072f8c179ff889bbf6ce8901bb3675ac67ecb Mon Sep 17 00:00:00 2001 From: Argo-Tianyi Date: Fri, 24 Dec 2021 01:15:00 +0800 Subject: [PATCH 4/7] =?UTF-8?q?fix:=20=E5=85=BC=E5=AE=B9=E5=AD=90=E7=B1=BB?= =?UTF-8?q?=20new=20=E5=A4=8D=E5=86=99=E7=88=B6=E7=B1=BB=E5=B1=9E=E6=80=A7?= =?UTF-8?q?=E6=83=85=E5=86=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/BootstrapBlazor/Utils/Utility.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/BootstrapBlazor/Utils/Utility.cs b/src/BootstrapBlazor/Utils/Utility.cs index aa8ec4bb7..05002e7a4 100644 --- a/src/BootstrapBlazor/Utils/Utility.cs +++ b/src/BootstrapBlazor/Utils/Utility.cs @@ -348,7 +348,8 @@ namespace BootstrapBlazor.Components public static object GenerateValueExpression(object model, string fieldName, Type fieldType) { // ValueExpression - var body = Expression.Property(Expression.Constant(model), model.GetType(), fieldName); + var pi = model.GetType().GetProperties().FirstOrDefault(p => p.Name == fieldName) ?? throw new InvalidOperationException($"the model {model.GetType().Name} not found property {fieldName}"); + var body = Expression.Property(Expression.Constant(model), pi); var tDelegate = typeof(Func<>).MakeGenericType(fieldType); return Expression.Lambda(tDelegate, body); } -- Gitee From 1593265302d92666a66b1bdab218de6f2478efe4 Mon Sep 17 00:00:00 2001 From: Argo-Tianyi Date: Fri, 24 Dec 2021 01:15:25 +0800 Subject: [PATCH 5/7] =?UTF-8?q?fix:=20=E5=A2=9E=E5=8A=A0=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E5=88=A4=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/BootstrapBlazor/Utils/Utility.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BootstrapBlazor/Utils/Utility.cs b/src/BootstrapBlazor/Utils/Utility.cs index 05002e7a4..744eff9b7 100644 --- a/src/BootstrapBlazor/Utils/Utility.cs +++ b/src/BootstrapBlazor/Utils/Utility.cs @@ -174,7 +174,7 @@ namespace BootstrapBlazor.Components if (p.CanWrite) { var v = p.GetValue(item); - var property = valType.GetProperty(p.Name); + var property = valType.GetProperties().FirstOrDefault(i => i.Name == p.Name && i.PropertyType == p.PropertyType); if (property != null) { property.SetValue(ret, v); -- Gitee From d8c09ce385155ff2185890df4b7d7d82a66a7fe8 Mon Sep 17 00:00:00 2001 From: Argo-Tianyi Date: Fri, 24 Dec 2021 01:16:00 +0800 Subject: [PATCH 6/7] =?UTF-8?q?test:=20=E5=A2=9E=E5=8A=A0=E4=BA=8C?= =?UTF-8?q?=E4=B9=89=E6=80=A7=E5=BC=82=E5=B8=B8=E5=8D=95=E5=85=83=E6=B5=8B?= =?UTF-8?q?=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/UnitTest/Components/ValidateTest.cs | 33 ++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/test/UnitTest/Components/ValidateTest.cs b/test/UnitTest/Components/ValidateTest.cs index c51754ade..f84a6d174 100644 --- a/test/UnitTest/Components/ValidateTest.cs +++ b/test/UnitTest/Components/ValidateTest.cs @@ -499,6 +499,28 @@ namespace UnitTest.Components }); } + [Fact] + public void Required_AmbiguousMatch() + { + var model = new Cat(); + var rules = new List + { + new FormItemValidator(new RequiredAttribute()) + }; + var cut = Context.RenderComponent(builder => + { + builder.Add(v => v.Model, model); + builder.AddChildContent>(pb => + { + pb.Add(v => v.Value, model.Foo); + pb.Add(v => v.ValueExpression, Utility.GenerateValueExpression(model, nameof(Cat.Foo), typeof(int))); + pb.Add(v => v.ValidateRules, rules); + }); + }); + + // 不会报错 AmbiguousMatchException + } + [Fact] public void TooltipHost_Ok() { @@ -532,5 +554,16 @@ namespace UnitTest.Components OnValidate(true); } } + + class Dummy + { + public virtual string? Foo { get; set; } + } + + class Cat : Dummy + { + [Required] + public new int Foo { get; set; } + } } } -- Gitee From 20df37b7c91d031276aaa6a6f2b344079476d9a7 Mon Sep 17 00:00:00 2001 From: Argo-Tianyi Date: Fri, 24 Dec 2021 01:24:07 +0800 Subject: [PATCH 7/7] chore: bump version to 6.1.1 --- src/BootstrapBlazor/BootstrapBlazor.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BootstrapBlazor/BootstrapBlazor.csproj b/src/BootstrapBlazor/BootstrapBlazor.csproj index dd1b72541..2a723d2c6 100644 --- a/src/BootstrapBlazor/BootstrapBlazor.csproj +++ b/src/BootstrapBlazor/BootstrapBlazor.csproj @@ -1,7 +1,7 @@ - 6.1.1-beta12 + 6.1.1 -- Gitee