Hey WondTech readers! Have you recently updated your projects to .NET 10 and started seeing a 'TypeLoadException' pop up, seemingly out of nowhere? If your code uses 'InlineArray', you might be running into a new, subtle compatibility change that .NET 10 introduces. Don't worry, it's not a bug, but a clearer definition of how things should work, and there's a good way to fix it.

Here's the deal: In .NET, 'InlineArrayAttribute' is a neat feature that lets you define a struct with a fixed-size buffer right inside it. For example, if you declare '[InlineArray(8)] struct Int8InlineArray { private int _element0; }', .NET understands that this struct should hold eight consecutive 'int' values, taking up 32 bytes (because an 'int' is 4 bytes). The 'InlineArray' attribute itself tells the runtime exactly how big this type should be.

The problem starts when you also add 'StructLayoutAttribute.Size' to the same type. Let's say you have '[InlineArray(8)] [StructLayout(LayoutKind.Sequential, Size = 32)] struct LegacyInt8InlineArray { private int _element0; }'. You're essentially giving .NET two different instructions about the type's size: one from 'InlineArray' and another from 'StructLayout.Size'. While older .NET runtimes might have allowed this, behaving in implementation-specific ways, .NET 10 is stricter. It sees these two declarations as conflicting and ambiguous.

Because .NET 10 can't decide which size definition to trust, it rejects the type when it's loaded, resulting in that 'TypeLoadException'. What's tricky is that this doesn't always happen during compilation. Your project might compile fine and even run for a while. The error usually surfaces when a 'cold path' of your code tries to load this specific type for the first time – maybe during reflection, interop registration, or serialization. It's a runtime error that can surprise you!

So, how do you fix this? The key is to understand why you added 'StructLayoutAttribute.Size' in the first place. Often, explicit sizes are used for interop scenarios, where you're dealing with external libraries or native code that expects a specific memory layout. Instead of just deleting 'StructLayoutAttribute.Size' blindly, which might break your interop, the recommended approach is to move that explicit size definition into a separate, unambiguous wrapper type.

By creating a wrapper, you can maintain the specific layout requirements for your interop needs while keeping your 'InlineArray' type clean and clear, adhering to .NET 10's stricter rules. This way, you get rid of the ambiguity, avoid the 'TypeLoadException', and ensure your code remains compatible and robust. Keep your .NET 10 projects running smoothly by handling these layout declarations carefully!