我在獲取該通用約束條件時遇到了一些麻煩。定義值類型和引用類型的通用接口類型約束條件
我有兩個接口如下。
我想限制ICommandHandlers TResult類型只使用實現ICommandResult的類型,但是ICommandResult有自己的需要提供的約束。 ICommandResult可能會從其Result屬性返回值或引用類型。我錯過了明顯的東西嗎?謝謝。
public interface ICommandResult<out TResult>
{
TResult Result { get; }
}
public interface ICommandHandler<in TCommand, TResult> where TCommand : ICommand
where TResult : ICommandResult<????>
{
TResult Execute(TCommand command);
}
我看不到這與引用類型和值類型有什麼關係 – 2013-03-18 10:20:49
ICommandResult Result屬性可以是值類型或引用類型。 – Matt 2013-03-18 10:24:13
所以可以使用任何其他泛型類型,除非受到「where T:class/struct」 ? – 2013-03-18 10:28:11