サンプルプログラムは:このジェネリックスの動作について説明し、回避策がある場合はこれを説明できますか?以下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GenericsTest
{
class Program
{
static void Main(string[] args)
{
IRetrievable<int, User> repo = new FakeRepository();
Console.WriteLine(repo.Retrieve(35));
}
}
class User
{
public int Id { get; set; }
public string Name { get; set; }
}
class FakeRepository : BaseRepository<User>, ICreatable<User>, IDeletable<User>, IRetrievable<int, User>
{
// why do I have to implement this here, instead of letting the
// TKey generics implementation in the baseclass handle it?
//public User Retrieve(int input)
//{
// throw new NotImplementedException();
//}
}
class BaseRepository<TPoco> where TPoco : class,new()
{
public virtual TPoco Create()
{
return new TPoco();
}
public virtual bool Delete(TPoco item)
{
return true;
}
public virtual TPoco Retrieve<TKey>(TKey input)
{
return null;
}
}
interface ICreatable<TPoco> { TPoco Create(); }
interface IDeletable<TPoco> { bool Delete(TPoco item); }
interface IRetrievable<TKey, TPoco> { TPoco Retrieve(TKey input); }
}
このサンプル・プログラムでは、私の実際のプログラムが使用するインターフェースを表し、(FakeRepository
でコメントアウト)私がいる問題を示しています。このメソッド呼び出しは、基本クラス(実際の例では95%を扱うことができる)によって一般的に処理され、TKeyの型を明示的に指定することで子クラスのオーバーライドが可能です。 IRetrievableにどのようなパラメータ制約が使用されているかは重要ではないようですが、基本クラスに渡るメソッド呼び出しを決して得ることはできません。
また、誰かがこの種の動作を実装して、私が最終的に探している結果を得る別の方法を見ることができれば、私はそれを見ることに非常に興味があります。
思考?
残念ながら私は元の例にこれを含めていませんでした。私のFakeRepositoryはいくつかのIRetrievableインターフェースを持っていて、すべて同じTPocoであるがTKeyは異なっています。 –