简体   繁体   中英

Should I create non-nullable class (and how) for MMF?

I explore Memory Mapped File (MMF) and here is what I have done so far just following example on MSDN http://msdn.microsoft.com/en-us/library/dd997372.aspx

class MMF {
    private long offset = 0x10000000; // 256 megabytes
    private long length = 0x20000000; // 512 megabytes
    public MMF() {
        using (var mmf = MemoryMappedFile.CreateFromFile(@"c:\NFS", FileMode.Open, "NMAP")) {
            using (var accessor = mmf.CreateViewAccessor(offset, length)) {
                int fsSize = Marshal.SizeOf(typeof(FS));
                FS nfs;
                // Make changes to the view.
                for (long i = 0; i < length; i += fsSize) {
                    accessor.Read(i, out nfs);
                    accessor.Write(i, ref nfs);

where FS is just array of another class objects

public  class FS {
    public NFS[] files;
    }

but I'm getting error:

The type 'NEN_Server.FS.FS' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'System.IO.UnmanagedMemoryAccessor.Read(long, out T)'

What am I doing wrong? Must my class to be non-nullable or should I use accessor in another way?

thank you

The keyword here is not "non-nullable", but "valuetyp". You're supposed to declare a struct for the data you want to read/write, not a class . The addition "non-nullable" only means you can't use foo? instead of foo .

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