Marshal.GetHRForException(Exception) Marshal.GetHRForException(Exception) Marshal.GetHRForException(Exception) Marshal.GetHRForException(Exception) Method


將指定的例外狀況轉換成 HRESULT。 Converts the specified exception to an HRESULT.

 static int GetHRForException(Exception ^ e);
public static int GetHRForException (Exception e);
static member GetHRForException : Exception -> int
Public Shared Function GetHRForException (e As Exception) As Integer


Exception Exception Exception Exception

要轉換成 HRESULT 的例外狀況。 The exception to convert to an HRESULT.


對應於提供的例外狀況的 HRESULT。 The HRESULT mapped to the supplied exception.


GetHRForException 也會設定IErrorInfo介面,可由呼叫 COM GetErrorInfoNEEDGUID 函式的例外狀況。GetHRForException also sets up an IErrorInfo interface for the exception that can be obtained by calling the COM GetErrorInfoNEEDGUID function. 您可以使用此函式傳回的 HRESULT 值上,您套用的 COM 介面的 managed 的類別實作PreserveSigAttribute屬性。You can use this function to return an HRESULT value on a managed class implementation of a COM interface where you apply the PreserveSigAttribute attribute. 已屬性化的方法攔截所有例外狀況,並使用GetHRForException方法來傳回適當的 HRESULT 值。Have the attributed method catch all exceptions and use the GetHRForException method to return the appropriate HRESULT value. 允許例外狀況傳播的方法之外,會產生不正確的行為。Allowing an exception to propagate outside the method produces incorrect behavior. (事實上,common language runtime 無法將例外狀況傳遞給 COM 用戶端呼叫 v 資料表透過這種方法。)(In fact, the common language runtime fails to pass an exception to a COM client that calls such a method through a v-table.)

請注意,GetHRForException方法會設定IErrorInfo目前執行緒的介面。Note that the GetHRForException method sets the IErrorInfo interface of the current thread. 方法之類的這可能會造成非預期的結果ThrowExceptionForHR預設使用的方法IErrorInfo目前的執行緒如果設定。This can cause unexpected results for methods like the ThrowExceptionForHR methods that default to using the IErrorInfo of the current thread if it is set.


需要完全信任立即呼叫端。 requires full trust for the immediate caller. 此成員不能由部分信任或不透明的程式碼。 This member cannot be used by partially trusted or transparent code.