Marshal.GetHRForException(Exception) 方法

定义

将指定异常转换为 HRESULT。

public:
 static int GetHRForException(Exception ^ e);
[System.Security.SecurityCritical]
public static int GetHRForException (Exception e);
public static int GetHRForException (Exception? e);
public static int GetHRForException (Exception e);
[<System.Security.SecurityCritical>]
static member GetHRForException : Exception -> int
static member GetHRForException : Exception -> int
Public Shared Function GetHRForException (e As Exception) As Integer

参数

e
Exception

要转换为 HRESULT 的异常。

返回

映射到所提供的异常的 HRESULT。

属性

注解

GetHRForException 还会为异常设置 IErrorInfo 接口,可通过调用 COM GetErrorInfoNEEDGUID 函数获取该异常。 可以使用此函数在 COM 接口的托管类实现上返回 HRESULT 值,其中应用 了 PreserveSigAttribute 特性。 让特性化方法捕获所有异常,并使用 GetHRForException 方法返回相应的 HRESULT 值。 允许异常在 方法外部传播会产生不正确的行为。 (事实上,公共语言运行时无法将异常传递给通过 v-table.)

请注意, GetHRForException 方法设置当前线程的 IErrorInfo 接口。 这可能会导致方法(如 ThrowExceptionForHR 默认使用当前线程的 IErrorInfo 的方法)出现意外结果(如果已设置)。

适用于

另请参阅