Skip to content

Commit c2e3ff4

Browse files
C#: Fix SearchResult.Found using dynamic dispatch instead of reflection
SearchResult.Found used GetMethod reflection to call WithMarkers on concrete tree types. This silently returned the same instance when GetMethod failed, breaking PreconditionsCheckTest assertions that were previously masked by Gradle build cache. Replace the fragile reflection with dynamic dispatch, which reliably resolves WithMarkers on any concrete type at runtime.
1 parent 71809c7 commit c2e3ff4

1 file changed

Lines changed: 1 addition & 2 deletions

File tree

rewrite-csharp/csharp/OpenRewrite/Core/SearchResult.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ public sealed class SearchResult(Guid id, string? description) : Marker, IRpcCod
3333
public static T Found<T>(T tree, string? description = null) where T : J
3434
{
3535
var newMarkers = tree.Markers.Add(new SearchResult(Guid.NewGuid(), description));
36-
var withMarkers = tree.GetType().GetMethod("WithMarkers", [typeof(Markers)]);
37-
return withMarkers != null ? (T)withMarkers.Invoke(tree, [newMarkers])! : tree;
36+
return (T)(object)((dynamic)tree).WithMarkers(newMarkers);
3837
}
3938

4039
public void RpcSend(SearchResult after, RpcSendQueue q)

0 commit comments

Comments
 (0)