I’m currently trying to create a ToString – extension method for FaultException, that can also deal with FaultException<>.
The problem I have is, that I want to include the details without using reflection.
What I currently have is:
if (ex.GetType() == typeof(FaultException<>))
{
var prop = ex.GetType().GetProperty("Detail");
if (prop == null)
return ex.ToString();
object details = prop.GetValue(ex, null);
}
Any idea how I can access the “Detail”-property without relection, if I have an object of type FaultException?
tia
Martin
Well, what would you do with the Detail if you knew it’s type anyway?
Because it’s generic you would have to have a generic method and use MethodInfo.MakeGenericMethod using the T of the FaultException as the generic parameter. Since you won’t know exactly what type it is at compile time, you’d have to code against it generically in some sense anyway.
As an example, here’s a method I wrote to log fault details:
And then I call it like so: