Stephan Baltzer
Stephan Baltzer

Reputation: 273

How to ignore Property Getter and Setter in C# with Roslyn

I'm enumerating through IMethodSymbols by getting GetMembers() and filtering with Linq. Only thing i can't filter out are property getters and setters methods - any suggestions?

For each property i also get a get_[PropertyName] and set_[PropertyName] Method.

TypeSymbol.GetMembers().Where(s => 
s.Kind == SymbolKind.Method && 
s.DeclaredAccessibility == Accessibility.Public && 
!s.IsImplicitlyDeclared && 
!s.IsVirtual))

I thought this would filter out the getters and setters...

Upvotes: 1

Views: 725

Answers (2)

meziantou
meziantou

Reputation: 21337

You need to cast the symbol to IMethodSymbol, then you can use MethodKind:

member is IMethodSymbol method &&
(method.MethodKind == MethodKind.PropertyGet || method.MethodKind == MethodKind.PropertySet)

Upvotes: 2

Stephan Baltzer
Stephan Baltzer

Reputation: 273

I'm now using a string filter like


.Name.StartsWith("get_")

but i hope there's a better solution.

Upvotes: 0

Related Questions