Getting the real exception from FaultException<>

.net, c#, exception

Solution

According to

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

The required property is `Detail`:

try {
  ...
}
catch(FaultException<MyCustomEx> e) {
  MyCustomEx detail = e.Detail;
  ...
}

Or, if you have to catch `FaultException` class, you can use Reflection:

  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) {
        ...
      }
      ...
    }
    ...
  }

Problem

I catch a `FaultException` while calling a service API, e.g. ``` catch(FaultException<MyCustomEx> e) { // How do I get the MyCustomEx object here? } ``` I want to call some method on the `MyCustomEx` object embedded inside. ``` e.getTheActualExc().getMyCustomErrorCode(); ``` How do I get the actual object?

Original source