-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Interop using Span and Structures: Error CS8377 and Error CS1663 #48784
Comments
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label. |
This is expected. The Additionally, your sample code using Unless your types are blittable, you should not be using For your scenario, you should either put For a better experience with fixed-size buffers with user-defined types, improvements in that use case are being tracked by dotnet/csharplang#1502. |
Hello,
I am trying to learn and take advantage of Spans when dealing with native code and I encountered immediately some problems.
This is how would look getting data using the old Marshalling techniques:
I am trying to convert the above using ReadOnlySpan and combine it with the new generics features of the language:
As is I get Error CS8377: The type StructA must be a non-nullable value type, along with all fields at any level of nesting, in order to use it as parameter T in the generic type or method...
So far this is what I have tried:
Although I am not happy to recur to unsafe code, even in this way I get Error CS1663: Fixed size buffer type must be one of the following: bool, byte, short, int, long, char, sbyte, ushort, uint, ulong, float or double
The error is self explanatory, but StructB only contains uint !!
If I replace the declaration with
public unsafe fixed uint ArrayOfStructB[16];
it compiles, but this opens the road to new issues when accessing the data from the code, problems with unsafe inside iterators along with another concern: in this sample I can use a fixed buffer because the struct only contains uint, but what if contains a mix of ushort, int, bool etc?Without unsafe context this is the only way I got it to work.
This is not feasible, extremely confusing and the debugger shows the data unordered and mixed.
I would have never expected that would have been so complicated the transition and therefore I would like to ask:
Memory<T>
instead ofSpan<T>
, but I see everywhere mostly the latter and several people recommend to try to use it whenever possible.Thanks
The text was updated successfully, but these errors were encountered: