6

この一般的な制約を機能させるのに問題があります。

以下に2つのインターフェースがあります。

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 );
}
4

4 に答える 4

1

インターフェースを次のように変更できます (これは、私にはすっきりと見えます)。

public interface ICommandHandler<in TCommand, TResult> where TCommand : ICommand
{
    ICommandResult<TResult> Execute( TCommand command );
}

ICommandResult<TResult>または、型パラメーターをジェネリック パラメーター リストに追加することもできます。

public interface ICommandHandler<in TCommand, TCommandResult, TResult> 
    where TCommand : ICommand
    where TCommandResult: ICommandResult<TResult>
{
    TCommandResult Execute( TCommand command );
}
于 2013-03-18T10:10:45.417 に答える
0

これはそれを行う必要があります:

public interface ICommandHandler<in TCommand, out TResult>  
    where TCommand : ICommand
    where TResult : ICommandResult<TResult>
{
    TResult Execute( TCommand command );
}   
于 2013-03-18T10:10:43.717 に答える
0

ICommandHandler に 3 番目のジェネリック型パラメーターを追加できます。

public interface ICommandResult<out TResult>
{
    TResult Result { get; }
}

public interface ICommandHandler<in TCommand, TResult, TResultType>  
                                                        where TCommand : ICommand
                                                        where TResult : ICommandResult<TResultType>
{
    TResult Execute( TCommand command );
}
于 2013-03-18T10:08:27.810 に答える
0

うーん、あなたの 2 番目のインターフェイスはもっとこのように見えるべきではありませんか?

public interface ICommandHandler<in TCommand, ICommandResult<TResult>>
    where TCommand : ICommand
{
    TResult Execute(TCommand command);
}
于 2013-03-18T10:08:46.307 に答える