Is it possible to get the method body (text) of an Action?

2.6k Views Asked by At

I have a circumstance where I have enqueued a number of Action objects and I have a threadpool working through each Action. However, if the application shuts down before the queue is empty, I would like to log what is remaining in the queue before closing.

Is it possible to get the method body from an Action object? I can see the MethodBody object from Action.Method.GetMethodBody(), but I don't see a way to get this out as a string.

Any thoughts, or am I crazy?

EDIT: One more thing; I would like to retain state information as well. That is, I would like to also retain the values of any variables used within the Action.

2

There are 2 best solutions below

0
On BEST ANSWER

Yes, you're crazy.

The code being executed has been compiled down to IL, and then JIT compiled to machine code. The text of the action is long gone.

You need to not be queuing Action. Instead, create a structure which contains the Action and the method name, and any other information you want to have logged. Make a queue of those instead of Action.

1
On

I suppose you aren't looking for the textual representation, but for a way to load and save expression trees. I did encounter that problem as well, and while I didn't use it yet, this seems promising: expression tree serialization.