Draft: Prevent disallowed conversions between object and by-ref-like parameter and return types
#665
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.
DynamicProxy currently does not support intercepting methods that have any by-ref-like (
ref struct) parameter or return types (such asSpan<T>orReadOnlySpan<T>), because by-ref-like types cannot be converted to or fromobject. DynamicProxy however attempts such conversions when it transfers argument and return values into or out ofIInvocationinstances, thus causingInvalidProgramExceptions and, to a lesser degree,NullReferenceExceptions.This PR targets the code locations (hopefully all of them) where such conversions between
objectand by-ref-like types occur, and suppresses those conversions in favor of writing certain default values:nullwhere assignments are made toIInvocation.ArgumentsorIInvocation.ReturnValuedefaultvalue of the by-ref-like type where assignments are made tooutarguments or when values arereturn-ed to callers.This work should be merged before #664, because the code locations that it touches are the same ones where #664 would introduce (optional) user-defined conversions instead of always writing fixed default values.
This is currently still a draft because two things is still missing:
defaultvalues should be written back torefarguments, i. e. those parameters should be left unmodified. They are already initialized and may contain more meaningful values that should perhaps not be overwritten with adefaultvalue.Type.IsByRefLikestill throwNotSupportedException: "Derived classes must provide an implementation" dotnet/runtime#91532Fixes #651.