Support for delegates with different method signatures #12
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Context
#11 added basic support for delegates by detecting calls to the special constructor they use, and hardcoding
_ => defaultwhich works for any non-void signature with 1 parameter.It works for what we need (currently there's only 1 delegate consumed across our code), but it would be nice to support all delegate signatures and to assert that the signature is still correct.
Approach
My approach to this PR was to run the DelegateWithOneArgument test, put a breakpoint in AssemblyAnalyzer and dump the opcodes for the method.
We can see that the instruction before the call to the constructor is loading a pointer to the function whose signature we care about.
Risks
This will break if the function pointer isn't loaded immediately before the constructor is called. To be honest I have no idea if this is likely to happen, but I've not found a way to make it happen yet.
Related reading
https://www.codeproject.com/articles/Dynamic-Runtime-Delegates-Conversion