简体   繁体   中英

byte array poiner to fixed byte array

I'm trying to make pointer to fixed byte array, so I can serialize it. But no matter what I try it doesn't work.

unsafe public struct Test
    {
        public byte*[] DataS { get => _data; }

        [JsonIgnore]
        private fixed byte _data[4];
    }

Here is what I'm trying to accomplish

struct tst
    {
        public byte[] Data { get => _data; }

        [JsonIgnore]
        private fixed byte[] _data;
    }

You should probably just create an expression bodied property to refer to the array.

struct tst
{
     public byte[] Data => _data; 
     [JsonIgnore]
     private byte[] _data;
}

This will return a reference to the actual array. But I would expect manylibraries to also need a public setter. Ie

public byte[] Data {
    get => _data;
    set => _data = value;
} 

Actual pointers are mostly used when interacting with native code, most other uses has been replaced by Span<T> / Memory<T> . And I see no reason at all to use pointers for serialization, most libraries should handle arrays just fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM