0

I have a method with the following signature:

public IDisposable RegisterSubscriber<T>(T subscriber)
    where T : MessageBusSubscriber<IMessageBusEvent>
{
    
}

I am trying to call this from a MessageBusSubscriber<MoveEvent>:

public class MoveSubscriber : MessageBusSubscriber<MoveEvent>
{ 
    private readonly IDisposable _subscription;

    public MoveSubscriber(MessageBus bus)
    {
        _subscription = bus.RegisterSubscriber(this);
    }
}

public class MoveEvent : IMessageBusEvent {}

public interface IMessageBusEvent { }

I get the following error: CS0311 The type 'MoveSubscriber' cannot be used as type parameter 'T' in the generic type or method 'MessageBus.RegisterSubscriber<T>(T)'. There is no implicit reference conversion from 'MoveSubscriber' to 'MessageBusSubscriber<IMessageBusEvent>'.

Am I missing something or should this work, I was under the impression that a class that uses a generic restricted to a certain interface should allow a class that implements said interface in it's place.

e.g A MessageBusSubscriber<MoveEvent> is a MessageBusSubscriber<IMessageBusEvent> so should be allowed.

Thanks

ScottKane
  • 47
  • 1
  • 9
  • 3
    No, `MessageBusSubscriber` is not `MessageBusSubscriber`. – Guru Stron Jul 01 '20 at 14:04
  • 3
    https://stackoverflow.com/q/8925400/11683? – GSerg Jul 01 '20 at 14:04
  • You can see that too : https://stackoverflow.com/questions/2033912/c-sharp-variance-problem-assigning-listderived-as-listbase –  Jul 01 '20 at 14:07
  • It's a bit of a special case here, but @GSerg is probably right. This is an instance of the Lion/Giraffe/Animal problem. – PMF Jul 01 '20 at 14:07

0 Answers0