我在玩捣乱任务内部状态机状态的想法,但我很难找到一种方法来实际访问我内部的状态机参考任务方法。获取实际参考任务状态机
class Test
{
async Task IndexAsync()
{
var nottheactualtype = GetType(); //This references the "Test" class, but this operation is actually located in the nested state machine class named "IndexAsync", in the method "MoveNext()".
var actualcalledmethod = new StackTrace().GetFrame(0).GetMethod(); //This shows the actual method currently being run: IndexAsync.MoveNext().
//But how do I get the reference to my current IndexAsync class?
}
}
我该如何访问当前运行的生成状态机的引用?
是否必须是方法本身?我有很多代码在https://github.com/jskeet/DemoCode/tree/master/Abusing%20CSharp/Code/FunWithAwaiters做这种事情可能会帮助你,但它并不倾向于获得状态机器*内*异步方法。 –
我相信如此。在异步方法中的某个位置,我想查看状态机中的哪个状态正在运行。除此之外还有更多,但其实质是我想访问当前正在运行的状态机的成员。我可以通过反射访问成员,并且可以访问生成的类的类型。但我似乎无法找到访问实际引用的方法。 – Micael
好吧,我想我已经想出了一些东西,但我需要尝试一下...... –