共用方式為


MethodBuilder.AddDeclarativeSecurity(SecurityAction, PermissionSet) 方法

定義

加入這個方法的宣告式安全性。

public:
 void AddDeclarativeSecurity(System::Security::Permissions::SecurityAction action, System::Security::PermissionSet ^ pset);
public void AddDeclarativeSecurity (System.Security.Permissions.SecurityAction action, System.Security.PermissionSet pset);
member this.AddDeclarativeSecurity : System.Security.Permissions.SecurityAction * System.Security.PermissionSet -> unit
Public Sub AddDeclarativeSecurity (action As SecurityAction, pset As PermissionSet)

參數

action
SecurityAction

要採取的安全性動作 (要求、判斷提示等)。

pset
PermissionSet

此動作適用的權限集。

例外狀況

action 無效 (RequestMinimumRequestOptionalRequestRefuse 無效)。

已使用 CreateType() 建立包含類型。

-或-

權限集 pset 包含稍早 AddDeclarativeSecurity(SecurityAction, PermissionSet) 所加入的動作。

-或-

針對於目前的方法,IsGenericMethod 屬性為 true,但 IsGenericMethodDefinition 屬性為 false

psetnull

範例

下列程式代碼範例說明 的內容使用 AddDeclarativeSecurity ,以要求方法的呼叫端具有不受限制的許可權。

// myModBuilder is an instance of ModuleBuilder.
// Note that for the use of PermissionSet and SecurityAction,
// the namespaces System::Security and System::Security::Permissions
// should be included.

TypeBuilder^ myTypeBuilder = myModBuilder->DefineType( "MyType",
                             TypeAttributes::Public );

array<Type^>^ temp0 = {int::typeid, int::typeid};
MethodBuilder^ myMethod1 = myTypeBuilder->DefineMethod( "MyMethod",
                           MethodAttributes::Public,
                           int::typeid, temp0 );

PermissionSet^ myMethodPermissions = gcnew PermissionSet(
                                     PermissionState::Unrestricted );

myMethod1->AddDeclarativeSecurity( SecurityAction::Demand,
                                   myMethodPermissions );
// myModBuilder is an instance of ModuleBuilder.

TypeBuilder myTypeBuilder = myModBuilder.DefineType("MyType",
                    TypeAttributes.Public);

MethodBuilder myMethod1 = myTypeBuilder.DefineMethod("MyMethod",
                    MethodAttributes.Public,
                    typeof(int),
                    new Type[]
                    {typeof(int), typeof(int)});	

PermissionSet myMethodPermissions = new PermissionSet(
            PermissionState.Unrestricted);

myMethod1.AddDeclarativeSecurity(SecurityAction.Demand,
                 myMethodPermissions);
' myModBuilder is an instance of ModuleBuilder.
' Note that for the use of PermissionSet and SecurityAction,
' the namespaces System.Security and System.Security.Permissions
' should be included.
Dim myTypeBuilder As TypeBuilder = myModBuilder.DefineType("MyType", _
                        TypeAttributes.Public)

Dim myMethod1 As MethodBuilder = myTypeBuilder.DefineMethod("MyMethod", _
                       MethodAttributes.Public, _
                       GetType(Integer), _
                       New Type() {GetType(Integer), GetType(Integer)})

Dim myMethodPermissions As New PermissionSet(PermissionState.Unrestricted)

myMethod1.AddDeclarativeSecurity(SecurityAction.Demand, myMethodPermissions)

備註

AddDeclarativeSecurity 可以呼叫數次,每個呼叫都會指定安全性動作 (,例如 DemandAssertDeny) ,以及動作適用的一組許可權。

注意

在 .NET Framework 1.0、1.1 和 2.0 版中,使用 AddDeclarativeSecurity 方法套用至方法的宣告式安全性屬性會以舊的 XML 元數據格式儲存。 請參閱發出宣告式安全性屬性。

適用於