2014-05-08 82 views
-2

我正在开发一个ASP.net MVC应用程序,我有一个控制器ExceptionController用于显示在我的应用程序中捕获的异常。调用接口方法C#

这种控制器实现一个接口IExceptionLogger

public class ExceptionController : Controller, IExceptionLogger 
    {...} 

具有一个方法void LogException(ExceptionDetail exceptionDetail);

我已经实现了ExceptionController太内部的方法。

void IExceptionLogger.LogException(ExceptionDetail exceptionDetail) 
{...} 

现在我需要从ExceptionController的作用Index调用该方法LogException()

public ActionResult Index(ExceptionDetail exceptionDetail) 
    { 
     // Method must be called here 
     return View(exceptionDetail); 
    } 

我该怎么做?

+0

'LogException(exceptionDetail);' ? – zerkms

+0

我会尝试调用那里的方法,看看会发生什么... –

+1

是否有任何原因明确地实现了接口? –

回答

4

因为LogExceptionimplemented explicitly,你必须调用它之前投给IExceptionLogger

public ActionResult Index(ExceptionDetail exceptionDetail) 
{ 
    ((IExceptionLogger)exceptionDetail).LogException(); 

    return View(exceptionDetail); 
} 

度过,即使没有铸造工作,隐式实现方法:

void LogException(ExceptionDetail exceptionDetail) 
{ 
} 
+0

它的工作..你能解释它是如何工作的 – RandomUser

+0

请参阅下面的链接*明确实施*; – MarcinJuraszek

+0

我无法实现该方法隐含 – RandomUser

0

直接调用LogException()。它不工作吗?

+0

名称'LogException'在当前上下文中不存在 – RandomUser