-
|
When generating, I periodically encounter similar structures: internal partial struct FWP_BYTE_ARRAY16
{
/// <summary>Array of 16 bytes.</summary>
internal __byte_16 byteArray16;
internal partial struct __byte_16
{
internal byte _0,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15;
/// <summary>Always <c>16</c>.</summary>
internal readonly int Length => 16;
}
}What's the best way to initialize them, given that there are similar structures with 256 values? Right now I am using something like this: internal static FWP_BYTE_ARRAY16 ToArray16(this IPAddress address)
{
var bytes = address.GetAddressBytes();
return new FWP_BYTE_ARRAY16
{
byteArray16 = new FWP_BYTE_ARRAY16.__byte_16
{
_0 = bytes[0],
_1 = bytes[1],
_2 = bytes[2],
_3 = bytes[3],
_4 = bytes[4],
_5 = bytes[5],
_6 = bytes[6],
_7 = bytes[7],
_8 = bytes[8],
_9 = bytes[9],
_10 = bytes[10],
_11 = bytes[11],
_12 = bytes[12],
_13 = bytes[13],
_14 = bytes[14],
_15 = bytes[15],
}
};
} |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 15 replies
-
|
I suggest adding something like this to those structures public unsafe void Set(string text)
{
text = text ?? string.Empty;
fixed (char* p0 = &_0)
{
for (var i = 0; i < text.Length && i < Length; i++)
{
p0[i] = text[i];
}
var nullTerminatedIndex = global::System.Math.Min(text.Length, Length - 1);
p0[nullTerminatedIndex] = '\0';
}
}Now I am using internal static class StringUtilities
{
public static unsafe void SetTo(
this string text,
char* start,
int maxLength)
{
for (var i = 0; i < text.Length && i < maxLength; i++)
{
start[i] = text[i];
}
start[Math.Min(text.Length, maxLength - 1)] = '\0';
}
}
fixed (char* p0 = &data._0)
{
title.SetTo(p0, data.Length);
} |
Beta Was this translation helpful? Give feedback.
-
|
The When targeting net472 and using C# 10, you get this: internal unsafe readonly void CopyTo(Span<byte> target, int length = 16)
{
if (length > 16) throw new ArgumentOutOfRangeException("length");
fixed (byte* p0 = &_0)
for(int i = 0; i < length; i++) target[i]= p0[i];
}We also offer these extension methods, that allow indexing in: internal static unsafe ref readonly byte ReadOnlyItemRef(this in winmdroot.NetworkManagement.WindowsFilteringPlatform.FWP_BYTE_ARRAY16.__byte_16 @this, int index)
{
fixed (byte* p0 = &@this._0)
return ref p0[index];
}
internal static unsafe ref byte ItemRef(this ref winmdroot.NetworkManagement.WindowsFilteringPlatform.FWP_BYTE_ARRAY16.__byte_16 @this, int index)
{
fixed (byte* p0 = &@this._0)
return ref p0[index];
}When targeting .NET Core 3.1 or later, you get that, and more: /// <summary>
/// Gets a ref to an individual element of the inline array.
/// ⚠ Important ⚠: When this struct is on the stack, do not let the returned reference outlive the stack frame that defines it.
/// </summary>
internal ref byte this[int index] => ref AsSpan()[index];
/// <summary>
/// Gets this inline array as a span.
/// </summary>
/// <remarks>
/// ⚠ Important ⚠: When this struct is on the stack, do not let the returned span outlive the stack frame that defines it.
/// </remarks>
internal Span<byte> AsSpan() => MemoryMarshal.CreateSpan(ref _0, 16);With the AsSpan and indexers, you can treat the fields with the same syntax as an array. |
Beta Was this translation helpful? Give feedback.
The
FWP_BYTE_ARRAY16you refer to is generated with several helpers, depending on the language version and target framework of your project.When targeting net472 and using C# 10, you get this:
We also offer these extension methods, that allow indexing in: