I am looking to get the method/action name from a task in C#. Specifically I am implementing a custom task scheduler, and would like to generate statistics on the duration a task runs, which I will then aggregate by the method running inside of the task. In the visual studio debugger you can access this and see the m_action private variable, as well as the debugger display annotation, displays it as Method={0}. Is there any way to get access to this from the Task itself?
Share
Well, you could use reflection to get at the private
m_actionfield, given aTaskvariabletask:Then get the
Nameof the method and theDeclaringType:To get the fully qualified method (
type + "." + name)…But, as soon as the task executes to completion,
m_actionisnull. I’m not sure how this would apply with TaskFactory.StartNew…