Skip to content

Commit

Permalink
Improve discovery of accessors
Browse files Browse the repository at this point in the history
- #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
  • Loading branch information
dougbu committed Feb 18, 2020
1 parent bd2a2b0 commit 25ab767
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions src/Microsoft.Cci.Extensions/Extensions/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using System.ComponentModel;
using System.Diagnostics.Contracts;
using System.Linq;
using Microsoft.Cci;
using Microsoft.Cci.Extensions.CSharp;

namespace Microsoft.Cci.Extensions
{
Expand Down Expand Up @@ -490,24 +490,27 @@ public static bool IsPropertyOrEventAccessor(this IMethodDefinition method)
public static AccessorType GetAccessorType(this IMethodDefinition methodDefinition)
{
if (!methodDefinition.IsSpecialName)
{
return AccessorType.None;
}

foreach (var p in methodDefinition.ContainingTypeDefinition.Properties)
// Cannot use MemberHelper.IsAdder(...) and similar due to their TypeMemberVisibility.Public restriction.
var name = methodDefinition.GetNameWithoutExplicitType();
if (name.StartsWith("add_"))
{
if (p.Getter != null && p.Getter.ResolvedMethod.InternedKey == methodDefinition.InternedKey)
return AccessorType.PropertyGetter;

if (p.Setter != null && p.Setter.ResolvedMethod.InternedKey == methodDefinition.InternedKey)
return AccessorType.PropertySetter;
return AccessorType.EventAdder;
}

foreach (var e in methodDefinition.ContainingTypeDefinition.Events)
else if (name.StartsWith("get_"))
{
if (e.Adder != null && e.Adder.ResolvedMethod.InternedKey == methodDefinition.InternedKey)
return AccessorType.EventAdder;

if (e.Remover != null && e.Remover.ResolvedMethod.InternedKey == methodDefinition.InternedKey)
return AccessorType.EventRemover;
return AccessorType.PropertyGetter;
}
else if (name.StartsWith("remove_"))
{
return AccessorType.EventRemover;
}
else if (name.StartsWith("set_"))
{
return AccessorType.PropertySetter;
}

return AccessorType.None;
Expand Down

0 comments on commit 25ab767

Please sign in to comment.