SafeHandle.IsInvalid 屬性


在衍生類別中覆寫時,取得值以指出這個控制代碼值是否無效。When overridden in a derived class, gets a value indicating whether the handle value is invalid.

 abstract property bool IsInvalid { bool get(); };
public abstract bool IsInvalid { get; }
member this.IsInvalid : bool
Public MustOverride ReadOnly Property IsInvalid As Boolean



如果控制代碼值無效,則為 true,否則為 falsetrue if the handle value is invalid; otherwise, false.


下列範例會檢查檔案是否已成功開啟。The following example checks if a file was opened successfully. 這個程式碼範例是針對類別提供之較大範例的一部分 SafeHandleThis code example is part of a larger example provided for the SafeHandle class.

// Determine if file is opened successfully.
if (_handle.IsInvalid)
    throw new Win32Exception(Marshal.GetLastWin32Error(), fileName);


衍生的類別必須執行 IsInvalid 屬性,讓 common language runtime 可以判斷是否需要進行重要的最終處理。Derived classes must implement the IsInvalid property so that the common language runtime can determine whether critical finalization is required. 衍生類別必須提供適合其支援的一般處理常式類型的實 (0 或-1) 無效。Derived classes must provide an implementation that suits the general type of handle they support (0 or -1 is invalid). 然後,您可以針對特定的安全控制碼類型進一步衍生這些類別。These classes can then be further derived for specific safe handle types.

不同于 IsClosed 屬性,它 SafeHandle 會報告物件是否已使用基礎控制碼完成, IsInvalid 屬性會計算給定的控制碼值是否一律視為無效。Unlike the IsClosed property, which reports whether the SafeHandle object has finished using the underlying handle, the IsInvalid property calculates whether the given handle value is always considered invalid. 因此, IsInvalid 屬性一律會針對任何一個控制碼值傳回相同的值。Therefore, the IsInvalid property always returns the same value for any one handle value.