From 5bf2fce31e02317236cf50ea00fbecbe8ed33ef5 Mon Sep 17 00:00:00 2001 From: dahall Date: Tue, 28 Jun 2022 15:04:13 -0600 Subject: [PATCH] Added IErrorProvider as equatable for HRESULT --- PInvoke/Shared/WinError/HRESULT.cs | 46 +++++++++++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 10 deletions(-) diff --git a/PInvoke/Shared/WinError/HRESULT.cs b/PInvoke/Shared/WinError/HRESULT.cs index 153584ae..be8d2d83 100644 --- a/PInvoke/Shared/WinError/HRESULT.cs +++ b/PInvoke/Shared/WinError/HRESULT.cs @@ -41,7 +41,7 @@ namespace Vanara.PInvoke [StructLayout(LayoutKind.Sequential)] [TypeConverter(typeof(HRESULTTypeConverter))] [PInvokeData("winerr.h")] - public partial struct HRESULT : IComparable, IComparable, IEquatable, IEquatable, IEquatable, IConvertible, IErrorProvider + public partial struct HRESULT : IComparable, IComparable, IEquatable, IEquatable, IEquatable, IEquatable, IConvertible, IErrorProvider { internal readonly int _value; @@ -535,12 +535,12 @@ namespace Vanara.PInvoke /// Performs an implicit conversion from to . /// The value. /// The result of the conversion. - public static implicit operator HRESULT(int value) => new HRESULT(value); + public static implicit operator HRESULT(int value) => new(value); /// Performs an implicit conversion from to . /// The value. /// The resulting instance from the conversion. - public static implicit operator HRESULT(uint value) => new HRESULT(value); + public static implicit operator HRESULT(uint value) => new(value); /// Maps an NT Status value to an HRESULT value. /// The NT Status value. @@ -565,7 +565,7 @@ namespace Vanara.PInvoke /// The code. /// The resulting . public static HRESULT Make(bool severe, uint facility, uint code) => - new HRESULT(unchecked((int)((severe ? severityMask : 0) | (facility << facilityShift) | code))); + new(unchecked((int)((severe ? severityMask : 0) | (facility << facilityShift) | code))); /// Implements the operator !=. /// The first . @@ -585,6 +585,12 @@ namespace Vanara.PInvoke /// The result of the operator. public static bool operator !=(HRESULT hrLeft, uint hrRight) => !(hrLeft == hrRight); + /// Implements the operator ==. + /// The first . + /// The second . + /// The result of the operator. + public static bool operator !=(HRESULT hrLeft, IErrorProvider hrRight) => !hrLeft.Equals(hrRight.ToHRESULT()); + /// Implements the operator ==. /// The first . /// The second . @@ -603,6 +609,21 @@ namespace Vanara.PInvoke /// The result of the operator. public static bool operator ==(HRESULT hrLeft, uint hrRight) => hrLeft.Equals(hrRight); + /// Implements the operator ==. + /// The first . + /// The second . + /// The result of the operator. + public static bool operator ==(HRESULT hrLeft, IErrorProvider hrRight) => hrLeft.Equals(hrRight.ToHRESULT()); + + /// + /// If the supplied raw HRESULT value represents a failure, throw the associated with the optionally + /// supplied message. + /// + /// The 32-bit raw HRESULT value. + /// The optional message to assign to the . + [System.Diagnostics.DebuggerStepThrough] + public static void ThrowIfFailed(HRESULT hresult, string message = null) => hresult.ThrowIfFailed(message); + /// /// If the supplied raw HRESULT value represents a failure, throw the associated with the optionally /// supplied message. @@ -649,6 +670,11 @@ namespace Vanara.PInvoke /// true if the current object is equal to the parameter; otherwise, false. public bool Equals(uint other) => unchecked((int)other) == _value; + /// Indicates whether the current object is equal to an . + /// An object to compare with this object. + /// true if the current object is equal to the parameter; otherwise, false. + public bool Equals(IErrorProvider other) => Equals(other.ToHRESULT()); + /// Determines whether the specified , is equal to this instance. /// The to compare with this instance. /// true if the specified is equal to this instance; otherwise, false. @@ -658,6 +684,7 @@ namespace Vanara.PInvoke HRESULT h => Equals(h), int i => Equals(i), uint u => Equals(u), + IErrorProvider e => Equals(e), _ => Equals(_value, ValueFromObj(obj)), }; @@ -715,9 +742,8 @@ namespace Vanara.PInvoke /// A that represents this instance. public override string ToString() { - string err = null; // Check for defined HRESULT value - if (!StaticFieldValueHash.TryGetFieldName(_value, out err) && Facility == FacilityCode.FACILITY_WIN32) + if (!StaticFieldValueHash.TryGetFieldName(_value, out var err) && Facility == FacilityCode.FACILITY_WIN32) { foreach (var info2 in typeof(Win32Error).GetFields(BindingFlags.Public | BindingFlags.Static).Where(fi => fi.FieldType == typeof(uint))) { @@ -819,7 +845,7 @@ namespace Vanara.PInvoke { public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) { - if (sourceType == typeof(Win32Error) || sourceType.IsPrimitive && sourceType != typeof(char)) + if (sourceType is IErrorProvider || sourceType.IsPrimitive && sourceType != typeof(char)) return true; return base.CanConvertFrom(context, sourceType); } @@ -833,13 +859,13 @@ namespace Vanara.PInvoke public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { - if (value is Win32Error e) + if (value is IErrorProvider e) return e.ToHRESULT(); if (value != null && value.GetType().IsPrimitive) { if (value is bool b) return b ? HRESULT.S_OK : HRESULT.S_FALSE; - if (!(value is char)) + if (value is not char) return new HRESULT((int)Convert.ChangeType(value, TypeCode.Int32)); } return base.ConvertFrom(context, culture, value); @@ -848,7 +874,7 @@ namespace Vanara.PInvoke public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { - if (!(value is HRESULT hr)) throw new NotSupportedException(); + if (value is not HRESULT hr) throw new NotSupportedException(); if (destinationType.IsPrimitive && destinationType != typeof(char)) return Convert.ChangeType(hr, destinationType); if (destinationType == typeof(string))