🔧 C#: Fix an Interceptor Silently Skipping an Overloaded Call
C# interceptors attach to a call site by its exact file path, line, and column, and an InterceptsLocation attribute targets one specific call expression with an exact method signature. If the intercepted method has multiple overloads and a refactor changes which overload a call site binds to — say, a new overload becomes a better match for the same arguments — the call site’s location is unchanged but the method it now resolves to is different, so the interceptor’s recorded signature no longer matches and the compiler silently stops applying it.
🔎 The Problem
// Before: only one overload existed, interceptor generated for it
cache.Get("key"); // binds to Get(string), intercepted for caching
// A new overload is added later:
public T Get<T>(string key, T fallback) => ...;
public T Get<T>(string key) => ...; // original, still the best match here
// Call site location unchanged, overload resolution unchanged too -
// but if a stale interceptor assembly still references the OLD
// signature shape, the compiler drops it silently, no diagnostic
✅ Fix: Regenerate Interceptors Whenever Overloads Change
- Regenerate interceptors as part of your build whenever overloads of an intercepted method change, rather than caching previously-generated interceptor code, since a stale interceptor doesn’t fail loudly — it just stops being applied.
- Add a runtime or test-time assertion that the behavior an interceptor was supposed to provide (a caching layer, a logging wrapper) is actually happening for the call sites that should have it, rather than trusting that the interceptor attribute being present in generated code means it’s active.
- When adding a new overload to a method with call sites relying on interception, check whether any existing call sites’ overload resolution changes as a result, since that’s the specific condition that breaks a previously-working interceptor without any compiler diagnostic.
🚨 Why This Is Dangerous
- A silently-skipped interceptor can disable an optimization or cross-cutting behavior — caching, telemetry, validation — that the rest of the system was built assuming is always active, and because there’s no compiler error, the gap can go unnoticed until something downstream behaves as if that behavior never ran.
An interceptor activates on an exact match between its recorded call site and what that call site actually resolves to — change which overload binds there, and the interceptor doesn’t warn you, it just quietly stops being part of the program.
