通常のインターフェース:
public interface IComputation
{
void Reset();
float GetValue1();
float GetValue2();
}
汎用インターフェース:
public interface IComputation<T> : IComputation where T : IComputation
{
T Proxy { get; set; }
}
クラスについては次のとおりです。
public abstract class Computation<T> : IComputation<T> where T : IComputation
{
public T Proxy { get; set; }
}
クラス「ComputationCache」は「装飾された」計算です:
internal abstract class ComputationCache<T> : IComputation where T : IComputation
{
public T Proxy { get; set; }
public float GetValue1()
{
bool isCached = //check cache.
if(!isCached)
{
//compute value
float value1 = Proxy.GetValue1();
//update cache
return value;
}
}
}
装飾された計算を初期化するために、次のことを試しました。
public ComputationCache(IComputation<T> proxy)
{
Proxy = (T) proxy;
proxy.Proxy = this;
}
...次のエラーが発生します":
ソース タイプ 'ComputationCache' をターゲット タイプ 'T' に変換できません。
誰かが使用する方が良いかどうかについてコメントできますか:
ComputationCache<T> : IComputation where T : IComputation
対
ComputationCache<T> : IComputation<T> where T : IComputation