Skip to content

Commit 336d730

Browse files
committed
Improve discovery of accessors
- #2066 - handle cases where accessors are overridden but related events or properties are not - use method prefixes rather than focus on events and properties defined in current type
1 parent f492130 commit 336d730

File tree

1 file changed

+17
-14
lines changed

1 file changed

+17
-14
lines changed

src/Microsoft.Cci.Extensions/Extensions/TypeExtensions.cs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
using System.ComponentModel;
88
using System.Diagnostics.Contracts;
99
using System.Linq;
10-
using Microsoft.Cci;
10+
using Microsoft.Cci.Extensions.CSharp;
1111

1212
namespace Microsoft.Cci.Extensions
1313
{
@@ -490,24 +490,27 @@ public static bool IsPropertyOrEventAccessor(this IMethodDefinition method)
490490
public static AccessorType GetAccessorType(this IMethodDefinition methodDefinition)
491491
{
492492
if (!methodDefinition.IsSpecialName)
493+
{
493494
return AccessorType.None;
495+
}
494496

495-
foreach (var p in methodDefinition.ContainingTypeDefinition.Properties)
497+
// Cannot use MemberHelper.IsAdder(...) and similar due to their TypeMemberVisibility.Public restriction.
498+
var name = methodDefinition.GetNameWithoutExplicitType();
499+
if (name.StartsWith("add_"))
496500
{
497-
if (p.Getter != null && p.Getter.ResolvedMethod.InternedKey == methodDefinition.InternedKey)
498-
return AccessorType.PropertyGetter;
499-
500-
if (p.Setter != null && p.Setter.ResolvedMethod.InternedKey == methodDefinition.InternedKey)
501-
return AccessorType.PropertySetter;
501+
return AccessorType.EventAdder;
502502
}
503-
504-
foreach (var e in methodDefinition.ContainingTypeDefinition.Events)
503+
else if (name.StartsWith("get_"))
505504
{
506-
if (e.Adder != null && e.Adder.ResolvedMethod.InternedKey == methodDefinition.InternedKey)
507-
return AccessorType.EventAdder;
508-
509-
if (e.Remover != null && e.Remover.ResolvedMethod.InternedKey == methodDefinition.InternedKey)
510-
return AccessorType.EventRemover;
505+
return AccessorType.PropertyGetter;
506+
}
507+
else if (name.StartsWith("remove_"))
508+
{
509+
return AccessorType.EventRemover;
510+
}
511+
else if (name.StartsWith("set_"))
512+
{
513+
return AccessorType.PropertySetter;
511514
}
512515

513516
return AccessorType.None;

0 commit comments

Comments
 (0)