Home > OS >  MarshalAs for Properties in a struct
MarshalAs for Properties in a struct

Time:10-12

I am trying to add getters and setters to this struct, so I can use it as a listView items source. The problem is, this causes MarshalAs to be invalid thus not allowing compilation. What would the best option be to fix this?

Attribute 'MarshalAs' is not valid on this declaration type. It is only valid on 'field, parameter, return' declarations.**

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
    public struct FileExplorerData
    {
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 251)]
        public string name { get; set; }
        public FILETIME lastwrite { get; set; }
        public bool type { get; set; }
        public int size { get; set; }
    };

CodePudding user response:

You can specify the underlying field as the target of the attribute

[field: MarshalAs(UnmanagedType.ByValTStr, SizeConst = 251)]
public string name { get; set; }

This is valid from C# 7.3 . See the documentation.

  • Related