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:
 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存在活动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 ,则当堆栈遍历到达尝试调用Assert的代码时,将引发。However, 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

适用于

另请参阅