Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement IncludeVirtualMembers in MembersConfig #27

Merged
merged 2 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/Dumpify.Tests/Providers/MemberProviderTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace Dumpify.Tests.Providers;

public class MemberProviderTests
{
[Theory]
[InlineData(true)]
[InlineData(false)]
public void IncludeVirtualMembers(bool includeVirtualMembers)
{
var membersConfig = new MembersConfig { IncludeVirtualMembers = includeVirtualMembers };
var testClass = new ClassWithVirtualProperty();

var output = testClass.DumpText(members: membersConfig);
var isContainsVirtualProperty = output.Contains(testClass.Bar);

includeVirtualMembers.Should().Be(isContainsVirtualProperty);
}

private class ClassWithVirtualProperty
{
public string Foo { get; set; } = "Oleg";
public virtual string Bar { get; set; } = "Hello";
}
}
1 change: 1 addition & 0 deletions src/Dumpify/Config/MembersConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ public class MembersConfig
{
public bool IncludePublicMembers { get; set; } = true;
public bool IncludeNonPublicMembers { get; set; } = false;
public bool IncludeVirtualMembers { get; set; } = true;
public bool IncludeProperties { get; set; } = true;
public bool IncludeFields { get; set; } = false;
}
30 changes: 21 additions & 9 deletions src/Dumpify/Descriptors/ValueProviders/MemberProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,24 @@ internal sealed record MemberProvider : IMemberProvider
private readonly bool _includeFields;
private readonly bool _includePublicMembers;
private readonly bool _includeNonPublicMembers;
private readonly bool _includeVirtualMembers;

public MemberProvider()
: this(true, false, true, false) { }
: this(true, false, true, false, true) { }

public MemberProvider(
bool includeProperties,
bool includeFields,
bool includePublicMembers,
bool includeNonPublicMembers
bool includeNonPublicMembers,
bool includeVirtualMembers
)
{
_includeProperties = includeProperties;
_includeFields = includeFields;
_includePublicMembers = includePublicMembers;
_includeNonPublicMembers = includeNonPublicMembers;
_includeVirtualMembers = includeVirtualMembers;
}

public IEnumerable<IValueProvider> GetMembers(Type type)
Expand All @@ -38,10 +41,14 @@ public IEnumerable<IValueProvider> GetMembers(Type type)
{
var properties = type.GetProperties(flags)
.Where(p => p.GetIndexParameters().Length == 0)
.Where(p => p.GetMethod is not null)
.Select(p => new PropertyValueProvider(p));
.Where(p => p.GetMethod is not null);

members = members.Concat(properties);
if (!_includeVirtualMembers)
properties = properties.Where(p => !IsVirtualProperty(p));

var providers = properties.Select(p => new PropertyValueProvider(p));

members = members.Concat(providers);
}

if (_includeFields)
Expand All @@ -62,16 +69,21 @@ public bool Equals(IMemberProvider? provider)
}

return _includeProperties == other._includeProperties
&& _includeFields == other._includeFields
&& _includePublicMembers == other._includePublicMembers
&& _includeNonPublicMembers == other._includeNonPublicMembers;
&& _includeFields == other._includeFields
&& _includePublicMembers == other._includePublicMembers
&& _includeNonPublicMembers == other._includeNonPublicMembers
&& _includeVirtualMembers == other._includeVirtualMembers;
}

public override int GetHashCode() =>
(
_includeProperties,
_includeFields,
_includePublicMembers,
_includeNonPublicMembers
_includeNonPublicMembers,
_includeVirtualMembers
).GetHashCode();

private bool IsVirtualProperty(PropertyInfo propertyInfo) =>
propertyInfo.GetAccessors().Any(accessor => accessor.IsVirtual);
}
3 changes: 2 additions & 1 deletion src/Dumpify/Extensions/DumpExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,8 @@ public static string DumpText<T>(
membersConfig.IncludeProperties,
membersConfig.IncludeFields,
membersConfig.IncludePublicMembers,
membersConfig.IncludeNonPublicMembers
membersConfig.IncludeNonPublicMembers,
membersConfig.IncludeVirtualMembers
),
TypeNameProvider = new TypeNameProvider(
typeNamingConfig.UseAliases,
Expand Down
Loading