Skip to content

Commit

Permalink
Allow usage of private setter
Browse files Browse the repository at this point in the history
  • Loading branch information
fubar-coder committed Jan 3, 2024
1 parent 8e65717 commit 1c076b8
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/FubarDev.BeanIO/Internal/Util/BeanUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions test/FubarDev.BeanIO.Test/Beans/Bean.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,14 @@ public class Bean
public Bean? record;
public Bean? segment;
#endregion

public int? NoOpSetter
{
get => 1;
}

private void SetNoOpSetter(int? value)
{
}
}
}
32 changes: 32 additions & 0 deletions test/FubarDev.BeanIO.Test/Issues/PrivateSetterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// <copyright file="PrivateSetterTests.cs" company="Fubar Development Junker">
// 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.
// </copyright>

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<Bean>(unmarshaller.Unmarshal("1"));
Assert.Equal(1, bean.NoOpSetter);
}
}

0 comments on commit 1c076b8

Please sign in to comment.