简体   繁体   English

如何使用 Roslyn 在 C# 中忽略属性 Getter 和 Setter

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

I'm enumerating through IMethodSymbols by getting GetMembers() and filtering with Linq.我通过获取 GetMembers() 和使用 Linq 过滤来枚举 IMethodSymbols。 Only thing i can't filter out are property getters and setters methods - any suggestions?我唯一无法过滤掉的是属性 getter 和 setter 方法 - 有什么建议吗?

For each property i also get a get_[PropertyName] and set_[PropertyName] Method.对于每个属性,我还得到一个 get_[PropertyName] 和 set_[PropertyName] 方法。

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...我认为这会过滤掉 getter 和 setter ......

You need to cast the symbol to IMethodSymbol , then you can use MethodKind :您需要将符号转换为IMethodSymbol ,然后您可以使用MethodKind

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

I'm now using a string filter like我现在正在使用一个字符串过滤器


.Name.StartsWith("get_")

but i hope there's a better solution.但我希望有更好的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM