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

要执行的安全操作(Demand、Assert 等等)。

pset
PermissionSet

应用此操作的权限集。

例外

action 无效(RequestMinimumRequestOptionalRequestRefuse 无效)。

已使用 CreateType() 创建包含类型。

- 或 -

权限集 pset 包含之前由 AddDeclarativeSecurity(SecurityAction, PermissionSet) 添加的操作。

- 或 -

对于当前的方法,属性 IsGenericMethodtrue,但属性 IsGenericMethodDefinitionfalse

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 元数据格式存储。 请参阅发出声明性安全属性。

适用于