Skip to content

Commit 57129ca

Browse files
Filter sealed and abstract methods from virtual (#29)
1 parent 4ef1df1 commit 57129ca

File tree

2 files changed

+14
-23
lines changed

2 files changed

+14
-23
lines changed

src/ClassExplorer/TypeExtensions.cs

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,36 +9,25 @@ internal static class TypeExtensions
99
{
1010
public static bool IsVirtualOrAbstract(this MemberInfo member)
1111
{
12-
if (member is MethodInfo method)
13-
{
14-
return method.IsVirtual || method.IsAbstract;
15-
}
16-
17-
if (member is PropertyInfo property)
18-
{
19-
MethodInfo? accessor = property.GetFirstMethod();
20-
return accessor?.IsVirtual is true || accessor?.IsAbstract is true;
21-
}
22-
23-
if (member is EventInfo eventInfo)
12+
return member switch
2413
{
25-
MethodInfo? accessor = eventInfo.GetFirstMethod();
26-
return accessor?.IsVirtual is true || accessor?.IsAbstract is true;
27-
}
28-
29-
return false;
14+
MethodInfo method => method.IsVirtualOrAbstract(),
15+
PropertyInfo property => property.GetFirstMethod().IsVirtualOrAbstract(),
16+
EventInfo eventInfo => eventInfo.GetFirstMethod().IsVirtualOrAbstract(),
17+
_ => false,
18+
};
3019
}
3120

32-
public static bool IsVirtualOrAbstract(this MethodInfo method)
33-
=> method.IsVirtual || method.IsAbstract;
21+
public static bool IsVirtualOrAbstract(this MethodInfo? method)
22+
=> method is { IsFinal: false } and ({ IsVirtual: true } or { IsAbstract: true });
3423

3524
public static bool IsVirtual(this MemberInfo member)
3625
{
3726
return member switch
3827
{
39-
MethodInfo method => method.IsVirtual,
40-
PropertyInfo property => property.GetFirstMethod()?.IsVirtual is true,
41-
EventInfo eventInfo => eventInfo.GetFirstMethod()?.IsVirtual is true,
28+
MethodInfo method => method is { IsVirtual: true, IsFinal: false, IsAbstract: false },
29+
PropertyInfo property => property.GetFirstMethod() is { IsVirtual: true, IsFinal: false, IsAbstract: false },
30+
EventInfo eventInfo => eventInfo.GetFirstMethod() is { IsVirtual: true, IsFinal: false, IsAbstract: false },
4231
_ => false,
4332
};
4433
}

test/Find-Member.Tests.ps1

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,9 @@ Describe 'Find-Member cmdlet tests' {
105105
$_.IsVirtual -or $_.GetMethod.IsVirtual -or $_.AddMethod.IsVirtual
106106
}
107107

108-
$results | Should -Any { $_.Name -eq 'CreateNestedPipeline' }
108+
$results | Should -Any { $_.Name -eq 'Debugger' }
109+
$results | Should -Any { $_.Name -eq 'ResetRunspaceState' }
110+
$results | Should -Not -Any { $_.Name -eq 'CreateNestedPipeline' }
109111
}
110112

111113
It 'filters to abstract' {

0 commit comments

Comments
 (0)