I’m playing with the .NET BackgroundWorker class. As part of its functionality you can call a method named ReportProgress that allows you to pass in the percentage your background task has completed, along with an optional user parameter.
Eventually ReportProgress calls an event handler and the optional user parameter becomes the “UserState” member of the event argument.
Here’s a quick sample in case I’m not being clear:
BackgroundProcess.ReportProgress(100, new{title="complete"});
/*****later on, this method is called******/
private void myEventHandler(object sender, RunWorkerCompletedEventArgs e)
{
//e.UserState is my anonymous type defined in the call to ReportProgress(...)
}
My question is, how can I access the “title” value in my anonymous type? I assume I’ll need to use reflection but so far I’m not having great luck.
If you are using C# 4.0:
You can use reflection, but it would be big, slow and ugly. A named type would be more sensible.