編輯: 也許這是一個更清晰,更對這個問題的角度制定:差異在返回類型
在一些通用接口IInterface<T>
,我想回到一個泛型類型的對象,其中一個的類型參數應該是IInterface<T>
的實現。
public class OtherType<T> {}
public interface IInterface<T>
{
OtherType<IInterface<T>> Operation();
}
public class Impl : IInterface<int>
{
public OtherType<IInterface<int>> Operation()
{
return new OtherType<Impl>();
}
}
由於Impl
工具IInterface<int>
,它似乎是合理的,我認爲我可以使用這種方式。然而,似乎我不能,我得到的編譯器錯誤
無法轉換表達式類型
OtherType<Impl>
到返回式OtherType<IInterface<int>>
請問您能否將您非常漫長而令人困惑的類名轉換爲A,B,C等類似的東西? – Sebastian 2013-03-27 08:39:14
@CarlPett:你是否一直關注Eric Lippert在Monads上的精彩博客系列? http://ericlippert.com/2013/02/21/monads-part-one/看來你正在努力重新發明這個構造,並且可以對一般機制有所瞭解。 – 2013-03-27 08:55:16
@PieterGeerkens:我沒有,謝謝你的提示!看起來很有趣。我只有時間閱讀前四部分,但到目前爲止,我真的不知道如何在這裏應用它? – carlpett 2013-03-27 16:37:17