从FaultException <>获取真正的异常

use*_*353 6 .net c# exception-handling exception

我赶上了一段FaultException时间调用服务API,例如

catch(FaultException<MyCustomEx> e)
{
    // How do I get the MyCustomEx object here?
}
Run Code Online (Sandbox Code Playgroud)

我想在MyCustomEx嵌入的对象中调用一些方法.

e.getTheActualExc().getMyCustomErrorCode();
Run Code Online (Sandbox Code Playgroud)

我如何获得实际对象?

Dmi*_*nko 8

根据

http://msdn.microsoft.com/ru-ru/library/ms576199(v=vs.110).aspx

所需的财产是Detail:

try {
  ...
}
catch(FaultException<MyCustomEx> e) {
  MyCustomEx detail = e.Detail;
  ...
}
Run Code Online (Sandbox Code Playgroud)

或者,如果你必须抓住FaultException课程,你可以使用反思:

  try {
    ...
  }
  catch (FaultException e) {
    PropertyInfo pi = e.GetType().GetProperty("Detail");

    if (pi != null) {
      Object rawDetail = pi.GetValue(e); 

      MyCustomEx detail = rawDetail as MyCustomEx;

      if (detail != null) {
        ...
      }
      ...
    }
    ...
  }
Run Code Online (Sandbox Code Playgroud)