1

いくつかの一般的なアクションを持つ複数のコントローラーがあります。私は汎用コントローラーを作成しました:

 public class FirstBaseController<TEntity> where TEntity : class, IFirst, new()
 public class SecondBaseController<TEntity> where TEntity : class, ISecond, new()

それから私はこのようなことをしたい:

 public class MyController : FirstBaseController<First>, SecondBaseController<Second>

また、C# では複数のクラスの継承が許可されていないことも知っています。これを行う別の方法を教えてもらえますか?

4

1 に答える 1

2

唯一のオプションは、基本クラスをインターフェイスに置き換え、構成を通じて再利用を実現することです。

public interface IMyFirstSetOfMethods<TEntity> { /*... */ }
public interface IMySecondSetOfMethods<TEntity> { /*... */}

public class FirstImpl 
{

}

public class SecondImpl
{
}


public class MyController : IMyFirstSetOfMethods<First> , IMySecondSetOfMethods<Second>
{
    FirstImpl myFirstImpl = new FirstImpl();
    SecondImpl mySecondImpl = new SecondImpl();

    // ... implement the methods from the interfaces by simply forwarding to the Impl classes
}
于 2012-11-28T07:27:22.843 に答える