【发布时间】:2010-09-04 21:27:50
【问题描述】:
我正在尝试像这样构建一个类...
public class IoCControllerFactory : DefaultControllerFactory
{
protected override IController GetControllerInstance(RequestContext request_context, Type controller_type)
{
// Attempt to resolve controller type.
IController resolvedController = null;
if (controller_type != null)
{
if (!typeof(IController).IsAssignableFrom(controller_type))
{
throw new ArgumentException(string.Format("Type requested is not a controller: {0}", controller_type.Name), "controller_type");
}
resolvedController = _container.Resolve(controller_type) as IController;
}
// Throw page not found if controller does not exist.
if (resolvedController == null)
{
throw new HttpException((int)HttpStatusCode.NotFound, "The requested page was not found.");
}
return resolvedController;
}
}
该方法正在尝试覆盖具有以下签名的内部虚拟方法(来自 .net 程序集...我无法修改它)
protected internal virtual IController GetControllerInstance(
RequestContext requestContext,
Type controllerType
)
当我尝试编译时,我得到以下...
'XXX.Web.Mvc.IoCControllerFactory.GetControllerInstance(System.Web.Routing.RequestContext, System.Type)': no suitable method found to override
网络上的每个人似乎都能很好地做到这一点,有什么明显的我遗漏了吗?
【问题讨论】:
-
从
GetControllerInstance的签名中删除internal?这行得通吗? -
@Jason,该方法是在 ASP.NET MVC 程序集中定义的,不能修改
-
这里是另一个扳手......如果我将它从我的原始程序集中删除,程序集就会建立!这真的难倒我。
-
你确定你在某个地方没有另一个 DefaultControllerFactory 类吗?或另一个 IController 接口?我没有看到任何其他解释
-
@wcpro:你看到我的更新了吗?我觉得我已经解决了这个问题,应该可以消除你的困惑。
标签: c# .net asp.net inversion-of-control