File tree Expand file tree Collapse file tree 2 files changed +41
-1
lines changed
src/CSharpGuidelinesAnalyzer
CSharpGuidelinesAnalyzer.Test/Specs/ClassDesign
CSharpGuidelinesAnalyzer/Rules/ClassDesign Expand file tree Collapse file tree 2 files changed +41
-1
lines changed Original file line number Diff line number Diff line change @@ -319,6 +319,37 @@ static void Main(string[] args)
319319 VerifyGuidelineDiagnostic ( source ) ;
320320 }
321321
322+ [ Fact ]
323+ internal void When_static_class_is_platform_invoke_wrapper_it_must_be_skipped ( )
324+ {
325+ // Arrange
326+ ParsedSourceCode source = new TypeSourceCodeBuilder ( )
327+ . InGlobalScope ( @"
328+ static class NativeMethods
329+ {
330+ }
331+
332+ static class SafeNativeMethods
333+ {
334+ }
335+
336+ static class UnsafeNativeMethods
337+ {
338+ }
339+
340+ public class Wrapper
341+ {
342+ private static class NativeMethods
343+ {
344+ }
345+ }
346+ " )
347+ . Build ( ) ;
348+
349+ // Act and assert
350+ VerifyGuidelineDiagnostic ( source ) ;
351+ }
352+
322353 protected override DiagnosticAnalyzer CreateAnalyzer ( )
323354 {
324355 return new AvoidStaticClassAnalyzer ( ) ;
Original file line number Diff line number Diff line change @@ -41,6 +41,10 @@ public sealed class AvoidStaticClassAnalyzer : DiagnosticAnalyzer
4141 private static readonly Action < SymbolAnalysisContext > AnalyzeNamedTypeAction = context =>
4242 context . SkipEmptyName ( AnalyzeNamedType ) ;
4343
44+ [ ItemNotNull ]
45+ private static readonly ImmutableArray < string > PlatformInvokeWrapperTypeNames =
46+ ImmutableArray . Create ( "NativeMethods" , "SafeNativeMethods" , "UnsafeNativeMethods" ) ;
47+
4448 [ ItemNotNull ]
4549 public override ImmutableArray < DiagnosticDescriptor > SupportedDiagnostics => ImmutableArray . Create ( TypeRule , MemberRule ) ;
4650
@@ -56,7 +60,7 @@ private static void AnalyzeNamedType(SymbolAnalysisContext context)
5660 {
5761 var type = ( INamedTypeSymbol ) context . Symbol ;
5862
59- if ( ! type . IsStatic || type . IsSynthesized ( ) )
63+ if ( ! type . IsStatic || type . IsSynthesized ( ) || IsPlatformInvokeWrapper ( type ) )
6064 {
6165 return ;
6266 }
@@ -74,6 +78,11 @@ private static void AnalyzeNamedType(SymbolAnalysisContext context)
7478 }
7579 }
7680
81+ private static bool IsPlatformInvokeWrapper ( [ NotNull ] INamedTypeSymbol type )
82+ {
83+ return PlatformInvokeWrapperTypeNames . Contains ( type . Name ) ;
84+ }
85+
7786 private static bool TypeContainsEntryPoint ( [ NotNull ] INamedTypeSymbol type , [ NotNull ] Compilation compilation ,
7887 CancellationToken cancellationToken )
7988 {
You can’t perform that action at this time.
0 commit comments