Skip to content

Commit

Permalink
Update SerializationSettings_IgnoreType.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp committed Oct 7, 2023
1 parent 563efb6 commit 36dfbf9
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions src/Verify/Serialization/SerializationSettings_IgnoreType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,37 @@ public void ScrubMembersWithType<T>()
ScrubMembersWithType(typeof(T));

public void ScrubMembersWithType(Type type) =>
ignoredTypes[type]= ScrubOrIgnore.Scrub;
Add(type, ScrubOrIgnore.Scrub);

public void IgnoreMembersWithType<T>()
where T : notnull =>
IgnoreMembersWithType(typeof(T));

public void IgnoreMembersWithType(Type type) =>
ignoredTypes[type] = ScrubOrIgnore.Ignore;
Add(type, ScrubOrIgnore.Ignore);

void Add(Type type, ScrubOrIgnore scrubOrIgnore)
{
ignoredTypes[type] = scrubOrIgnore;

if (type.IsValueType)
{
var genericType = typeof(Nullable<>).MakeGenericType(type);
ignoredTypes[genericType] = scrubOrIgnore;
}
}

bool TryGetScrubOrIgnoreByType(Type memberType, [NotNullWhen(true)]out ScrubOrIgnore? scrubOrIgnore)
{
foreach (var member in ignoredTypes)
if (ignoredTypes.TryGetValue(memberType, out var value))
{
if (memberType.InheritsFrom(member.Key))
{
scrubOrIgnore = member.Value;
return true;
}
scrubOrIgnore = value;
return true;
}
var typeFromNullable = Nullable.GetUnderlyingType(memberType);

foreach (var member in ignoredTypes)
{
if (member.Key.IsAssignableFrom(typeFromNullable))
if (memberType.InheritsFrom(member.Key))
{
scrubOrIgnore = member.Value;
return true;
Expand Down

0 comments on commit 36dfbf9

Please sign in to comment.