CodeAccessPermission.Assert CodeAccessPermission.Assert CodeAccessPermission.Assert CodeAccessPermission.Assert Method

定義

宣告即使堆疊中較高層的呼叫端未獲得資源存取權限,呼叫程式碼仍可透過呼叫這個方法的程式碼要求權限,來存取受保護的資源。Declares that the calling code can access the resource protected by a permission demand through the code that calls this method, even if callers higher in the stack have not been granted permission to access the resource. 使用 Assert() 可能會造成安全性問題。Using Assert() can create security issues.

public:
 virtual void Assert();
[System.Diagnostics.Conditional("FEATURE_MONO_CAS")]
public void Assert ();
abstract member Assert : unit -> unit
override this.Assert : unit -> unit
Public Sub Assert ()

實作

例外狀況

呼叫的程式碼沒有 AssertionThe calling code does not have Assertion.

-或--or- 目前的框架已有作用中的 Assert()There is already an active Assert() for the current frame.

備註

通常,向下成長,來表示呼叫堆疊,使方法呼叫堆疊中較高層呼叫方法較低的呼叫堆疊中。The call stack is typically represented as growing down, so that methods higher in the call stack call methods lower in the call stack. 呼叫Assert防止源自於呼叫堆疊,從繼續向上之外的程式碼,會呼叫這個方法的呼叫堆疊的較低的堆疊查核行程。Calling Assert prevents a stack walk originating lower in the call stack from proceeding up the call stack beyond the code that calls this method. 因此,即使較高的呼叫堆疊上呼叫端沒有必要的權限來存取資源,他們仍然可以透過呼叫這個方法所需的權限的程式碼。Therefore, even if callers higher on the call stack do not have the requisite permissions to access a resource, they can still access it through the code that calls this method on the necessary permission. 判斷提示是有效的程式碼呼叫時,才Assert通過安全性檢查,它會判斷提示之權限。An assertion is effective only if the code that calls Assert passes the security check for the permission that it is asserting.

若要呼叫Assert會生效,直到呼叫程式碼傳回至呼叫端。The call to Assert is effective until the calling code returns to its caller. 只有一個Assert作用中的框架。Only one Assert can be active on a frame. 嘗試呼叫Assert當作用Assert中的框架結果存在於SecurityExceptionAn attempt to call Assert when an active Assert exists on the frame results in a SecurityException. 呼叫RevertAssert或是RevertAll若要移除作用AssertCall RevertAssert or RevertAll to remove an active Assert.

Assert 會忽略該權限的要求將會失敗,因為未授與權限。Assert is ignored for a permission not granted because a demand for that permission will not succeed. 不過,如果在呼叫堆疊中較低的程式碼會呼叫Demand該權限,請SecurityException堆疊查核行程達到嘗試呼叫的程式碼時,會擲回AssertHowever, if code lower on the call stack calls Demand for that permission, a SecurityException is thrown when the stack walk reaches the code that tried to call Assert. 這是因為,所呼叫的程式碼Assert沒有被授予權限,即使應用程式嘗試Assert它。This happens because the code that called Assert has not been granted the permission, even though it tried to Assert it.

警告

因為呼叫Assert移除所有程式碼呼叫鏈結中的需求必須被授與權限來存取指定的資源,如果不正確或不當使用,它可以開啟安全性的問題。Because calling Assert removes the requirement that all code in the call chain must be granted permission to access the specified resource, it can open up security issues if used incorrectly or inappropriately. 因此,它應該使用小心謹慎。Therefore, it should be used with great caution.

給繼承者的注意事項

您無法覆寫這個方法。You cannot override this method.

安全性

SecurityPermission
能夠呼叫Assert()for the ability to call Assert(). 相關聯的列舉型別: AssertionAssociated enumeration: Assertion

適用於

另請參閱