I'm calling a method which I do not own that returns a 'Task' object. I want to return that object from my method, but I also need to override the Exception property of that object. If the underlying task catches an exception, and my user retrieves that exception via the Exception property, I need to tweak the data they see in the returned exception.
I'm wondering if there is a better solution to this problem than to do a copy via reflection. I don't really want to create a new instance, but I can't find a way to avoid it.
Another possibility is to wrap the Task object in my own class. But then my method won't be returning a Task (or derived from) object, which may cause difficulty downstream. Not to mention the volume of code to implement all of Task's public members and do nothing but call the base versions.
The code below doesn't work, obviously, but conceptually it's what I want to do:
public class MyTask : Task
{
override Exception Exception { get { return Tweak(base.Exception); } }
}
public MyTask MyMethod()
{
Task t = MethodIDontOwnThatReturnsTask();
return (MyTask) t; // Can I do this type of operation without copying t?
}
Rather than trying to mutate the existing task, or change the way it's seen, just create a new Task
(which is what ContinueWith
will do). They're not really expensive:
public Task MyMethod()
{
return MethodIDontOwnThatReturnsTask()
.ContinueWith(t => { throw Tweak(t.Exception); }
, TaskContinuationOptions.OnlyOnFaulted);
}