From c5814672d54e1e2f720593d68d7c032158920e18 Mon Sep 17 00:00:00 2001 From: dahall Date: Wed, 19 Aug 2020 20:51:11 -0600 Subject: [PATCH] Added protected size and offset values --- Core/InteropServices/SafeMemStruct.cs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/Core/InteropServices/SafeMemStruct.cs b/Core/InteropServices/SafeMemStruct.cs index 53565b71..b3c379db 100644 --- a/Core/InteropServices/SafeMemStruct.cs +++ b/Core/InteropServices/SafeMemStruct.cs @@ -17,14 +17,17 @@ namespace Vanara.InteropServices /// public abstract class SafeMemStruct : SafeMemoryHandle, IEquatable where TMem : IMemoryMethods, new() where TStruct : struct { + /// The structure size, in bytes, of TStruct. + protected static readonly SizeT BaseStructSize = InteropExtensions.SizeOf(); + /// Initializes a new instance of the class. /// The TStruct value. /// The capacity of the buffer, in bytes. - protected SafeMemStruct(in TStruct s, SizeT capacity = default) : base(Math.Max((ulong)SizeOf(), (ulong)capacity)) => handle.Write(s); + protected SafeMemStruct(in TStruct s, SizeT capacity = default) : base(Math.Max(BaseStructSize, (ulong)capacity)) => handle.Write(s); /// Initializes a new instance of the class. /// The capacity of the buffer, in bytes. - protected SafeMemStruct(SizeT capacity = default) : base(Math.Max((ulong)SizeOf(), (ulong)capacity)) { } + protected SafeMemStruct(SizeT capacity = default) : base(Math.Max(BaseStructSize, (ulong)capacity)) { } /// Initializes a new instance of the class. /// The PTR. @@ -116,7 +119,10 @@ namespace Vanara.InteropServices /// A value held by this instance or null if the handle is invalid. public override string ToString() => ((TStruct?)this).ToString(); - private static SizeT SizeOf() => InteropExtensions.SizeOf(); + /// Returns the field offset of the named field. + /// The field name. + /// The offset, in bytes, of the field within the structure. + protected static long FieldOffset(string name) => Marshal.OffsetOf(typeof(TStruct), name).ToInt64(); #if ALLOWSPAN /// Gets a reference to a structure based on this allocated memory.