Skip to content

Instantly share code, notes, and snippets.

@smoogipoo
Last active July 7, 2023 14:43
Show Gist options
  • Save smoogipoo/de8d9b967e30ddbae09adcd3c6ce7c8d to your computer and use it in GitHub Desktop.
Save smoogipoo/de8d9b967e30ddbae09adcd3c6ce7c8d to your computer and use it in GitHub Desktop.
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;
// Works if you uncomment this line.
Console.WriteLine(Marshal.OffsetOf(typeof(Struct), nameof(Struct.A)));
// Fails if the above line is commented.
method(typeof(Struct));
return;
static void method([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type type)
{
Console.WriteLine(Marshal.OffsetOf(type, nameof(Struct.A)));
}
internal struct Struct
{
public int A;
}
@smoogipoo
Copy link
Author

Uncommented:

$ dotnet publish -r linux-x64 && ./bin/Release/net8.0/linux-x64/publish/MarshalTest

MSBuild version 17.7.0-preview-23281-03+4ce2ff1f8 for .NET
  Determining projects to restore...
  Restored /home/smgi/Workbench/MarshalTest/MarshalTest/MarshalTest.csproj (in 139 ms).
/home/smgi/.dotnet/sdk/8.0.100-preview.5.23303.2/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.RuntimeIdentifierInference.targets(314,5): message NETSDK1057: You are using a preview version of .NET. See: https://aka.ms/dotnet-support-policy [/home/smgi/Workbench/MarshalTest/MarshalTest/MarshalTest.csproj]
/home/smgi/Workbench/MarshalTest/MarshalTest/Program.cs(21,16): warning CS0649: Field 'Struct.A' is never assigned to, and will always have its default value 0 [/home/smgi/Workbench/MarshalTest/MarshalTest/MarshalTest.csproj]
  MarshalTest -> /home/smgi/Workbench/MarshalTest/MarshalTest/bin/Release/net8.0/linux-x64/MarshalTest.dll
  Generating native code
  MarshalTest -> /home/smgi/Workbench/MarshalTest/MarshalTest/bin/Release/net8.0/linux-x64/publish/
0
0

Commented:

$ dotnet publish -r linux-x64 && ./bin/Release/net8.0/linux-x64/publish/MarshalTest

MSBuild version 17.7.0-preview-23281-03+4ce2ff1f8 for .NET
  Determining projects to restore...
  All projects are up-to-date for restore.
/home/smgi/.dotnet/sdk/8.0.100-preview.5.23303.2/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.RuntimeIdentifierInference.targets(314,5): message NETSDK1057: You are using a preview version of .NET. See: https://aka.ms/dotnet-support-policy [/home/smgi/Workbench/MarshalTest/MarshalTest/MarshalTest.csproj]
/home/smgi/Workbench/MarshalTest/MarshalTest/Program.cs(21,16): warning CS0649: Field 'Struct.A' is never assigned to, and will always have its default value 0 [/home/smgi/Workbench/MarshalTest/MarshalTest/MarshalTest.csproj]
  MarshalTest -> /home/smgi/Workbench/MarshalTest/MarshalTest/bin/Release/net8.0/linux-x64/MarshalTest.dll
  Generating native code
  MarshalTest -> /home/smgi/Workbench/MarshalTest/MarshalTest/bin/Release/net8.0/linux-x64/publish/
Unhandled Exception: System.NotSupportedException: 'Struct' is missing structure marshalling data. This can happen for code that is not compatible with AOT. Inspect and fix AOT related warnings that were generated when the app was published. For more information see https://aka.ms/nativeaot-compatibility
   at Internal.Runtime.CompilerHelpers.RuntimeInteropData.GetStructFieldOffset(RuntimeTypeHandle, String) + 0xaa
   at System.Runtime.InteropServices.Marshal.OffsetOf(Type, String) + 0x6d
   at Program.<<Main>$>g__method|0_0(Type) + 0x10
   at MarshalTest!<BaseAddress>+0xefab1
Aborted (core dumped)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment