DI013

Implementation Type Mismatch

invalid `typeof` service/implementation pairs that compile but fail at runtime.

Default severity: Error · Code fix: No

Why it matters

service activation throws at runtime (`ArgumentException`/`InvalidOperationException` depending on path).

A round plug will not fit a square socket just because both are on your desk.

README problem example

public interface IRepository { }
public sealed class WrongType { }

services.AddSingleton(typeof(IRepository), typeof(WrongType));

README better pattern

public sealed class SqlRepository : IRepository { }
services.AddSingleton(typeof(IRepository), typeof(SqlRepository));

No.

Repo sample extraction

Examples pulled from the sample app

Open full sample file

Sample app safe pattern

        // ✅ GOOD: Correct implementation type
        services.AddSingleton(typeof(IRepository), typeof(SqlRepository));

        // ⚠️ BAD: WrongType does not implement IRepository
        // This will throw an ArgumentException at runtime
        services.AddSingleton(typeof(IRepository), typeof(WrongType));

Sample app warning case

        // ⚠️ BAD: WrongType does not implement IRepository
        // This will throw an ArgumentException at runtime
        services.AddSingleton(typeof(IRepository), typeof(WrongType));
        
        // ⚠️ BAD: String does not implement IRepository
        services.AddScoped(typeof(IRepository), typeof(string));
    }
}