I implemented a custom async method builder but I have several questions for which i cannot find a proper answer.
1) I saw that depending on how i build (Debug / Release), the state machine gets compiled into a struct or a class. Is it possible to instruct the compiler to always generate a class no matter what?
2) I saw that the void SetStateMachine(IAsyncStateMachine m) is not called at all, even though in the documentation is says that, if the state machine is a struct, it should get called with a boxed version of it.
3) I made my method builder a class. Should I make it a struct? what is the best practice for this?
4) Strangely the GetAwaiter is sometimes called AFTER the builder.SetResult method is called. Is this a normal behavior? if we check the decompiled version of a Task, we can see clearly that it request first for an awaiter and if the awaiter is not completed, it calls the machine state AwaitOnCompleted / AwaitUnsafeOnCompleted.
Environment: .Net Core 2.1
Thanks a lot!
Answer for your question 1 and 3, you can refere roslyn compiler code for asyncRewriter
selection of struct or class for rewrite is decide by EditAndContinue status
// The CLR doesn't support adding fields to structs, so in order to enable EnC in an async method we need to generate a class.
var typeKind = compilationState.Compilation.Options.EnableEditAndContinue ? TypeKind.Class : TypeKind.Struct;
Link
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With