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 ofIInvocation
instances, thus causingInvalidProgramException
s and, to a lesser degree,NullReferenceException
s.This PR targets the code locations (hopefully all of them) where such conversions between
object
and by-ref-like types occur, and suppresses those conversions in favor of writing certain default values:null
where assignments are made toIInvocation.Arguments
orIInvocation.ReturnValue
default
value of the by-ref-like type where assignments are made toout
arguments 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:
default
values should be written back toref
arguments, 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 adefault
value.Type.IsByRefLike
still throwNotSupportedException
: "Derived classes must provide an implementation" dotnet/runtime#91532Fixes #651.