CodeAccessPermission.Assert 方法


声明调用代码能够通过调用此方法的代码,访问受权限请求保护的资源,即使未对堆栈中处于较高位置的调用方授予访问该资源的权限。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.

 virtual void Assert();
 void Assert();
public void Assert ();
public void Assert ();
abstract member Assert : unit -> unit
override this.Assert : unit -> unit
member 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. 调用 RevertAssertRevertAll 删除活动 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 未被授予权限,即使它尝试了该权限 AssertThis 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.