- C#, C♯, C#相談室 Part94
13 :デフォルトの名無しさん (ワッチョイ 0d0c-bVUD)[sage]:2020/04/06(月) 10:58:20.70 ID:sF8ihuq40 - 暗黙的な既定のコンストラクタが挿入されないようにとのC#コンパイラの最適化の都合
特に回避策はない 標準ライブラリで struct Vector3 { ... public static Vector3 Up = new Vector3(0, 1, 0); } みたいなのはある
| - C#, C♯, C#相談室 Part94
21 :デフォルトの名無しさん (ワッチョイ 0d0c-bVUD)[sage]:2020/04/06(月) 16:24:25.71 ID:sF8ihuq40 - そのclassの内容によるとしか…
無制限にマーシャリングできるわけでもないし classのメンバがblittableならそのままstructにすりゃいいだけだけど 参考になるか知らんがstringを含む場合 [StructLayout(LayoutKind.Sequential)] struct Foo { public int id; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] public string name; } class Program { static void Main(string[] args) { var sizeInBytes = Marshal.SizeOf<Foo>(); Console.WriteLine($"SizeOf={sizeInBytes}"); var coTaskMem = Marshal.AllocCoTaskMem(sizeInBytes); var foo = Marshal.PtrToStructure<Foo>(coTaskMem); foo.name = "John"; Console.WriteLine(foo.name); Marshal.DestroyStructure<Foo>(coTaskMem); Marshal.FreeCoTaskMem(coTaskMem); } }
|
|