Q: Why cannot box Span<T>? #6683
-
I was creating static cache for types and its converters that uses delegate T Decoder<T>(ReadOnlySpan<byte> span);
var decoders = new Dictionary<Type, Delegate>()
{
[typeof(int)] = new Decoder<int>(BinaryPrimitives.ReadInt32BitEndian)
};
var type = typeof(int);
var fn = decoders[type];
var buf = new byte[4]{1, 2, 3, 4};
var result = (int) fn.DynamicInvoke(buf.AsSpan()); // compile error: Cannot cast ReadOnlySpan<byte> to object Don't work separate span into variable like: var span = buf.AsSpan();
var result = (int) fn.DynamicInvoke(span); Same happened with Why cannot just cast to base class |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 6 replies
-
|
Beta Was this translation helpful? Give feedback.
See: https://learn.microsoft.com/en-us/archive/msdn-magazine/2018/january/csharp-all-about-span-exploring-a-new-net-mainstay