Skip to content

Commit

Permalink
refactor "includeVirtualMembers"
Browse files Browse the repository at this point in the history
  • Loading branch information
MoaidHathot committed May 5, 2024
1 parent 34077dd commit f0dee72
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
7 changes: 5 additions & 2 deletions src/Dumpify.Playground/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,12 @@ void TestSpecific()
};

Person[] arr = [moaid, moaid];
arr.Dump();
// arr.Dump();

var value = SearchValues.Create("a");
// var value = SearchValues.Create("lskdjflskdfj").Dump();
new TestVirtual().Dump();
new TestVirtual().Dump("explcit include", members: new MembersConfig { IncludeVirtualMembers = true });
new TestVirtual().Dump("explcit exclude", members: new MembersConfig { IncludeVirtualMembers = false });
//value.Dump();
// ((nuint)5).Dump();
// ((nint)5).Dump();
Expand Down
24 changes: 12 additions & 12 deletions src/Dumpify/Descriptors/ValueProviders/MemberProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,11 @@ public IEnumerable<IValueProvider> GetMembers(Type type)
{
var properties = type.GetProperties(flags)
.Where(p => p.GetIndexParameters().Length == 0)
.Where(p => p.GetMethod is not null);
.Where(p => p.GetMethod is not null)
.Where(ShouldIncludeProperty)
.Select(p => new PropertyValueProvider(p));

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

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

members = members.Concat(providers);
members = members.Concat(properties);
}

if (_includeFields)
Expand All @@ -60,7 +57,7 @@ public IEnumerable<IValueProvider> GetMembers(Type type)
members = members.Concat(fields);
}

if(_memberFilter != null)
if (_memberFilter != null)
{
members = members.Where(member => _memberFilter(member));
}
Expand Down Expand Up @@ -92,7 +89,10 @@ public override int GetHashCode() =>
_includeVirtualMembers,
_memberFilter
).GetHashCode();

private bool IsVirtualProperty(PropertyInfo propertyInfo) =>
propertyInfo.GetAccessors().Any(accessor => accessor.IsVirtual);
}

private bool IsVirtualProperty(PropertyInfo property)
=> property.GetMethod?.IsVirtual is true;

private bool ShouldIncludeProperty(PropertyInfo propertyInfo)
=> _includeVirtualMembers || !IsVirtualProperty(propertyInfo);
}

0 comments on commit f0dee72

Please sign in to comment.