-
public interface IOption {
public DHCPOptions Type { get; set; }
public byte Length { get; set; }
//public Span<byte> Value { get; }
} NO1. how to cteate a Span property from a fixed size buffer ?public unsafe struct OptionClassId : IOption {
public DHCPOptions Type { get; set; }
public byte Length { get; set; }
public fixed byte ClassId[128];
//public Span<byte> Value => Unsafe.AsRef(ClassId);// Unsafe.Read<byte>(ClassId);
} NO2.how to use sizeof like C++ in C# get the size of fixed size buffer ?like: //get size of the filed of ClassId
int classIdLenght= sizeof(ClassId); NO3.how to create a Extension Method for fixed size buffer ?like: public static unsafe Span<byte> AsSpan<T>(this T* unmanagedT) where T : unmanaged {
return new Span<byte>(unmanagedT, sizeof(T));
}
ClassId.AsSpan() NO4.how to create a custom struct as the fixed size buffer's base Type?like: public struct OptionDomainNameServer : IOption {
public DHCPOptions Type { get; set; }
public byte Length { get; set; }
public fixed IPv4 DomainNameServers[2];//?????
public Span<byte> Value => throw new NotImplementedException();
}
public readonly struct IPv4 : IEquatable<IPv4> {
private readonly uint value;
//...
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
NO1. how to cteate a Span property from a fixed size buffer ? using System;
struct Foo
{
private const int BufLen = 128;
private unsafe fixed byte Buffer[BufLen];
public Span<byte> Data {
get{
unsafe {
fixed(Byte* ptr = &Buffer[0]){
return new Span<byte>(ptr, BufLen);
}
}
}
}
} NO2.how to use sizeof like C++ in C# get the size of fixed size buffer ? NO3.how to create a Extension Method for fixed size buffer? NO4.how to create a custom struct as the fixed size buffer's base Type? |
Beta Was this translation helpful? Give feedback.
NO1. how to cteate a Span property from a fixed size buffer ?
You could write:
NO2.how to use sizeof like C++ in C# get the size of fixed size buffer ?
There is no
sizeof
support. The type of an fixed buffer access is just a pointer.NO3.how to create a Extension Method for fixed size buffer?
You can't create extension method for pointers -> you can't create…