IPermission.Demand 方法


如果不符合安全性要求,在 Run Time 時會擲回 SecurityExceptionThrows a SecurityException at run time if the security requirement is not met.

 void Demand();
public void Demand ();
abstract member Demand : unit -> unit
Public Sub Demand ()


安全程式庫通常會使用這個方法,以確保呼叫端有權存取資源。This method is typically used by secure libraries to ensure that callers have permission to access a resource. 例如,在 Demand 執行呼叫端要求的檔案作業之前,安全類別庫中的檔案類別會呼叫必要的 FileIOPermissionFor example, a file class in a secure class library calls Demand for the necessary FileIOPermission before performing a file operation requested by the caller.

雖然實此介面方法的大部分類別都是藉由執行完整堆疊的逐步解說來滿足安全性準則,但不一定會執行堆疊的逐步解說。Although the majority of the classes that implement this interface method satisfy the security criteria by performing a full stack walk, a stack walk is not necessarily performed. 未執行堆疊逐步解說的實作為範例 PrincipalPermission.DemandAn example of an implementation that does not perform a stack walk is PrincipalPermission.Demand.

執行堆疊逐步解說時,不會檢查呼叫這個方法的程式碼許可權;檢查會從該程式碼的立即呼叫端開始,並啟動堆疊。When a stack walk is performed, the permissions of the code that calls this method are not examined; the check begins from the immediate caller of that code and proceeds up the stack. 呼叫堆疊通常會顯示為不斷增加,讓呼叫堆疊中較高的方法在呼叫堆疊中較低的呼叫方法。The call stack is typically represented as growing down, so that methods higher in the call stack call methods lower in the call stack. Demand 只有在未 SecurityException 引發時,才會成功。Demand succeeds only if no SecurityException is raised.