似乎无法使通用协方差/逆变起作用



假设我有以下代码:

public interface IBaseMessage { }
public interface IMessageProcessor<in T> where T : IBaseMessage {
            void Process(T msg);
        }
public class RRMessage : IBaseMessage {
          //something here
        }
public class BaseMessageProcessor {
         //something here
        }
public class RRMessageProcessor : BaseMessageProcessor, IMessageProcessor<RRMessage> {
            public void Process(RRMessage msg) {
                Console.WriteLine("Processed RRMessage");
            }
        }
public Dictionary<Type, IMessageProcessor<IBaseMessage>> MessageProcessors = new Dictionary<Type, IMessageProcessor<IBaseMessage>>();
[Test]
public void Test1() {
     var msgProcessor = new RRMessageProcessor();
     MessageProcessors.Add(typeof(RRMessage), msgProcessor);
     }

我为接口 IMessageProcessor 启用了逆变。为什么MessageProcessors.Add(typeof(RRMessage), msgProcessor);会导致编译时错误:

参数 2:无法从"RRMessageProcessor"转换为 'IMessageProcessor'

似乎它应该能够转换,因为RRMessageProcessor:IMessageProcessor<RRMessage:IBaseMessage>>我怎样才能让它工作?

IMessageProcessor<in T>

T中是逆变的,而不是协变的。这意味着允许以下内容:

class RRSubtype : RRMessage {}
IMessageProcessor<RRSubtype> p = new RRMessageProcessor();

您尝试执行的操作在静态上是不安全的,因为它将允许您执行以下操作:

class NotRRMessage : IBaseMessage { }
IMessageProcessor<IBaseMessage> p = new RRMessageProcessor();
p.Process(new NotRRMessage());

因此,您需要动态维护安全性,您似乎正在尝试使用字典Type -> Processor。因此,您可以创建一个不安全的包装器类型:

public class ProcessorWrapper<T> : IMessageProcessor<IBaseMessage> {
    private readonly IMessageProcessor<T> inner;
    public ProcessorWrapper(IMessageProcessor<T> inner) { this.inner = inner; }
    public void Process(IBaseMessage msg)
    {
        if(msg is T) { inner.Process((T)msg); }
        else throw new ArgumentException("Invalid message type");
    }
}

然后,您可以使用这些包装器构建列表,同时根据需要维护内部处理器的类型,例如

public Dictionary<Type, IMessageProcessor<IBaseMessage>> MessageProcessors = new Dictionary<Type, IMessageProcessor<IBaseMessage>>();
MessageProcessors.Add(new ProcessorWrapper<RRMessage>(new RRMessageProcessor());

相关内容

  • 没有找到相关文章

最新更新