castleproject / Core

Castle Core, including Castle DynamicProxy, Logging Services and DictionaryAdapter
http://www.castleproject.org/
Other
2.2k stars 467 forks source link

Draft: Prevent disallowed conversions between `object` and by-ref-like parameter and return types #665

Open stakx opened 1 year ago

stakx commented 1 year ago

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:

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:

Fixes #651.