DynamicProxy currently does not support intercepting methods that have any by-ref-like (ref struct) parameter or return types (such as Span<T> or ReadOnlySpan<T>), because by-ref-like types cannot be converted to or from object. DynamicProxy however attempts such conversions when it transfers argument and return values into or out of IInvocation instances, thus causing InvalidProgramExceptions and, to a lesser degree, NullReferenceExceptions.
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 to IInvocation.Arguments or IInvocation.ReturnValue
the default value of the by-ref-like type where assignments are made to out arguments or when values are return-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:
[ ] No default values should be written back to ref 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 a default value.
[X] I may have stumbled over another CoreCLR bug. I should perhaps verify and report this, and see if a better workaround is possible than simply swallowing exceptions. → https://github.com/dotnet/runtime/issues/91532
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.Fixes #651.