// declared as class [ StructLayout( LayoutKind.Sequential )] public class OSVersionInfo { public int OSVersionInfoSize; public int majorVersion; public int minorVersion; public int buildNumber; public int platformId;
[ MarshalAs( UnmanagedType.ByValTStr, SizeConst=128 )] public String versionString; }
// declared as struct [ StructLayout( LayoutKind.Sequential )] public struct OSVersionInfo2 { public int OSVersionInfoSize; public int majorVersion; public int minorVersion; public int buildNumber; public int platformId;
[ MarshalAs( UnmanagedType.ByValTStr, SizeConst=128 )] public String versionString; } MashalAs特性:用於描述字段、方法或參數的封送處理格式。特性作為參數前綴並指定目標需要的數據類型。例如,以下代碼將兩個參數作為數據類型長指針封送給 Windows API 函數的字符串 (LPStr): [MarshalAs(UnmanagedType.LPStr)] String existingfile; [MarshalAs(UnmanagedType.LPStr)] String newfile;