Description
Details about Problem
nanoFramework area: (C# code)
Description
Exception raised when span bellow is accessed and data from span[] are assigned:
using System;
using System.Diagnostics;
namespace TestSpanRef
{
public class Program
{
public static void Main()
{
try
{
SpanByte span = new byte[2];
span[0] = 42;
span[1] = 24;
Debug.WriteLine($"{span.Length}, {span[0]}, {span[1]}");
}
catch (Exception ex)
{
Debug.WriteLine($"{ex}");
}
}
}
public ref struct SpanByte
{
private byte[] _array;
private int _length;
public SpanByte(byte[] array)
{
_array = array;
_length = array == null ? 0 : array.Length;
}
public ref byte this[int index] => ref _array[index];
public int Length => _length;
public static implicit operator SpanByte(byte[] array)
{
return new SpanByte(array);
}
}
}
Exception raised:
++++ Exception System.Exception - CLR_E_WRONG_TYPE (1) ++++
++++ Message:
++++ TestSpanRef.SpanByte::get_Length [IP: 0004] ++++
Detailed repro steps so we can see the same problem
Run the follow up code, after accessing the span[0], the internal buffer is still 0x00 and after accessing the second span[1], the span gets to something different than SpanByte and the exception is rasied.
Expected behaviour
span[0], span[1] should contains the allocated values. Also the internal array should stay the same, the type of SpanByte should stay the same as well.
Sample Project
Code above.
Additional context
As nanoFramework does not support generic, idea is to reimplement a almost compatible Span class to handle similar behavior as original Span.
Make an effort to fix the bug
Still discovering nanoFramework. Will be of course happy to help!