TypeBuilder.AddDeclarativeSecurity(SecurityAction, PermissionSet) Metode
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Menambahkan keamanan deklaratif ke tipe ini.
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)
Parameter
- action
- SecurityAction
Tindakan keamanan yang akan diambil seperti Demand, Assert, dan sebagainya.
- pset
- PermissionSet
Kumpulan izin yang berlaku untuk tindakan.
Pengecualian
action
tidak valid (RequestMinimum
, RequestOptional
, dan RequestRefuse
tidak valid).
Jenis yang berisi telah dibuat menggunakan CreateType().
-atau-
Kumpulan pset
izin berisi tindakan yang ditambahkan sebelumnya oleh AddDeclarativeSecurity
.
pset
adalah null
.
Contoh
Contoh berikut menunjukkan penggunaan AddDeclarativeSecurity
metode untuk menambahkan permintaan SecurityPermission keamanan dengan SecurityPermissionFlag.ControlEvidence bendera ke jenis dinamis bernama MyDynamicClass
, dalam rakitan bernama EmittedExample.dll. Contohnya tidak menghasilkan output konsol; setelah menjalankannya, Anda dapat menggunakan Ildasm.exe (IL Disassembler) untuk memeriksa EmittedExample.dll. Di MyDynamicClass
, buka .class public auto ansi
pernyataan untuk melihat izin deklaratif.
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
Keterangan
AddDeclarativeSecurity
dapat dipanggil beberapa kali dengan setiap panggilan yang menentukan tindakan keamanan (seperti Permintaan, Pernyataan, atau Tolak) dan sekumpulan izin yang berlaku untuk tindakan.
Catatan
Dalam .NET Framework versi 1.0, 1.1, dan 2.0, atribut keamanan deklaratif yang diterapkan ke jenis dengan menggunakan metode ini disimpan dalam format metadata XML lama.