调用接口方法C#
我正在开发一个ASP.net MVC应用程序,我有一个控制器调用接口方法C#,c#,interface,virtual,C#,Interface,Virtual,我正在开发一个ASP.net MVC应用程序,我有一个控制器ExceptionController,用于显示应用程序中捕获的异常 此控制器实现一个接口IEExceptionLogger public class ExceptionController : Controller, IExceptionLogger {...} 它有一个方法void LogException(exceptiondeail exceptiondeail) 我也在ExceptionController中实现了该方法
ExceptionController
,用于显示应用程序中捕获的异常
此控制器实现一个接口IEExceptionLogger
public class ExceptionController : Controller, IExceptionLogger
{...}
它有一个方法void LogException(exceptiondeail exceptiondeail)代码>
我也在ExceptionController
中实现了该方法
void IExceptionLogger.LogException(ExceptionDetail exceptionDetail)
{...}
现在我需要从ExceptionController
的操作索引中调用方法LogException()
public ActionResult Index(ExceptionDetail exceptionDetail)
{
// Method must be called here
return View(exceptionDetail);
}
如何执行此操作?直接调用LogException()。它不工作吗?因为LogException
是,在调用它之前,必须强制转换到ieexceptionlogger
:
public ActionResult Index(ExceptionDetail exceptionDetail)
{
((IExceptionLogger)exceptionDetail).LogException();
return View(exceptionDetail);
}
要使其在不强制转换的情况下工作,请隐式实现方法:
void LogException(ExceptionDetail exceptionDetail)
{
}
LogException(exceptiondeail)代码>?我会尝试在那里调用该方法,然后看看会发生什么…是否有任何原因让您显式实现该接口?该方法可能会做什么?它必须是ActionResult
链的一部分,否则就不好了——您将无法实际返回错误视图。您可能应该使用操作过滤器--HandleErrorAttribute
将是一个很好的起点;您可以从操作筛选器上下文设置返回的视图。该方法用于在文件中记录错误当前上下文中不存在名称“LogException”请参阅“显式实现”下的链接;我无法隐式实现该方法,因此必须在调用LogException
之前强制转换它。