MissingSemicolon
MissingSemicolon

Reputation: 1

The type '...' cannot be used as type parameter '...' in the generic type or method

I've got some already existing generic code:

    public interface IRepositoryBase {}

    public interface IExtendedRepositoryBase {}


    public abstract class FooServiceBase<TRepository>
        where TRepository : IRepositoryBase    {}


    public abstract class BarServiceBase<TRepository> : FooServiceBase<TRepository> 
        where TRepository : IExtendedRepositoryBase, IRepositoryBase    {}

Then I try to define a domain-specific service class with a domain-specific repository like this:

    public interface ISpecificRepository : IExtendedRepositoryBase   {}

    public class SpecificService : FooServiceBase<ISpecificRepository>  {}

On the SpecificService class I got the error saying:

The type ISpecificRepository cannot be used as type parameter 'TRepository' in the generic type or method 'FooServiceBase'. There is no implicit reference conversion from 'ISpecificRepository' to 'IRepositoryBase'.

I know the problem is that FooServiceBase expects TRepository (in that case ISpecificRepository) to inherit from IRepositoryBase directly.

Is there any way to make the code works without modifying the generic code (at most I can modify BarServiceBase)?

Upvotes: 0

Views: 2896

Answers (1)

pm100
pm100

Reputation: 50110

lets look see what we have

public interface IExtendedRepositoryBase {}
public interface ISpecificRepository : IExtendedRepositoryBase   {}

so ISpecificRepository has no relationship to IRepositoryBase at all, hence the complaint when you do

  public class SpecificService : FooServiceBase<ISpecificRepository>  {}

since FooService requires that the type supllied must implement IRepositoryBase

Maybe you need

public interface IExtendedRepositoryBase : IRepositoryBase    {}
public interface ISpecificRepository : IExtendedRepositoryBase   {}

Upvotes: 1

Related Questions