C# 通用控制器

C# 通用控制器,c#,asp.net-mvc-3,generics,multiple-inheritance,C#,Asp.net Mvc 3,Generics,Multiple Inheritance,我有多个控制器,它们有一些共同的动作。我制作了通用控制器: public class FirstBaseController<TEntity> where TEntity : class, IFirst, new() public class SecondBaseController<TEntity> where TEntity : class, ISecond, new() 公共类FirstBaseController,其中tenty:class、IFirst、n

我有多个控制器,它们有一些共同的动作。我制作了通用控制器:

 public class FirstBaseController<TEntity> where TEntity : class, IFirst, new()
 public class SecondBaseController<TEntity> where TEntity : class, ISecond, new()
公共类FirstBaseController,其中tenty:class、IFirst、new()
公共类SecondBaseController,其中tenty:class、IsSecond、new()
然后我想做这样的事情:

 public class MyController : FirstBaseController<First>, SecondBaseController<Second>
公共类MyController:FirstBaseController、SecondBaseController

我知道C#中不允许多类继承。您能告诉我另一种方法吗?

唯一的选择是用接口替换基类,并通过组合实现重用:

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
}
公共接口IMyFirstSetOfMethods{/*…*/}
公共接口IMySecondSetOfMethods{/*…*/}
公共类第一impl
{
}
公共类二次导入
{
}
公共类MyController:IMyFirstSetOfMethods、IMySecondSetOfMethods
{
FirstImpl myFirstImpl=新的FirstImpl();
SecondImpl mySecondImpl=new SecondImpl();
//…通过简单地转发到Impl类,从接口实现方法
}

您可以使用通用接口。在C#中允许多接口继承和实现。您还可以从继承更改为组合。因此,控制器不是“一个”而是“使用一个”感谢大家的帮助