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.

public:
 void Assert();
[System.Diagnostics.Conditional("FEATURE_MONO_CAS")]
public void Assert ();
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 导致 SecurityException时,尝试调用 AssertAn 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,当堆栈遍历到达尝试调用 Assert的代码时,将引发 SecurityExceptionHowever, 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

适用于

另请参阅