mirror of
https://github.com/Thealexbarney/LibHac.git
synced 2024-11-14 10:49:41 +01:00
19 lines
690 B
C#
19 lines
690 B
C#
|
using System;
|
|||
|
using System.Runtime.CompilerServices;
|
|||
|
|
|||
|
namespace LibHac.Tests.Common;
|
|||
|
|
|||
|
public class Layout
|
|||
|
{
|
|||
|
public static int GetOffset<TStruct, TField>(in TStruct structRef, in TField fieldRef)
|
|||
|
{
|
|||
|
ref TField structOffset = ref Unsafe.As<TStruct, TField>(ref Unsafe.AsRef(in structRef));
|
|||
|
ref TField fieldOffset = ref Unsafe.AsRef(in fieldRef);
|
|||
|
int offset = Unsafe.ByteOffset(ref structOffset, ref fieldOffset).ToInt32();
|
|||
|
|
|||
|
if (offset >= Unsafe.SizeOf<TStruct>())
|
|||
|
throw new ArgumentException($"Error getting field offset. {nameof(structRef)} and {nameof(fieldRef)} must be from the same struct instance.");
|
|||
|
|
|||
|
return offset;
|
|||
|
}
|
|||
|
}
|