4

我一直认为 C# 编译器能够在以下情况下推断类型参数:

class Program
{
    static void Main(string[] args)
    {
        IMessageBus messageBus = null;

       //Here the compiler nags "type params for Publish cannot be inferred from the usage.. .."
        messageBus.Publish(new CorrectorAdded(10));
    }
}

public interface IEvent<out TPayload>
{
    TPayload Payload { get; }
}

public abstract class EventBase<TPayload> : IEvent<TPayload>
{
    public TPayload Payload { get; private set; }

    protected EventBase(TPayload payload)
    {
        Payload = payload;
    }
}

public interface IMessageBus
{
    void Publish<TEvent, TPayload>(TEvent @event) where TEvent : IEvent<TPayload>;

    IDisposable Subscribe<TEvent, TPayload>(Action<TPayload> listener) where TEvent : IEvent<TPayload>;
}

public class CorrectorAdded : EventBase<CorrectorAddedArgs>
{
    public CorrectorAdded(CorrectorAddedArgs payload) : base(payload)
    {
    }

    public CorrectorAdded(int correctorId) : this(new CorrectorAddedArgs(correctorId))
    {
    }
}

public class CorrectorAddedArgs
{
    public int CorrectorId { get; private set; }

    public CorrectorAddedArgs(int correctorId)
    {
        CorrectorId = correctorId;
    }
}

关于为什么会发生这种情况以及如何让类型推断在这种情况下起作用的任何想法?

谢谢。

4

2 回答 2

3

接口签名publish定义了 2 个约束:

void Publish<TEvent, TPayload>(TEvent @event) where TEvent : IEvent<TPayload>;
  1. TEvent这将是方法参数的签名
  2. TPayload哪些状态TEvent必须实现接口IEvent<TPayload>

如果 Publish 方法只有 的约束TEvent,则编译器可以推断用法,因为方法的签名已经定义了类型约束,即

void Publish<TEvent>(TEvent @event);

然后上面将允许在没有类型约束的情况下使用方法调用,即

messageBus.Publish( new CorrectorAdded( 10 ) );
// would be the same as
messageBus.Publish<CorrectorAdded>( new CorrectorAdded( 10 ) );

但是,由于接口定义了第二个约束,编译器不知道意图是什么,TPayload因为事件可以实现任意数量的IEvent<TPayload>'。但是,如果方法签名确实包含 的类型,TPayload那么编译器可以实际推断出约束,即

void Publish<TEvent, TPayload>(TEvent @event, TPayload payload) where TEvent : IEvent<TPayload>;

然后可以在没有类型约束的情况下调用 then 方法:

messageBus.Publish( new CorrectorAdded( 10 ), new FooThatImplementsTPayload() );
于 2013-11-09T20:29:42.203 回答
0

对于那些仍然对替代 API 的外观感到好奇的人,这是我想出的:

class Program
{
    static void Main(string[] args)
    {
        IMessageBus messageBus = null;

        messageBus
            .Event<CorrectorAdded>()
            .Subscribe(eventArgs => { /*skipped*/ });

        //skipped

        messageBus
            .Event<CorrectorAdded>()
            .Publish(new CorrectorAddedArgs(1));
    }
}

public abstract class EventBase
{
    //skipped
}

public abstract class EventBase<TPayload> : EventBase
{
    public IDisposable Subscribe(Action<TPayload> listener)
    {
        //skipped
    }

    public void Publish(TPayload payload)
    {
        //skipped
    }
}

public class CorrectorAdded : EventBase<CorrectorAddedArgs>
{
}

public class CorrectorAddedArgs
{
    public int CorrectorId { get; private set; }

    public CorrectorAddedArgs(int correctorId)
    {
        CorrectorId = correctorId;
    }
}

public interface IMessageBus
{
    TEvent Event<TEvent>() where TEvent : EventBase, new();
}
于 2013-11-10T10:11:05.980 回答