Skip to content

Commit 1c076b8

Browse files
committed
Allow usage of private setter
1 parent 8e65717 commit 1c076b8

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

src/FubarDev.BeanIO/Internal/Util/BeanUtil.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ private string RemovePrefixForSetter(string setterName)
354354
var typeInfo = _typeInfo;
355355
while (typeInfo != typeof(object))
356356
{
357-
methodInfo = typeInfo.GetMethod(name);
357+
methodInfo = typeInfo.GetMethod(name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
358358
if (methodInfo != null)
359359
break;
360360
if (typeInfo.BaseType == null)

test/FubarDev.BeanIO.Test/Beans/Bean.cs

+9
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,14 @@ public class Bean
3737
public Bean? record;
3838
public Bean? segment;
3939
#endregion
40+
41+
public int? NoOpSetter
42+
{
43+
get => 1;
44+
}
45+
46+
private void SetNoOpSetter(int? value)
47+
{
48+
}
4049
}
4150
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// <copyright file="PrivateSetterTests.cs" company="Fubar Development Junker">
2+
// Copyright (c) 2016 Fubar Development Junker. All rights reserved.
3+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
4+
// </copyright>
5+
6+
using BeanIO.Beans;
7+
using BeanIO.Builder;
8+
9+
using Xunit;
10+
11+
namespace BeanIO.Issues;
12+
13+
public class PrivateSetterTests
14+
{
15+
[Fact]
16+
public void TestPrivateSetter()
17+
{
18+
var factory = StreamFactory.NewInstance();
19+
factory.Define(
20+
new StreamBuilder("s")
21+
.Format("fixedlength")
22+
.AddRecord(
23+
new RecordBuilder("r", typeof(Bean))
24+
.AddField(new FieldBuilder(nameof(Bean.NoOpSetter))
25+
.Length(1)
26+
.RegEx("[0-9]")
27+
.Setter("SetNoOpSetter"))));
28+
var unmarshaller = factory.CreateUnmarshaller("s");
29+
var bean = Assert.IsType<Bean>(unmarshaller.Unmarshal("1"));
30+
Assert.Equal(1, bean.NoOpSetter);
31+
}
32+
}

0 commit comments

Comments
 (0)