SuppressUnmanagedCodeSecurityAttribute 類別

定義

允許 Managed 程式代碼在沒有堆疊逐步解說的情況下呼叫 Unmanaged 程式代碼。 此類別無法獲得繼承。

public ref class SuppressUnmanagedCodeSecurityAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Delegate | System.AttributeTargets.Interface | System.AttributeTargets.Method, AllowMultiple=true, Inherited=false)]
public sealed class SuppressUnmanagedCodeSecurityAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Interface | System.AttributeTargets.Method, AllowMultiple=true, Inherited=false)]
public sealed class SuppressUnmanagedCodeSecurityAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Delegate | System.AttributeTargets.Interface | System.AttributeTargets.Method, AllowMultiple=true, Inherited=false)]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class SuppressUnmanagedCodeSecurityAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Delegate | System.AttributeTargets.Interface | System.AttributeTargets.Method, AllowMultiple=true, Inherited=false)>]
type SuppressUnmanagedCodeSecurityAttribute = class
    inherit Attribute
[<System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Interface | System.AttributeTargets.Method, AllowMultiple=true, Inherited=false)>]
type SuppressUnmanagedCodeSecurityAttribute = class
    inherit Attribute
[<System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Delegate | System.AttributeTargets.Interface | System.AttributeTargets.Method, AllowMultiple=true, Inherited=false)>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type SuppressUnmanagedCodeSecurityAttribute = class
    inherit Attribute
Public NotInheritable Class SuppressUnmanagedCodeSecurityAttribute
Inherits Attribute
繼承
SuppressUnmanagedCodeSecurityAttribute
屬性

備註

Important

部分受信任的程式碼已不再支援。 此屬性在 .NET Core 中無效。

注意事項

使用這個屬性時要非常小心。 使用不當可能導致安全漏洞。

此屬性可套用於想要呼叫原生程式碼且不因執行時安全檢查而損失效能的方法。 在執行時,呼叫非管理程式碼時的堆疊走動會省略,從而大幅節省效能。 在類別中使用此屬性時,會將其套用到所有包含的方法。

通常,當受管理程式碼呼叫非受管理程式碼(透過 PInvoke 或 COM 互通進入原生程式碼)時,都會有需求取得 UnmanagedCode 權限以確保所有呼叫者都擁有允許此權限。 透過套用此明確屬性,開發者可以在執行時抑制需求。 開發者必須負責確保轉換到非管理程式碼時,透過其他方式獲得充分保護。 授權需求 UnmanagedCode 仍會在連結時發生。 例如,若函式 A 呼叫函式 B,且函式 B 標記為 SuppressUnmanagedCodeSecurityAttribute,函式 A 會在即時編譯時檢查未管理程式碼權限,但執行時不會檢查。

此屬性僅在應用於 PInvoke 方法(或包含 PInvoke 方法的類別)或定義透過的互通呼叫介面時有效。 在其他所有情境下,它都會被忽略。

此屬性對於實作一個類別,透過非管理程式碼提供系統資源存取非常有用。 沒有權限存取非管理程式碼的程式碼,可以呼叫帶有此屬性的類別來存取非受管理程式碼。 只有當擁有此屬性的類別寫入者已將該類別設定為安全時,這才是安全的。 若不然,這個屬性就很危險,可能會讓使用它的程式碼被濫用。

這不是宣告式的安全屬性,而是一個常規屬性(它源自 Attribute,而非 SecurityAttribute)。

建構函式

名稱 Description
SuppressUnmanagedCodeSecurityAttribute()

初始化 SuppressUnmanagedCodeSecurityAttribute 類別的新執行個體。

屬性

名稱 Description
TypeId

在衍生類別中實作時,取得這個 Attribute的唯一標識碼。

(繼承來源 Attribute)

方法

名稱 Description
Equals(Object)

傳回值,這個值表示這個實例是否等於指定的物件。

(繼承來源 Attribute)
GetHashCode()

傳回這個實例的哈希碼。

(繼承來源 Attribute)
GetType()

取得目前實例的 Type

(繼承來源 Object)
IsDefaultAttribute()

在衍生類別中覆寫時,指出這個實例的值是否為衍生類別的預設值。

(繼承來源 Attribute)
Match(Object)

在衍生類別中覆寫時,傳回值,指出這個實例是否等於指定的物件。

(繼承來源 Attribute)
MemberwiseClone()

建立目前 Object的淺層複本。

(繼承來源 Object)
ToString()

傳回表示目前 物件的字串。

(繼承來源 Object)

明確介面實作

名稱 Description
_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。

(繼承來源 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

擷取 物件的型別資訊,可用來取得介面的類型資訊。

(繼承來源 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

擷取物件提供的類型資訊介面數目 (0 或 1)。

(繼承來源 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供物件所公開屬性和方法的存取權。

(繼承來源 Attribute)

適用於

另請參閱