@@ -42,14 +42,12 @@ public sealed class TestContextPropertyUsageAnalyzer : DiagnosticAnalyzer
4242 "TestDisplayName" ,
4343 "DataRow" ,
4444 "DataConnection" ,
45- "TestName" ,
46- "ManagedMethod" ) ;
45+ "TestName" ) ;
4746
4847 // Properties that cannot be accessed in assembly initialize or assembly cleanup
4948 private static readonly ImmutableHashSet < string > RestrictedInAssemblyMethods = ImmutableHashSet . Create (
5049 StringComparer . Ordinal ,
51- "FullyQualifiedTestClassName" ,
52- "ManagedType" ) ;
50+ "FullyQualifiedTestClassName" ) ;
5351
5452 /// <inheritdoc />
5553 public override void Initialize ( AnalysisContext context )
@@ -68,28 +66,39 @@ public override void Initialize(AnalysisContext context)
6866 return ;
6967 }
7068
71- context . RegisterOperationAction ( context => AnalyzePropertyReference ( context , testContextSymbol , assemblyInitializeAttributeSymbol , assemblyCleanupAttributeSymbol , classInitializeAttributeSymbol , classCleanupAttributeSymbol ) , OperationKind . PropertyReference ) ;
69+ IPropertySymbol ? propertiesSymbol = testContextSymbol . GetMembers ( "Properties" ) . OfType < IPropertySymbol > ( ) . FirstOrDefault ( ) ;
70+
71+ context . RegisterOperationAction ( context => AnalyzePropertyReference ( context , testContextSymbol , propertiesSymbol , assemblyInitializeAttributeSymbol , assemblyCleanupAttributeSymbol , classInitializeAttributeSymbol , classCleanupAttributeSymbol ) , OperationKind . PropertyReference ) ;
7272 } ) ;
7373 }
7474
7575 private static void AnalyzePropertyReference (
7676 OperationAnalysisContext context ,
7777 INamedTypeSymbol testContextSymbol ,
78+ IPropertySymbol ? propertiesSymbol ,
7879 INamedTypeSymbol assemblyInitializeAttributeSymbol ,
7980 INamedTypeSymbol assemblyCleanupAttributeSymbol ,
8081 INamedTypeSymbol classInitializeAttributeSymbol ,
8182 INamedTypeSymbol classCleanupAttributeSymbol )
8283 {
8384 var propertyReference = ( IPropertyReferenceOperation ) context . Operation ;
8485
85- // Check if the property is a TestContext property
86- if ( ! SymbolEqualityComparer . Default . Equals ( propertyReference . Property . ContainingType , testContextSymbol ) )
86+ string ? propertyName ;
87+ if ( SymbolEqualityComparer . Default . Equals ( propertyReference . Property . ContainingType , testContextSymbol ) )
88+ {
89+ // Direct typed property access, e.g. testContext.TestName.
90+ propertyName = propertyReference . Property . Name ;
91+ }
92+ else if ( TryGetRestrictedPropertiesIndexerKey ( propertyReference , propertiesSymbol ) is { } key )
93+ {
94+ // Indirect access through the string-keyed bag, e.g. testContext.Properties["TestName"].
95+ propertyName = key ;
96+ }
97+ else
8798 {
8899 return ;
89100 }
90101
91- string propertyName = propertyReference . Property . Name ;
92-
93102 // Check if we're in a forbidden context
94103 if ( context . ContainingSymbol is not IMethodSymbol containingMethod )
95104 {
@@ -116,6 +125,38 @@ private static void AnalyzePropertyReference(
116125 }
117126 }
118127
128+ private static string ? TryGetRestrictedPropertiesIndexerKey ( IPropertyReferenceOperation propertyReference , IPropertySymbol ? propertiesSymbol )
129+ {
130+ // We only care about indexer access such as testContext.Properties["TestName"].
131+ if ( propertiesSymbol is null
132+ || ! propertyReference . Property . IsIndexer
133+ || propertyReference . Arguments . Length != 1
134+ || propertyReference . Instance is not IPropertyReferenceOperation instanceReference
135+ || ! IsTestContextPropertiesReference ( instanceReference . Property , propertiesSymbol ) )
136+ {
137+ return null ;
138+ }
139+
140+ // The key must be a compile-time constant string.
141+ return propertyReference . Arguments [ 0 ] . Value . ConstantValue is { HasValue : true , Value : string key }
142+ ? key
143+ : null ;
144+ }
145+
146+ private static bool IsTestContextPropertiesReference ( IPropertySymbol property , IPropertySymbol propertiesSymbol )
147+ {
148+ // Match TestContext.Properties as well as overrides of it in derived contexts.
149+ for ( IPropertySymbol ? current = property ; current is not null ; current = current . OverriddenProperty )
150+ {
151+ if ( SymbolEqualityComparer . Default . Equals ( current , propertiesSymbol ) )
152+ {
153+ return true ;
154+ }
155+ }
156+
157+ return false ;
158+ }
159+
119160 private static string GetMethodType ( bool isAssemblyInitialize , bool isAssemblyCleanup , bool isClassInitialize , bool isClassCleanup )
120161 => ( isAssemblyInitialize , isAssemblyCleanup , isClassInitialize , isClassCleanup ) switch
121162 {
0 commit comments