TypeBuilder.AddDeclarativeSecurity(SecurityAction, PermissionSet) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将声明性安全添加到此类型。
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
无效(RequestMinimum
、RequestOptional
和 RequestRefuse
无效)。
pset
为 null
。
示例
下面的示例演示了如何使用 AddDeclarativeSecurity
方法将 具有 SecurityPermissionFlag.ControlEvidence 标志的安全需求SecurityPermission添加到名为 MyDynamicClass
的动态类型,该类型位于名为 EmittedExample.dll 的程序集中。 该示例不生成控制台输出;运行后,可以使用 Ildasm.exe (IL 反汇编程序) 来检查 EmittedExample.dll。 在 MyDynamicClass
中, .class public auto ansi
打开 语句以查看声明性权限。
using namespace System;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
using namespace System::Security;
using namespace System::Security::Permissions;
int main()
{
// Create a simple name for the assembly; create the assembly and module.
AssemblyName^ myAssemblyName = gcnew AssemblyName("EmittedAssembly");
AssemblyBuilder^ myAssemblyBuilder =
AppDomain::CurrentDomain->DefineDynamicAssembly( myAssemblyName, AssemblyBuilderAccess::RunAndSave );
ModuleBuilder^ myModuleBuilder =
myAssemblyBuilder->DefineDynamicModule( "EmittedAssembly", "EmittedAssembly.dll");
// Define a public class named "MyDynamicClass" in the assembly.
TypeBuilder^ myTypeBuilder = myModuleBuilder->DefineType( "MyDynamicClass", TypeAttributes::Public );
// Create a permission set and add a security permission
// with the ControlEvidence flag.
//
PermissionSet^ myPermissionSet = gcnew PermissionSet(PermissionState::None);
myPermissionSet->AddPermission(
gcnew SecurityPermission(SecurityPermissionFlag::ControlEvidence));
// Add the permission set to the MyDynamicClass type,
// as a declarative security demand.
//
myTypeBuilder->AddDeclarativeSecurity(SecurityAction::Demand, myPermissionSet);
Type^ myType = myTypeBuilder->CreateType();
myAssemblyBuilder->Save("EmittedAssembly.dll");
}
using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Security;
using System.Security.Permissions;
namespace CustomAttribute_Sample
{
public class MyApplication
{
static void Main()
{
// Create a simple name for the assembly, and create the assembly and module.
AssemblyName myAssemblyName = new AssemblyName("EmittedAssembly");
AssemblyBuilder myAssemblyBuilder =
AppDomain.CurrentDomain.DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.RunAndSave);
ModuleBuilder myModuleBuilder =
myAssemblyBuilder.DefineDynamicModule("EmittedAssembly", "EmittedAssembly.dll");
// Define a public class named "MyDynamicClass" in the assembly.
TypeBuilder myTypeBuilder = myModuleBuilder.DefineType("MyDynamicClass",
TypeAttributes.Public);
// Create a permission set and add a security permission
// with the ControlEvidence flag.
//
PermissionSet myPermissionSet = new PermissionSet(PermissionState.None);
myPermissionSet.AddPermission(
new SecurityPermission(SecurityPermissionFlag.ControlEvidence));
// Add the permission set to the MyDynamicClass type,
// as a declarative security demand.
//
myTypeBuilder.AddDeclarativeSecurity(SecurityAction.Demand, myPermissionSet);
Type myType = myTypeBuilder.CreateType();
myAssemblyBuilder.Save("EmittedAssembly.dll");
}
}
}
Imports System.Reflection
Imports System.Reflection.Emit
Imports System.Security
Imports System.Security.Permissions
Namespace CustomAttribute_Sample
Class MyApplication
Shared Sub Main()
' Create a simple name for the assembly; create the assembly and the module.
Dim myAssemblyName As New AssemblyName("EmittedAssembly")
Dim myAssemblyBuilder As AssemblyBuilder = _
AppDomain.CurrentDomain.DefineDynamicAssembly( _
myAssemblyName, AssemblyBuilderAccess.RunAndSave)
Dim myModuleBuilder As ModuleBuilder = _
myAssemblyBuilder.DefineDynamicModule("EmittedAssembly", "EmittedAssembly.dll")
' Define a public class named "MyDynamicClass" in the assembly.
Dim myTypeBuilder As TypeBuilder = _
myModuleBuilder.DefineType("MyDynamicClass", TypeAttributes.Public)
' Create a permission set and add a security permission
' with the ControlEvidence flag.
'
Dim myPermissionSet As New PermissionSet(PermissionState.None)
Dim ce As New SecurityPermission(SecurityPermissionFlag.ControlEvidence)
myPermissionSet.AddPermission(ce)
' Add the permission set to the MyDynamicClass type,
' as a declarative security demand.
'
myTypeBuilder.AddDeclarativeSecurity(SecurityAction.Demand, myPermissionSet)
Dim myType As Type = myTypeBuilder.CreateType()
myAssemblyBuilder.Save("EmittedAssembly.dll")
End Sub
End Class
End Namespace
注解
AddDeclarativeSecurity
可以多次调用,每次调用指定一个安全操作 ((如 Demand、Assert 或 Deny) )以及一组应用于该操作的权限。
注意
在 .NET Framework版本 1.0、1.1 和 2.0 中,使用此方法应用于类型的声明性安全属性以旧的 XML 元数据格式存储。