Skip to content

Commit

Permalink
fix: ensure returning false when getter/setter is missing from proper…
Browse files Browse the repository at this point in the history
…ty and trying to get/set value
  • Loading branch information
myarichuk committed Sep 17, 2022
1 parent 4cd60e6 commit 48f9c6c
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 25 deletions.
11 changes: 9 additions & 2 deletions src/ObjectTreeWalker/ObjectAccessor.cs
Expand Up @@ -36,8 +36,15 @@ public ObjectAccessor(Type objectType)
_objectType = objectType;
foreach (var propertyInfo in objectType.GetProperties(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public))
{
_getPropertyMethods.Add(propertyInfo.Name, CreateGetPropertyFunc(propertyInfo));
_setPropertyMethods.Add(propertyInfo.Name, CreateSetPropertyFunc(propertyInfo));
if (propertyInfo.GetMethod != null)
{
_getPropertyMethods.Add(propertyInfo.Name, CreateGetPropertyFunc(propertyInfo));
}

if (propertyInfo.SetMethod != null)
{
_setPropertyMethods.Add(propertyInfo.Name, CreateSetPropertyFunc(propertyInfo));
}
}

foreach (var fieldInfo in objectType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public))
Expand Down
23 changes: 0 additions & 23 deletions src/ObjectTreeWalker/ObjectIterator.cs

This file was deleted.

3 changes: 3 additions & 0 deletions src/ObjectTreeWalker/ObjectTreeWalker.cs
@@ -1,4 +1,5 @@
using System.Collections.Concurrent;
using Microsoft.Extensions.ObjectPool;

namespace ObjectTreeWalker
{
Expand All @@ -8,6 +9,8 @@ namespace ObjectTreeWalker
public class ObjectMemberIterator
{
private static readonly ConcurrentDictionary<Type, ObjectAccessor> ObjectAccessorCache = new();
private static readonly ObjectPool<Queue<KeyValuePair<string, object>>> TraversalQueuePool =
new DefaultObjectPoolProvider().Create<Queue<KeyValuePair<string, object>>>();

/// <summary>
/// Initializes a new instance of the <see cref="ObjectMemberIterator"/> class
Expand Down
38 changes: 38 additions & 0 deletions tests/ObjectTreeWalker.Tests/ObjectAccessorTests.cs
Expand Up @@ -13,6 +13,28 @@ internal class PublicFooBar
public PrivateFooBar Bar { get; set; } = new();
}

internal class PublicFooBarNoSet
{
public int Foo { get; } = 123;

public int Test123 = 555;

public PrivateFooBar Bar { get; set; } = new();
}

internal class PublicFooBarNoGet
{
public int Foo
{
set => _foo = value;
}

public int Test123 = 555;
private int _foo;

public PrivateFooBar Bar { get; set; } = new();
}

internal struct PublicFooBarStruct
{
public int Foo { get; set; } = 123;
Expand Down Expand Up @@ -77,6 +99,13 @@ public void Can_get_public_value_type_property(Type type)
Assert.Equal(123, value);
}

[Fact]
public void Should_fail_get_public_value_no_get()
{
var accessor = new ObjectAccessor(typeof(PublicFooBarNoGet));
Assert.False(accessor.TryGetValue(new PublicFooBarNoGet(), "Foo", out var value));
}

[Theory]
[InlineData(typeof(PublicFooBar))]
[InlineData(typeof(PublicFooBarStruct))]
Expand Down Expand Up @@ -137,6 +166,15 @@ public void Can_set_public_value_type_property(Type type)
Assert.Equal(345, ((dynamic)obj).Foo);
}

[Fact]
public void Should_fail_setting_property_without_set()
{
var accessor = new ObjectAccessor(typeof(PublicFooBarNoSet));
var obj = new PublicFooBarNoSet();

Assert.False(accessor.TrySetValue(obj, "Foo", 345));
}

[Theory]
[InlineData(typeof(PublicFooBar))]
[InlineData(typeof(PublicFooBarStruct))]
Expand Down

0 comments on commit 48f9c6c

Please sign in to comment.