From 1c076b836aa11aec2cde12781736801f1ee16d2d Mon Sep 17 00:00:00 2001 From: Mark Junker Date: Wed, 3 Jan 2024 10:31:03 +0100 Subject: [PATCH] Allow usage of private setter --- src/FubarDev.BeanIO/Internal/Util/BeanUtil.cs | 2 +- test/FubarDev.BeanIO.Test/Beans/Bean.cs | 9 ++++++ .../Issues/PrivateSetterTests.cs | 32 +++++++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 test/FubarDev.BeanIO.Test/Issues/PrivateSetterTests.cs diff --git a/src/FubarDev.BeanIO/Internal/Util/BeanUtil.cs b/src/FubarDev.BeanIO/Internal/Util/BeanUtil.cs index 9546f86..a9473fa 100644 --- a/src/FubarDev.BeanIO/Internal/Util/BeanUtil.cs +++ b/src/FubarDev.BeanIO/Internal/Util/BeanUtil.cs @@ -354,7 +354,7 @@ private string RemovePrefixForSetter(string setterName) var typeInfo = _typeInfo; while (typeInfo != typeof(object)) { - methodInfo = typeInfo.GetMethod(name); + methodInfo = typeInfo.GetMethod(name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); if (methodInfo != null) break; if (typeInfo.BaseType == null) diff --git a/test/FubarDev.BeanIO.Test/Beans/Bean.cs b/test/FubarDev.BeanIO.Test/Beans/Bean.cs index 8e353a2..00f6e39 100644 --- a/test/FubarDev.BeanIO.Test/Beans/Bean.cs +++ b/test/FubarDev.BeanIO.Test/Beans/Bean.cs @@ -37,5 +37,14 @@ public class Bean public Bean? record; public Bean? segment; #endregion + + public int? NoOpSetter + { + get => 1; + } + + private void SetNoOpSetter(int? value) + { + } } } diff --git a/test/FubarDev.BeanIO.Test/Issues/PrivateSetterTests.cs b/test/FubarDev.BeanIO.Test/Issues/PrivateSetterTests.cs new file mode 100644 index 0000000..2e6b613 --- /dev/null +++ b/test/FubarDev.BeanIO.Test/Issues/PrivateSetterTests.cs @@ -0,0 +1,32 @@ +// +// Copyright (c) 2016 Fubar Development Junker. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. +// + +using BeanIO.Beans; +using BeanIO.Builder; + +using Xunit; + +namespace BeanIO.Issues; + +public class PrivateSetterTests +{ + [Fact] + public void TestPrivateSetter() + { + var factory = StreamFactory.NewInstance(); + factory.Define( + new StreamBuilder("s") + .Format("fixedlength") + .AddRecord( + new RecordBuilder("r", typeof(Bean)) + .AddField(new FieldBuilder(nameof(Bean.NoOpSetter)) + .Length(1) + .RegEx("[0-9]") + .Setter("SetNoOpSetter")))); + var unmarshaller = factory.CreateUnmarshaller("s"); + var bean = Assert.IsType(unmarshaller.Unmarshal("1")); + Assert.Equal(1, bean.NoOpSetter); + } +}