Odex
Odex
CC#
Created by Odex on 2/9/2025 in #help
`readonly` struct, `in` modifier and defensive copies
Hi people, I just encountered the in modifier for structs. As far as I'm concerned the in modifier can negatively affect performance if the struct I'm passing is NOT readonly, because it might create defensive copies - however, how can I avoid defensive copies without marking my struct readonly? Is there a rule of thumb when defensive copies are created? Thanks in advance!
31 replies
CC#
Created by Odex on 5/13/2024 in #help
How to marshall pointer to array inside struct
I have the following struct
[StructLayout(LayoutKind.Sequential)]
public unsafe struct Font
{
public int BaseSize;
public int GlyphCount;
public int GlyphPadding;
public Texture Texture;
public Rectangle* Recs;
public GlyphInfo* Glyphs;
}
[StructLayout(LayoutKind.Sequential)]
public unsafe struct Font
{
public int BaseSize;
public int GlyphCount;
public int GlyphPadding;
public Texture Texture;
public Rectangle* Recs;
public GlyphInfo* Glyphs;
}
Here Recs and Glyphs are C like arrays iirc. I want to marshal them to managed ones but I get the following error
[StructLayout(LayoutKind.Sequential)]
public unsafe struct Font
{
public int BaseSize;
public int GlyphCount;
public int GlyphPadding;
public Texture Texture;
// Already tried [MarshalAs(UnmanagedType.ByValArray)]
public Rectangle[] Recs;
public GlyphInfo[] Glyphs;
}
[StructLayout(LayoutKind.Sequential)]
public unsafe struct Font
{
public int BaseSize;
public int GlyphCount;
public int GlyphPadding;
public Texture Texture;
// Already tried [MarshalAs(UnmanagedType.ByValArray)]
public Rectangle[] Recs;
public GlyphInfo[] Glyphs;
}
Error SYSLIB1051 : The type 'MyNamespace.Font' is not supported by source-generated P/Invokes. The generated source will not handle marshalling of parameter 'font'.
Error SYSLIB1051 : The type 'MyNamespace.Font' is not supported by source-generated P/Invokes. The generated source will not handle marshalling of parameter 'font'.
I am using LibraryImport
17 replies