From f0976ae9240a4071ca02bfe2cb108fbdad3fe853 Mon Sep 17 00:00:00 2001 From: dahall Date: Wed, 27 May 2020 22:14:24 -0600 Subject: [PATCH] Added implicit conversion operator to SafeCoTaskMemStruct and SafeHGlobalStruct from nullable struct. --- Core/InteropServices/SafeMemStruct.cs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Core/InteropServices/SafeMemStruct.cs b/Core/InteropServices/SafeMemStruct.cs index ca3bae82..53565b71 100644 --- a/Core/InteropServices/SafeMemStruct.cs +++ b/Core/InteropServices/SafeMemStruct.cs @@ -151,6 +151,11 @@ namespace Vanara.InteropServices /// The number of bytes allocated to . [ExcludeFromCodeCoverage] public SafeCoTaskMemStruct(IntPtr ptr, bool ownsHandle = true, SizeT allocatedBytes = default) : base(ptr, ownsHandle, allocatedBytes) { } + + /// Performs an implicit conversion from to . + /// The value of the instance or . + /// The resulting instance from the conversion. + public static implicit operator SafeCoTaskMemStruct(TStruct? s) => s.HasValue ? new SafeCoTaskMemStruct(s.Value) : new SafeCoTaskMemStruct(IntPtr.Zero); } /// @@ -175,5 +180,10 @@ namespace Vanara.InteropServices /// The number of bytes allocated to . [ExcludeFromCodeCoverage] public SafeHGlobalStruct(IntPtr ptr, bool ownsHandle = true, SizeT allocatedBytes = default) : base(ptr, ownsHandle, allocatedBytes) { } + + /// Performs an implicit conversion from to . + /// The value of the instance or . + /// The resulting instance from the conversion. + public static implicit operator SafeHGlobalStruct(TStruct? s) => s.HasValue ? new SafeHGlobalStruct(s.Value) : new SafeHGlobalStruct(IntPtr.Zero); } } \ No newline at end of file