How do you generate a "yield return null;" for an IEnumerator method? #897
-
Hi! I'm trying to use Mono.Cecil to duplicate some methods based on custom Attributes to make these copies use IEnumerator (so I can use them as Unity Coroutines). I can load the original libraries with Cecil, find the methods, duplicate them, etc. The problem is: I have to replace some placeholder method calls with a yield return null;, which seems to require generating more IL code than I expected. Does anyone know how that could be represented as IL code with Mono.Cecil? I've been using https://sharplab.io/ to understand the pattern, but it seems like it could be more complex. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
So, something like: [CoroutineMethod]
private int TestMethod(AssetLoaderContext assetLoaderContext, int a, int b) {
if (assetLoaderContext.CheckMainThreadStall()) {
}
return a + b;
} Could be duplicated to: private IEnumerator TestMethod_Coroutine(Stack result, AssetLoaderContext assetLoaderContext, int a, int b) {
if (assetLoaderContext.CheckMainThreadStall()) {
yield return null;
}
result.Push(a + b);
} |
Beta Was this translation helpful? Give feedback.
-
There is no direct IL equivalent to I'm afraid this transformation is not exactly straightforward (especially starting from the low-level IL representation), but you can take a look at the Roslyn implementation if you'd like to replicate it. Comments there can be insightful, like this one: // yield return expression;
// is translated to
// this.current = expression;
// this.state = <next_state>;
// return true;
// <next_state_label>: ;
// <hidden sequence point>
// this.state = finalizeState; |
Beta Was this translation helpful? Give feedback.
There is no direct IL equivalent to
yield return
. Methods that useyield
are rewritten by the compiler as finite state machines, with state changes onyield
statements.I'm afraid this transformation is not exactly straightforward (especially starting from the low-level IL representation), but you can take a look at the Roslyn implementation if you'd like to replicate it. Comments there can be insightful, like this one: