AppDomain.DefineDynamicAssembly 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.
Menentukan rakitan dinamis di domain aplikasi saat ini.
Overload
DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String, Evidence, PermissionSet, PermissionSet, PermissionSet, Boolean, IEnumerable<CustomAttributeBuilder>) |
Kedaluwarsa.
Menentukan rakitan dinamis dengan nama, mode akses, direktori penyimpanan, bukti, permintaan izin, opsi sinkronisasi, dan atribut kustom yang ditentukan. |
DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String, Evidence, PermissionSet, PermissionSet, PermissionSet, Boolean) |
Kedaluwarsa.
Menentukan rakitan dinamis menggunakan nama, mode akses, direktori penyimpanan, bukti, permintaan izin, dan opsi sinkronisasi yang ditentukan. |
DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String, Evidence, PermissionSet, PermissionSet, PermissionSet) |
Kedaluwarsa.
Menentukan rakitan dinamis menggunakan nama, mode akses, direktori penyimpanan, bukti, dan permintaan izin yang ditentukan. |
DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String, PermissionSet, PermissionSet, PermissionSet) |
Kedaluwarsa.
Menentukan rakitan dinamis menggunakan nama, mode akses, direktori penyimpanan, dan permintaan izin yang ditentukan. |
DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, Evidence, PermissionSet, PermissionSet, PermissionSet) |
Kedaluwarsa.
Menentukan rakitan dinamis menggunakan nama, mode akses, bukti, dan permintaan izin yang ditentukan. |
DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String, Boolean, IEnumerable<CustomAttributeBuilder>) |
Menentukan rakitan dinamis menggunakan opsi nama, mode akses, direktori penyimpanan, dan sinkronisasi yang ditentukan. |
DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, PermissionSet, PermissionSet, PermissionSet) |
Kedaluwarsa.
Menentukan rakitan dinamis menggunakan nama, mode akses, dan permintaan izin yang ditentukan. |
DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String, Evidence) |
Kedaluwarsa.
Menentukan rakitan dinamis menggunakan nama, mode akses, direktori penyimpanan, dan bukti yang ditentukan. |
DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, IEnumerable<CustomAttributeBuilder>, SecurityContextSource) |
Menentukan rakitan dinamis dengan nama, mode akses, dan atribut kustom yang ditentukan, dan menggunakan sumber yang ditentukan untuk konteks keamanannya. |
DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String) |
Menentukan rakitan dinamis menggunakan nama, mode akses, dan direktori penyimpanan yang ditentukan. |
DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, Evidence) |
Kedaluwarsa.
Mendefinisikan rakitan dinamis menggunakan nama, mode akses, dan bukti yang ditentukan. |
DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, IEnumerable<CustomAttributeBuilder>) |
Menentukan rakitan dinamis dengan nama, mode akses, dan atribut kustom yang ditentukan. |
DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess) |
Menentukan rakitan dinamis dengan nama dan mode akses yang ditentukan. |
DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String, Evidence, PermissionSet, PermissionSet, PermissionSet, Boolean, IEnumerable<CustomAttributeBuilder>)
Perhatian
Assembly level declarative security is obsolete and is no longer enforced by the CLR by default. See http://go.microsoft.com/fwlink/?LinkID=155570 for more information.
Menentukan rakitan dinamis dengan nama, mode akses, direktori penyimpanan, bukti, permintaan izin, opsi sinkronisasi, dan atribut kustom yang ditentukan.
public:
System::Reflection::Emit::AssemblyBuilder ^ DefineDynamicAssembly(System::Reflection::AssemblyName ^ name, System::Reflection::Emit::AssemblyBuilderAccess access, System::String ^ dir, System::Security::Policy::Evidence ^ evidence, System::Security::PermissionSet ^ requiredPermissions, System::Security::PermissionSet ^ optionalPermissions, System::Security::PermissionSet ^ refusedPermissions, bool isSynchronized, System::Collections::Generic::IEnumerable<System::Reflection::Emit::CustomAttributeBuilder ^> ^ assemblyAttributes);
public System.Reflection.Emit.AssemblyBuilder DefineDynamicAssembly (System.Reflection.AssemblyName name, System.Reflection.Emit.AssemblyBuilderAccess access, string dir, System.Security.Policy.Evidence evidence, System.Security.PermissionSet requiredPermissions, System.Security.PermissionSet optionalPermissions, System.Security.PermissionSet refusedPermissions, bool isSynchronized, System.Collections.Generic.IEnumerable<System.Reflection.Emit.CustomAttributeBuilder> assemblyAttributes);
[System.Obsolete("Assembly level declarative security is obsolete and is no longer enforced by the CLR by default. See http://go.microsoft.com/fwlink/?LinkID=155570 for more information.")]
public System.Reflection.Emit.AssemblyBuilder DefineDynamicAssembly (System.Reflection.AssemblyName name, System.Reflection.Emit.AssemblyBuilderAccess access, string dir, System.Security.Policy.Evidence evidence, System.Security.PermissionSet requiredPermissions, System.Security.PermissionSet optionalPermissions, System.Security.PermissionSet refusedPermissions, bool isSynchronized, System.Collections.Generic.IEnumerable<System.Reflection.Emit.CustomAttributeBuilder> assemblyAttributes);
member this.DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * string * System.Security.Policy.Evidence * System.Security.PermissionSet * System.Security.PermissionSet * System.Security.PermissionSet * bool * seq<System.Reflection.Emit.CustomAttributeBuilder> -> System.Reflection.Emit.AssemblyBuilder
[<System.Obsolete("Assembly level declarative security is obsolete and is no longer enforced by the CLR by default. See http://go.microsoft.com/fwlink/?LinkID=155570 for more information.")>]
member this.DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * string * System.Security.Policy.Evidence * System.Security.PermissionSet * System.Security.PermissionSet * System.Security.PermissionSet * bool * seq<System.Reflection.Emit.CustomAttributeBuilder> -> System.Reflection.Emit.AssemblyBuilder
Public Function DefineDynamicAssembly (name As AssemblyName, access As AssemblyBuilderAccess, dir As String, evidence As Evidence, requiredPermissions As PermissionSet, optionalPermissions As PermissionSet, refusedPermissions As PermissionSet, isSynchronized As Boolean, assemblyAttributes As IEnumerable(Of CustomAttributeBuilder)) As AssemblyBuilder
Parameter
- name
- AssemblyName
Identitas unik rakitan dinamis.
- access
- AssemblyBuilderAccess
Mode di mana rakitan dinamis akan diakses.
- dir
- String
Nama direktori tempat rakitan dinamis akan disimpan. Jika dir
adalah null
, direktori saat ini digunakan.
- evidence
- Evidence
Bukti yang disediakan untuk rakitan dinamis. Bukti digunakan tidak berubah sebagai set bukti akhir yang digunakan untuk resolusi kebijakan.
- requiredPermissions
- PermissionSet
Permintaan izin yang diperlukan.
- optionalPermissions
- PermissionSet
Permintaan izin opsional.
- refusedPermissions
- PermissionSet
Permintaan izin yang ditolak.
- isSynchronized
- Boolean
true
untuk menyinkronkan pembuatan modul, jenis, dan anggota di perakitan dinamis; jika tidak, false
.
- assemblyAttributes
- IEnumerable<CustomAttributeBuilder>
Daftar atribut yang dapat dijumlahkan untuk diterapkan ke assembly, atau null
jika tidak ada atribut.
Mengembalikan
Rakitan dinamis dengan nama dan fitur yang ditentukan.
- Atribut
Pengecualian
name
adalah null
.
Properti Name
dari name
adalah null
.
-atau-
Name
Properti dimulai name
dengan spasi kosong, atau berisi garis miring ke depan atau mundur.
Operasi ini dicoba pada domain aplikasi yang dibongkar.
Keterangan
Gunakan metode ini kelebihan beban untuk menentukan atribut yang tidak berfungsi dengan benar kecuali jika diterapkan saat rakitan dinamis dibuat. Misalnya, atribut keamanan seperti SecurityTransparentAttribute dan SecurityCriticalAttribute tidak berfungsi dengan benar jika ditambahkan setelah rakitan dinamis dibuat.
Permintaan izin yang ditentukan untuk requiredPermissions
parameter , , optionalPermissions
dan refusedPermissions
hanya digunakan jika evidence
parameter juga disediakan, atau jika rakitan dinamis disimpan dan dimuat ulang ke dalam memori.
Catatan
Saat Anda mengembangkan kode yang memancarkan rakitan dinamis, kami sarankan Anda menyertakan SecurityPermissionFlag.SkipVerification bendera dalam refusedPermissions
parameter . Penyertaan bendera ini memastikan bahwa bahasa perantara Microsoft (MSIL) akan diverifikasi. Teknik ini akan mendeteksi generasi kode yang tidak disengaja dari kode yang tidak dapat diverifikasi, yang sebaliknya sangat sulit dideteksi. Keterbatasan teknik ini adalah juga menyebabkan SecurityException dilemparkan ketika digunakan dengan kode yang menuntut kepercayaan penuh.
Hanya penelepon yang sepenuhnya tepercaya yang dapat memberikan bukti saat menentukan dinamis Assembly. Runtime memetakan Evidence melalui kebijakan keamanan untuk menentukan izin yang diberikan. Penelepon tepercaya sebagian harus menyediakan null
parameter evidence
. Jika evidence
adalah null
, runtime menyalin kumpulan izin (yaitu, pemberian saat ini dan set tolak) dari rakitan pemanggil ke rakitan dinamis yang sedang ditentukan, dan menandai kebijakan sebagai diselesaikan.
Jika rakitan dinamis disimpan ke disk, beban berikutnya akan mendapatkan hibah berdasarkan kebijakan yang terkait dengan lokasi tempat perakitan dinamis disimpan.
Jika isSynchronized
adalah true
, metode berikut dari yang dihasilkan AssemblyBuilder akan disinkronkan: DefineDynamicModule, , DefineResource, AddResourceFileGetDynamicModule, SetEntryPoint, dan Save. Jika dua metode ini dipanggil pada utas yang berbeda, satu akan memblokir sampai yang lain selesai.
Kelebihan beban metode ini diperkenalkan dalam .NET Framework 3.5.
Lihat juga
Berlaku untuk
DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String, Evidence, PermissionSet, PermissionSet, PermissionSet, Boolean)
Perhatian
Assembly level declarative security is obsolete and is no longer enforced by the CLR by default. See http://go.microsoft.com/fwlink/?LinkID=155570 for more information.
Menentukan rakitan dinamis menggunakan nama, mode akses, direktori penyimpanan, bukti, permintaan izin, dan opsi sinkronisasi yang ditentukan.
public:
virtual System::Reflection::Emit::AssemblyBuilder ^ DefineDynamicAssembly(System::Reflection::AssemblyName ^ name, System::Reflection::Emit::AssemblyBuilderAccess access, System::String ^ dir, System::Security::Policy::Evidence ^ evidence, System::Security::PermissionSet ^ requiredPermissions, System::Security::PermissionSet ^ optionalPermissions, System::Security::PermissionSet ^ refusedPermissions, bool isSynchronized);
public System.Reflection.Emit.AssemblyBuilder DefineDynamicAssembly (System.Reflection.AssemblyName name, System.Reflection.Emit.AssemblyBuilderAccess access, string dir, System.Security.Policy.Evidence evidence, System.Security.PermissionSet requiredPermissions, System.Security.PermissionSet optionalPermissions, System.Security.PermissionSet refusedPermissions, bool isSynchronized);
[System.Obsolete("Assembly level declarative security is obsolete and is no longer enforced by the CLR by default. See http://go.microsoft.com/fwlink/?LinkID=155570 for more information.")]
public System.Reflection.Emit.AssemblyBuilder DefineDynamicAssembly (System.Reflection.AssemblyName name, System.Reflection.Emit.AssemblyBuilderAccess access, string dir, System.Security.Policy.Evidence evidence, System.Security.PermissionSet requiredPermissions, System.Security.PermissionSet optionalPermissions, System.Security.PermissionSet refusedPermissions, bool isSynchronized);
abstract member DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * string * System.Security.Policy.Evidence * System.Security.PermissionSet * System.Security.PermissionSet * System.Security.PermissionSet * bool -> System.Reflection.Emit.AssemblyBuilder
override this.DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * string * System.Security.Policy.Evidence * System.Security.PermissionSet * System.Security.PermissionSet * System.Security.PermissionSet * bool -> System.Reflection.Emit.AssemblyBuilder
[<System.Obsolete("Assembly level declarative security is obsolete and is no longer enforced by the CLR by default. See http://go.microsoft.com/fwlink/?LinkID=155570 for more information.")>]
abstract member DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * string * System.Security.Policy.Evidence * System.Security.PermissionSet * System.Security.PermissionSet * System.Security.PermissionSet * bool -> System.Reflection.Emit.AssemblyBuilder
override this.DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * string * System.Security.Policy.Evidence * System.Security.PermissionSet * System.Security.PermissionSet * System.Security.PermissionSet * bool -> System.Reflection.Emit.AssemblyBuilder
Public Function DefineDynamicAssembly (name As AssemblyName, access As AssemblyBuilderAccess, dir As String, evidence As Evidence, requiredPermissions As PermissionSet, optionalPermissions As PermissionSet, refusedPermissions As PermissionSet, isSynchronized As Boolean) As AssemblyBuilder
Parameter
- name
- AssemblyName
Identitas unik rakitan dinamis.
- access
- AssemblyBuilderAccess
Mode di mana rakitan dinamis akan diakses.
- dir
- String
Nama direktori tempat rakitan dinamis akan disimpan. Jika dir
adalah null
, direktori default ke direktori saat ini.
- evidence
- Evidence
Bukti yang diberikan untuk rakitan dinamis. Bukti digunakan tidak berubah sebagai set bukti akhir yang digunakan untuk resolusi kebijakan.
- requiredPermissions
- PermissionSet
Permintaan izin yang diperlukan.
- optionalPermissions
- PermissionSet
Permintaan izin opsional.
- refusedPermissions
- PermissionSet
Permintaan izin yang ditolak.
- isSynchronized
- Boolean
true
untuk menyinkronkan pembuatan modul, jenis, dan anggota di perakitan dinamis; jika tidak, false
.
Mengembalikan
Rakitan dinamis dengan nama dan fitur yang ditentukan.
Penerapan
- Atribut
Pengecualian
name
adalah null
.
Properti Name
dari name
adalah null
.
-atau-
Name
Properti dimulai name
dengan spasi putih, atau berisi garis miring ke depan atau mundur.
Operasi ini dicoba pada domain aplikasi yang dibongkar.
Contoh
Sampel berikut menunjukkan DefineDynamicAssembly metode dan AssemblyResolve peristiwa.
Agar contoh kode ini berjalan, Anda harus memberikan nama rakitan yang sepenuhnya memenuhi syarat. Untuk informasi tentang cara mendapatkan nama rakitan yang sepenuhnya memenuhi syarat, lihat Nama Rakitan.
using namespace System;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
ref class Test
{
public:
static void InstantiateMyDynamicType( AppDomain^ domain )
{
try
{
// You must supply a valid fully qualified assembly name here.
domain->CreateInstance( "Assembly text name, Version, Culture, PublicKeyToken", "MyDynamicType" );
}
catch ( Exception^ e )
{
Console::WriteLine( e->Message );
}
}
static Assembly^ MyResolveEventHandler( Object^ sender, ResolveEventArgs^ args )
{
return DefineDynamicAssembly( dynamic_cast<AppDomain^>(sender) );
}
static Assembly^ DefineDynamicAssembly( AppDomain^ domain )
{
// Build a dynamic assembly using Reflection Emit API.
AssemblyName^ assemblyName = gcnew AssemblyName;
assemblyName->Name = "MyDynamicAssembly";
AssemblyBuilder^ assemblyBuilder = domain->DefineDynamicAssembly( assemblyName, AssemblyBuilderAccess::Run );
ModuleBuilder^ moduleBuilder = assemblyBuilder->DefineDynamicModule( "MyDynamicModule" );
TypeBuilder^ typeBuilder = moduleBuilder->DefineType( "MyDynamicType", TypeAttributes::Public );
ConstructorBuilder^ constructorBuilder = typeBuilder->DefineConstructor( MethodAttributes::Public, CallingConventions::Standard, nullptr );
ILGenerator^ ilGenerator = constructorBuilder->GetILGenerator();
ilGenerator->EmitWriteLine( "MyDynamicType instantiated!" );
ilGenerator->Emit( OpCodes::Ret );
typeBuilder->CreateType();
return assemblyBuilder;
}
};
int main()
{
AppDomain^ currentDomain = AppDomain::CurrentDomain;
Test::InstantiateMyDynamicType( currentDomain ); // Failed!
currentDomain->AssemblyResolve += gcnew ResolveEventHandler( Test::MyResolveEventHandler );
Test::InstantiateMyDynamicType( currentDomain ); // OK!
}
using System;
using System.Reflection;
using System.Reflection.Emit;
class DefinedDynamicAssemblySnippet {
public static void Main() {
AppDomain currentDomain = AppDomain.CurrentDomain;
InstantiateMyDynamicType(currentDomain); // Failed!
currentDomain.AssemblyResolve += new ResolveEventHandler(MyResolveEventHandler);
InstantiateMyDynamicType(currentDomain); // OK!
}
static void InstantiateMyDynamicType(AppDomain domain) {
try {
// You must supply a valid fully qualified assembly name here.
domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyDynamicType");
} catch (Exception e) {
Console.WriteLine(e.Message);
}
}
static Assembly MyResolveEventHandler(object sender, ResolveEventArgs args) {
return DefineDynamicAssembly((AppDomain) sender);
}
static Assembly DefineDynamicAssembly(AppDomain domain) {
// Build a dynamic assembly using Reflection Emit API.
AssemblyName assemblyName = new AssemblyName();
assemblyName.Name = "MyDynamicAssembly";
AssemblyBuilder assemblyBuilder = domain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("MyDynamicModule");
TypeBuilder typeBuilder = moduleBuilder.DefineType("MyDynamicType", TypeAttributes.Public);
ConstructorBuilder constructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, null);
ILGenerator ilGenerator = constructorBuilder.GetILGenerator();
ilGenerator.EmitWriteLine("MyDynamicType instantiated!");
ilGenerator.Emit(OpCodes.Ret);
typeBuilder.CreateType();
return assemblyBuilder;
}
}
open System
open System.Reflection
open System.Reflection.Emit
let instantiateMyDynamicType (domain: AppDomain) =
try
// You must supply a valid fully qualified assembly name here.
domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyDynamicType")
|> ignore
with e ->
printfn $"{e.Message}"
let defineDynamicAssembly (domain: AppDomain) =
// Build a dynamic assembly using Reflection Emit API.
let assemblyName = AssemblyName()
assemblyName.Name <- "MyDynamicAssembly"
let assemblyBuilder = domain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run)
let moduleBuilder = assemblyBuilder.DefineDynamicModule "MyDynamicModule"
let typeBuilder = moduleBuilder.DefineType("MyDynamicType", TypeAttributes.Public)
let constructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, null)
let ilGenerator = constructorBuilder.GetILGenerator()
ilGenerator.EmitWriteLine "MyDynamicType instantiated!"
ilGenerator.Emit OpCodes.Ret
typeBuilder.CreateType() |> ignore
assemblyBuilder
let myResolveEventHandler (sender: obj) _ =
defineDynamicAssembly (sender :?> AppDomain)
:> Assembly
let currentDomain = AppDomain.CurrentDomain
instantiateMyDynamicType currentDomain // Failed!
currentDomain.add_AssemblyResolve(ResolveEventHandler myResolveEventHandler)
instantiateMyDynamicType currentDomain // OK!
Imports System.Reflection
Imports System.Reflection.Emit
Module Test
Sub Main()
Dim currentDomain As AppDomain = AppDomain.CurrentDomain
InstantiateMyDynamicType(currentDomain) 'Failed!
AddHandler currentDomain.AssemblyResolve, AddressOf MyResolveEventHandler
InstantiateMyDynamicType(currentDomain) 'OK!
End Sub
Sub InstantiateMyDynamicType(domain As AppDomain)
Try
' You must supply a valid fully qualified assembly name here.
domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyDynamicType")
Catch e As Exception
Console.WriteLine(e.Message)
End Try
End Sub
Function MyResolveEventHandler(sender As Object, args As ResolveEventArgs) As System.Reflection.Assembly
Return DefineDynamicAssembly(DirectCast(sender, AppDomain))
End Function 'MyResolveEventHandler
Function DefineDynamicAssembly(domain As AppDomain) As System.Reflection.Assembly
' Build a dynamic assembly using Reflection Emit API.
Dim assemblyName As New AssemblyName()
assemblyName.Name = "MyDynamicAssembly"
Dim assemblyBuilder As AssemblyBuilder = domain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run)
Dim moduleBuilder As ModuleBuilder = assemblyBuilder.DefineDynamicModule("MyDynamicModule")
Dim typeBuilder As TypeBuilder = moduleBuilder.DefineType("MyDynamicType", TypeAttributes.Public)
Dim constructorBuilder As ConstructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, Nothing)
Dim ilGenerator As ILGenerator = constructorBuilder.GetILGenerator()
ilGenerator.EmitWriteLine("MyDynamicType instantiated!")
ilGenerator.Emit(OpCodes.Ret)
typeBuilder.CreateType()
Return assemblyBuilder
End Function 'DefineDynamicAssembly
End Module 'Test
Keterangan
Permintaan izin yang ditentukan untuk requiredPermissions
, , dan refusedPermissions
hanya digunakan jika evidence
juga disediakan, atau jika rakitan dinamis disimpan dan dimuat ulang optionalPermissions
ke dalam memori.
Catatan
Selama pengembangan kode yang memancarkan rakitan dinamis, disarankan agar Anda menyertakan SecurityPermissionFlag.SkipVerification dalam refusedPermissions
. Termasuk SkipVerification dalam refusedPermissions
parameter memastikan bahwa MSIL diverifikasi. Batasan teknik ini adalah juga menyebabkan SecurityException dilemparkan ketika digunakan dengan kode yang menuntut kepercayaan penuh.
Hanya penelepon yang sepenuhnya tepercaya yang dapat memberikan bukti mereka ketika menentukan dinamis Assembly. Runtime akan memetakan Evidence melalui kebijakan keamanan untuk menentukan izin yang diberikan. Penelepon tepercaya sebagian harus menyediakan null
parameter evidence
. Jika evidence
adalah null
, runtime menyalin set izin, yaitu, set pemberian dan tolak saat ini, dari pemanggil Assembly hingga dinamis Assembly yang ditentukan dan menandai kebijakan sebagai diselesaikan.
Jika dinamis Assembly disimpan ke disk, beban berikutnya akan mendapatkan hibah berdasarkan kebijakan yang terkait dengan lokasi tempat Assembly penyimpanan.
Jika isSynchronized
adalah true
, metode berikut dari yang dihasilkan AssemblyBuilder akan disinkronkan: DefineDynamicModule, , DefineResource, AddResourceFileGetDynamicModule, SetEntryPoint, dan Save. Jika dua metode ini dipanggil pada utas yang berbeda, satu akan memblokir sampai yang lain selesai.
Berlaku untuk
DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String, Evidence, PermissionSet, PermissionSet, PermissionSet)
Perhatian
Assembly level declarative security is obsolete and is no longer enforced by the CLR by default. Please see http://go.microsoft.com/fwlink/?LinkId=155570 for more information.
Menentukan rakitan dinamis menggunakan nama, mode akses, direktori penyimpanan, bukti, dan permintaan izin yang ditentukan.
public:
virtual System::Reflection::Emit::AssemblyBuilder ^ DefineDynamicAssembly(System::Reflection::AssemblyName ^ name, System::Reflection::Emit::AssemblyBuilderAccess access, System::String ^ dir, System::Security::Policy::Evidence ^ evidence, System::Security::PermissionSet ^ requiredPermissions, System::Security::PermissionSet ^ optionalPermissions, System::Security::PermissionSet ^ refusedPermissions);
public System.Reflection.Emit.AssemblyBuilder DefineDynamicAssembly (System.Reflection.AssemblyName name, System.Reflection.Emit.AssemblyBuilderAccess access, string dir, System.Security.Policy.Evidence evidence, System.Security.PermissionSet requiredPermissions, System.Security.PermissionSet optionalPermissions, System.Security.PermissionSet refusedPermissions);
[System.Obsolete("Assembly level declarative security is obsolete and is no longer enforced by the CLR by default. Please see http://go.microsoft.com/fwlink/?LinkId=155570 for more information.")]
public System.Reflection.Emit.AssemblyBuilder DefineDynamicAssembly (System.Reflection.AssemblyName name, System.Reflection.Emit.AssemblyBuilderAccess access, string dir, System.Security.Policy.Evidence evidence, System.Security.PermissionSet requiredPermissions, System.Security.PermissionSet optionalPermissions, System.Security.PermissionSet refusedPermissions);
abstract member DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * string * System.Security.Policy.Evidence * System.Security.PermissionSet * System.Security.PermissionSet * System.Security.PermissionSet -> System.Reflection.Emit.AssemblyBuilder
override this.DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * string * System.Security.Policy.Evidence * System.Security.PermissionSet * System.Security.PermissionSet * System.Security.PermissionSet -> System.Reflection.Emit.AssemblyBuilder
[<System.Obsolete("Assembly level declarative security is obsolete and is no longer enforced by the CLR by default. Please see http://go.microsoft.com/fwlink/?LinkId=155570 for more information.")>]
abstract member DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * string * System.Security.Policy.Evidence * System.Security.PermissionSet * System.Security.PermissionSet * System.Security.PermissionSet -> System.Reflection.Emit.AssemblyBuilder
override this.DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * string * System.Security.Policy.Evidence * System.Security.PermissionSet * System.Security.PermissionSet * System.Security.PermissionSet -> System.Reflection.Emit.AssemblyBuilder
Public Function DefineDynamicAssembly (name As AssemblyName, access As AssemblyBuilderAccess, dir As String, evidence As Evidence, requiredPermissions As PermissionSet, optionalPermissions As PermissionSet, refusedPermissions As PermissionSet) As AssemblyBuilder
Parameter
- name
- AssemblyName
Identitas unik rakitan dinamis.
- access
- AssemblyBuilderAccess
Mode di mana rakitan dinamis akan diakses.
- dir
- String
Nama direktori tempat assembly akan disimpan. Jika dir
adalah null
, direktori default ke direktori saat ini.
- evidence
- Evidence
Bukti yang diberikan untuk rakitan dinamis. Bukti digunakan tidak berubah sebagai set bukti akhir yang digunakan untuk resolusi kebijakan.
- requiredPermissions
- PermissionSet
Permintaan izin yang diperlukan.
- optionalPermissions
- PermissionSet
Permintaan izin opsional.
- refusedPermissions
- PermissionSet
Permintaan izin yang ditolak.
Mengembalikan
Rakitan dinamis dengan nama dan fitur yang ditentukan.
Penerapan
- Atribut
Pengecualian
name
adalah null
.
Properti Name
dari name
adalah null
.
-atau-
Name
Properti dimulai name
dengan spasi putih, atau berisi garis miring ke depan atau mundur.
Operasi ini dicoba pada domain aplikasi yang dibongkar.
Contoh
Sampel berikut menunjukkan DefineDynamicAssembly metode dan AssemblyResolve peristiwa.
Agar contoh kode ini berjalan, Anda harus memberikan nama rakitan yang sepenuhnya memenuhi syarat. Untuk informasi tentang cara mendapatkan nama rakitan yang sepenuhnya memenuhi syarat, lihat Nama Rakitan.
using namespace System;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
ref class Test
{
public:
static void InstantiateMyDynamicType( AppDomain^ domain )
{
try
{
// You must supply a valid fully qualified assembly name here.
domain->CreateInstance( "Assembly text name, Version, Culture, PublicKeyToken", "MyDynamicType" );
}
catch ( Exception^ e )
{
Console::WriteLine( e->Message );
}
}
static Assembly^ MyResolveEventHandler( Object^ sender, ResolveEventArgs^ args )
{
return DefineDynamicAssembly( dynamic_cast<AppDomain^>(sender) );
}
static Assembly^ DefineDynamicAssembly( AppDomain^ domain )
{
// Build a dynamic assembly using Reflection Emit API.
AssemblyName^ assemblyName = gcnew AssemblyName;
assemblyName->Name = "MyDynamicAssembly";
AssemblyBuilder^ assemblyBuilder = domain->DefineDynamicAssembly( assemblyName, AssemblyBuilderAccess::Run );
ModuleBuilder^ moduleBuilder = assemblyBuilder->DefineDynamicModule( "MyDynamicModule" );
TypeBuilder^ typeBuilder = moduleBuilder->DefineType( "MyDynamicType", TypeAttributes::Public );
ConstructorBuilder^ constructorBuilder = typeBuilder->DefineConstructor( MethodAttributes::Public, CallingConventions::Standard, nullptr );
ILGenerator^ ilGenerator = constructorBuilder->GetILGenerator();
ilGenerator->EmitWriteLine( "MyDynamicType instantiated!" );
ilGenerator->Emit( OpCodes::Ret );
typeBuilder->CreateType();
return assemblyBuilder;
}
};
int main()
{
AppDomain^ currentDomain = AppDomain::CurrentDomain;
Test::InstantiateMyDynamicType( currentDomain ); // Failed!
currentDomain->AssemblyResolve += gcnew ResolveEventHandler( Test::MyResolveEventHandler );
Test::InstantiateMyDynamicType( currentDomain ); // OK!
}
using System;
using System.Reflection;
using System.Reflection.Emit;
class DefinedDynamicAssemblySnippet {
public static void Main() {
AppDomain currentDomain = AppDomain.CurrentDomain;
InstantiateMyDynamicType(currentDomain); // Failed!
currentDomain.AssemblyResolve += new ResolveEventHandler(MyResolveEventHandler);
InstantiateMyDynamicType(currentDomain); // OK!
}
static void InstantiateMyDynamicType(AppDomain domain) {
try {
// You must supply a valid fully qualified assembly name here.
domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyDynamicType");
} catch (Exception e) {
Console.WriteLine(e.Message);
}
}
static Assembly MyResolveEventHandler(object sender, ResolveEventArgs args) {
return DefineDynamicAssembly((AppDomain) sender);
}
static Assembly DefineDynamicAssembly(AppDomain domain) {
// Build a dynamic assembly using Reflection Emit API.
AssemblyName assemblyName = new AssemblyName();
assemblyName.Name = "MyDynamicAssembly";
AssemblyBuilder assemblyBuilder = domain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("MyDynamicModule");
TypeBuilder typeBuilder = moduleBuilder.DefineType("MyDynamicType", TypeAttributes.Public);
ConstructorBuilder constructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, null);
ILGenerator ilGenerator = constructorBuilder.GetILGenerator();
ilGenerator.EmitWriteLine("MyDynamicType instantiated!");
ilGenerator.Emit(OpCodes.Ret);
typeBuilder.CreateType();
return assemblyBuilder;
}
}
open System
open System.Reflection
open System.Reflection.Emit
let instantiateMyDynamicType (domain: AppDomain) =
try
// You must supply a valid fully qualified assembly name here.
domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyDynamicType")
|> ignore
with e ->
printfn $"{e.Message}"
let defineDynamicAssembly (domain: AppDomain) =
// Build a dynamic assembly using Reflection Emit API.
let assemblyName = AssemblyName()
assemblyName.Name <- "MyDynamicAssembly"
let assemblyBuilder = domain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run)
let moduleBuilder = assemblyBuilder.DefineDynamicModule "MyDynamicModule"
let typeBuilder = moduleBuilder.DefineType("MyDynamicType", TypeAttributes.Public)
let constructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, null)
let ilGenerator = constructorBuilder.GetILGenerator()
ilGenerator.EmitWriteLine "MyDynamicType instantiated!"
ilGenerator.Emit OpCodes.Ret
typeBuilder.CreateType() |> ignore
assemblyBuilder
let myResolveEventHandler (sender: obj) _ =
defineDynamicAssembly (sender :?> AppDomain)
:> Assembly
let currentDomain = AppDomain.CurrentDomain
instantiateMyDynamicType currentDomain // Failed!
currentDomain.add_AssemblyResolve(ResolveEventHandler myResolveEventHandler)
instantiateMyDynamicType currentDomain // OK!
Imports System.Reflection
Imports System.Reflection.Emit
Module Test
Sub Main()
Dim currentDomain As AppDomain = AppDomain.CurrentDomain
InstantiateMyDynamicType(currentDomain) 'Failed!
AddHandler currentDomain.AssemblyResolve, AddressOf MyResolveEventHandler
InstantiateMyDynamicType(currentDomain) 'OK!
End Sub
Sub InstantiateMyDynamicType(domain As AppDomain)
Try
' You must supply a valid fully qualified assembly name here.
domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyDynamicType")
Catch e As Exception
Console.WriteLine(e.Message)
End Try
End Sub
Function MyResolveEventHandler(sender As Object, args As ResolveEventArgs) As System.Reflection.Assembly
Return DefineDynamicAssembly(DirectCast(sender, AppDomain))
End Function 'MyResolveEventHandler
Function DefineDynamicAssembly(domain As AppDomain) As System.Reflection.Assembly
' Build a dynamic assembly using Reflection Emit API.
Dim assemblyName As New AssemblyName()
assemblyName.Name = "MyDynamicAssembly"
Dim assemblyBuilder As AssemblyBuilder = domain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run)
Dim moduleBuilder As ModuleBuilder = assemblyBuilder.DefineDynamicModule("MyDynamicModule")
Dim typeBuilder As TypeBuilder = moduleBuilder.DefineType("MyDynamicType", TypeAttributes.Public)
Dim constructorBuilder As ConstructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, Nothing)
Dim ilGenerator As ILGenerator = constructorBuilder.GetILGenerator()
ilGenerator.EmitWriteLine("MyDynamicType instantiated!")
ilGenerator.Emit(OpCodes.Ret)
typeBuilder.CreateType()
Return assemblyBuilder
End Function 'DefineDynamicAssembly
End Module 'Test
Keterangan
Permintaan izin yang ditentukan untuk requiredPermissions
, , dan refusedPermissions
hanya digunakan jika evidence
juga disediakan, atau jika rakitan dinamis disimpan dan dimuat ulang optionalPermissions
ke dalam memori.
Catatan
Selama pengembangan kode yang memancarkan rakitan dinamis, disarankan agar Anda menyertakan SecurityPermissionFlag.SkipVerification dalam refusedPermissions
. Termasuk SkipVerification dalam refusedPermissions
parameter memastikan bahwa MSIL diverifikasi. Batasan teknik ini adalah juga menyebabkan SecurityException dilemparkan ketika digunakan dengan kode yang menuntut kepercayaan penuh.
Hanya penelepon yang sepenuhnya tepercaya yang dapat menyediakannya evidence
saat menentukan dinamis Assembly. Runtime akan memetakan Evidence melalui kebijakan keamanan untuk menentukan izin yang diberikan. Penelepon tepercaya sebagian harus menyediakan null evidence
. Jika evidence
adalah null
, runtime menyalin set izin, yaitu, set pemberian dan tolak saat ini, dari pemanggil Assembly hingga dinamis Assembly yang ditentukan dan menandai kebijakan sebagai diselesaikan.
Jika dinamis Assembly disimpan ke disk, beban berikutnya akan mendapatkan hibah berdasarkan kebijakan yang terkait dengan lokasi tempat Assembly penyimpanan.
Metode ini hanya boleh digunakan untuk menentukan rakitan dinamis di domain aplikasi saat ini. Untuk informasi selengkapnya, lihat Load(AssemblyName) metode kelebihan beban.
Berlaku untuk
DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String, PermissionSet, PermissionSet, PermissionSet)
Perhatian
Assembly level declarative security is obsolete and is no longer enforced by the CLR by default. See http://go.microsoft.com/fwlink/?LinkID=155570 for more information.
Menentukan rakitan dinamis menggunakan nama, mode akses, direktori penyimpanan, dan permintaan izin yang ditentukan.
public:
virtual System::Reflection::Emit::AssemblyBuilder ^ DefineDynamicAssembly(System::Reflection::AssemblyName ^ name, System::Reflection::Emit::AssemblyBuilderAccess access, System::String ^ dir, System::Security::PermissionSet ^ requiredPermissions, System::Security::PermissionSet ^ optionalPermissions, System::Security::PermissionSet ^ refusedPermissions);
public System.Reflection.Emit.AssemblyBuilder DefineDynamicAssembly (System.Reflection.AssemblyName name, System.Reflection.Emit.AssemblyBuilderAccess access, string dir, System.Security.PermissionSet requiredPermissions, System.Security.PermissionSet optionalPermissions, System.Security.PermissionSet refusedPermissions);
[System.Obsolete("Assembly level declarative security is obsolete and is no longer enforced by the CLR by default. See http://go.microsoft.com/fwlink/?LinkID=155570 for more information.")]
public System.Reflection.Emit.AssemblyBuilder DefineDynamicAssembly (System.Reflection.AssemblyName name, System.Reflection.Emit.AssemblyBuilderAccess access, string dir, System.Security.PermissionSet requiredPermissions, System.Security.PermissionSet optionalPermissions, System.Security.PermissionSet refusedPermissions);
abstract member DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * string * System.Security.PermissionSet * System.Security.PermissionSet * System.Security.PermissionSet -> System.Reflection.Emit.AssemblyBuilder
override this.DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * string * System.Security.PermissionSet * System.Security.PermissionSet * System.Security.PermissionSet -> System.Reflection.Emit.AssemblyBuilder
[<System.Obsolete("Assembly level declarative security is obsolete and is no longer enforced by the CLR by default. See http://go.microsoft.com/fwlink/?LinkID=155570 for more information.")>]
abstract member DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * string * System.Security.PermissionSet * System.Security.PermissionSet * System.Security.PermissionSet -> System.Reflection.Emit.AssemblyBuilder
override this.DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * string * System.Security.PermissionSet * System.Security.PermissionSet * System.Security.PermissionSet -> System.Reflection.Emit.AssemblyBuilder
Public Function DefineDynamicAssembly (name As AssemblyName, access As AssemblyBuilderAccess, dir As String, requiredPermissions As PermissionSet, optionalPermissions As PermissionSet, refusedPermissions As PermissionSet) As AssemblyBuilder
Parameter
- name
- AssemblyName
Identitas unik rakitan dinamis.
- access
- AssemblyBuilderAccess
Mode di mana rakitan dinamis akan diakses.
- dir
- String
Nama direktori tempat assembly akan disimpan. Jika dir
adalah null
, direktori default ke direktori saat ini.
- requiredPermissions
- PermissionSet
Permintaan izin yang diperlukan.
- optionalPermissions
- PermissionSet
Permintaan izin opsional.
- refusedPermissions
- PermissionSet
Permintaan izin yang ditolak.
Mengembalikan
Rakitan dinamis dengan nama dan fitur yang ditentukan.
Penerapan
- Atribut
Pengecualian
name
adalah null
.
Properti Name
dari name
adalah null
.
-atau-
Name
Properti dimulai name
dengan spasi putih, atau berisi garis miring ke depan atau mundur.
Operasi ini dicoba pada domain aplikasi yang dibongkar.
Contoh
Sampel berikut menunjukkan DefineDynamicAssembly metode dan AssemblyResolve peristiwa.
Agar contoh kode ini berjalan, Anda harus memberikan nama rakitan yang sepenuhnya memenuhi syarat. Untuk informasi tentang cara mendapatkan nama rakitan yang sepenuhnya memenuhi syarat, lihat Nama Rakitan.
using namespace System;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
ref class Test
{
public:
static void InstantiateMyDynamicType( AppDomain^ domain )
{
try
{
// You must supply a valid fully qualified assembly name here.
domain->CreateInstance( "Assembly text name, Version, Culture, PublicKeyToken", "MyDynamicType" );
}
catch ( Exception^ e )
{
Console::WriteLine( e->Message );
}
}
static Assembly^ MyResolveEventHandler( Object^ sender, ResolveEventArgs^ args )
{
return DefineDynamicAssembly( dynamic_cast<AppDomain^>(sender) );
}
static Assembly^ DefineDynamicAssembly( AppDomain^ domain )
{
// Build a dynamic assembly using Reflection Emit API.
AssemblyName^ assemblyName = gcnew AssemblyName;
assemblyName->Name = "MyDynamicAssembly";
AssemblyBuilder^ assemblyBuilder = domain->DefineDynamicAssembly( assemblyName, AssemblyBuilderAccess::Run );
ModuleBuilder^ moduleBuilder = assemblyBuilder->DefineDynamicModule( "MyDynamicModule" );
TypeBuilder^ typeBuilder = moduleBuilder->DefineType( "MyDynamicType", TypeAttributes::Public );
ConstructorBuilder^ constructorBuilder = typeBuilder->DefineConstructor( MethodAttributes::Public, CallingConventions::Standard, nullptr );
ILGenerator^ ilGenerator = constructorBuilder->GetILGenerator();
ilGenerator->EmitWriteLine( "MyDynamicType instantiated!" );
ilGenerator->Emit( OpCodes::Ret );
typeBuilder->CreateType();
return assemblyBuilder;
}
};
int main()
{
AppDomain^ currentDomain = AppDomain::CurrentDomain;
Test::InstantiateMyDynamicType( currentDomain ); // Failed!
currentDomain->AssemblyResolve += gcnew ResolveEventHandler( Test::MyResolveEventHandler );
Test::InstantiateMyDynamicType( currentDomain ); // OK!
}
using System;
using System.Reflection;
using System.Reflection.Emit;
class DefinedDynamicAssemblySnippet {
public static void Main() {
AppDomain currentDomain = AppDomain.CurrentDomain;
InstantiateMyDynamicType(currentDomain); // Failed!
currentDomain.AssemblyResolve += new ResolveEventHandler(MyResolveEventHandler);
InstantiateMyDynamicType(currentDomain); // OK!
}
static void InstantiateMyDynamicType(AppDomain domain) {
try {
// You must supply a valid fully qualified assembly name here.
domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyDynamicType");
} catch (Exception e) {
Console.WriteLine(e.Message);
}
}
static Assembly MyResolveEventHandler(object sender, ResolveEventArgs args) {
return DefineDynamicAssembly((AppDomain) sender);
}
static Assembly DefineDynamicAssembly(AppDomain domain) {
// Build a dynamic assembly using Reflection Emit API.
AssemblyName assemblyName = new AssemblyName();
assemblyName.Name = "MyDynamicAssembly";
AssemblyBuilder assemblyBuilder = domain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("MyDynamicModule");
TypeBuilder typeBuilder = moduleBuilder.DefineType("MyDynamicType", TypeAttributes.Public);
ConstructorBuilder constructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, null);
ILGenerator ilGenerator = constructorBuilder.GetILGenerator();
ilGenerator.EmitWriteLine("MyDynamicType instantiated!");
ilGenerator.Emit(OpCodes.Ret);
typeBuilder.CreateType();
return assemblyBuilder;
}
}
open System
open System.Reflection
open System.Reflection.Emit
let instantiateMyDynamicType (domain: AppDomain) =
try
// You must supply a valid fully qualified assembly name here.
domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyDynamicType")
|> ignore
with e ->
printfn $"{e.Message}"
let defineDynamicAssembly (domain: AppDomain) =
// Build a dynamic assembly using Reflection Emit API.
let assemblyName = AssemblyName()
assemblyName.Name <- "MyDynamicAssembly"
let assemblyBuilder = domain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run)
let moduleBuilder = assemblyBuilder.DefineDynamicModule "MyDynamicModule"
let typeBuilder = moduleBuilder.DefineType("MyDynamicType", TypeAttributes.Public)
let constructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, null)
let ilGenerator = constructorBuilder.GetILGenerator()
ilGenerator.EmitWriteLine "MyDynamicType instantiated!"
ilGenerator.Emit OpCodes.Ret
typeBuilder.CreateType() |> ignore
assemblyBuilder
let myResolveEventHandler (sender: obj) _ =
defineDynamicAssembly (sender :?> AppDomain)
:> Assembly
let currentDomain = AppDomain.CurrentDomain
instantiateMyDynamicType currentDomain // Failed!
currentDomain.add_AssemblyResolve(ResolveEventHandler myResolveEventHandler)
instantiateMyDynamicType currentDomain // OK!
Imports System.Reflection
Imports System.Reflection.Emit
Module Test
Sub Main()
Dim currentDomain As AppDomain = AppDomain.CurrentDomain
InstantiateMyDynamicType(currentDomain) 'Failed!
AddHandler currentDomain.AssemblyResolve, AddressOf MyResolveEventHandler
InstantiateMyDynamicType(currentDomain) 'OK!
End Sub
Sub InstantiateMyDynamicType(domain As AppDomain)
Try
' You must supply a valid fully qualified assembly name here.
domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyDynamicType")
Catch e As Exception
Console.WriteLine(e.Message)
End Try
End Sub
Function MyResolveEventHandler(sender As Object, args As ResolveEventArgs) As System.Reflection.Assembly
Return DefineDynamicAssembly(DirectCast(sender, AppDomain))
End Function 'MyResolveEventHandler
Function DefineDynamicAssembly(domain As AppDomain) As System.Reflection.Assembly
' Build a dynamic assembly using Reflection Emit API.
Dim assemblyName As New AssemblyName()
assemblyName.Name = "MyDynamicAssembly"
Dim assemblyBuilder As AssemblyBuilder = domain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run)
Dim moduleBuilder As ModuleBuilder = assemblyBuilder.DefineDynamicModule("MyDynamicModule")
Dim typeBuilder As TypeBuilder = moduleBuilder.DefineType("MyDynamicType", TypeAttributes.Public)
Dim constructorBuilder As ConstructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, Nothing)
Dim ilGenerator As ILGenerator = constructorBuilder.GetILGenerator()
ilGenerator.EmitWriteLine("MyDynamicType instantiated!")
ilGenerator.Emit(OpCodes.Ret)
typeBuilder.CreateType()
Return assemblyBuilder
End Function 'DefineDynamicAssembly
End Module 'Test
Keterangan
Permintaan izin yang ditentukan untuk requiredPermissions
, optionalPermissions
, dan refusedPermissions
tidak digunakan kecuali rakitan dinamis telah disimpan dan dimuat ulang ke dalam memori. Untuk menentukan permintaan izin untuk rakitan sementara yang tidak pernah disimpan ke disk, gunakan kelebihan beban DefineDynamicAssembly metode yang menentukan bukti serta izin yang diminta, dan berikan Evidence objek.
Catatan
Selama pengembangan kode yang memancarkan rakitan dinamis, disarankan agar Anda menggunakan kelebihan beban DefineDynamicAssembly metode yang menentukan bukti dan izin, berikan bukti yang Anda inginkan untuk dimiliki perakitan dinamis, dan sertakan SecurityPermissionFlag.SkipVerification dalam refusedPermissions
. Termasuk SkipVerification dalam refusedPermissions
parameter memastikan bahwa MSIL diverifikasi. Batasan teknik ini adalah juga menyebabkan SecurityException dilemparkan ketika digunakan dengan kode yang menuntut kepercayaan penuh.
Metode ini hanya boleh digunakan untuk menentukan rakitan dinamis di domain aplikasi saat ini. Untuk informasi selengkapnya, lihat Load(AssemblyName) metode kelebihan beban.
Berlaku untuk
DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, Evidence, PermissionSet, PermissionSet, PermissionSet)
Perhatian
Assembly level declarative security is obsolete and is no longer enforced by the CLR by default. See http://go.microsoft.com/fwlink/?LinkID=155570 for more information.
Menentukan rakitan dinamis menggunakan nama, mode akses, bukti, dan permintaan izin yang ditentukan.
public:
virtual System::Reflection::Emit::AssemblyBuilder ^ DefineDynamicAssembly(System::Reflection::AssemblyName ^ name, System::Reflection::Emit::AssemblyBuilderAccess access, System::Security::Policy::Evidence ^ evidence, System::Security::PermissionSet ^ requiredPermissions, System::Security::PermissionSet ^ optionalPermissions, System::Security::PermissionSet ^ refusedPermissions);
public System.Reflection.Emit.AssemblyBuilder DefineDynamicAssembly (System.Reflection.AssemblyName name, System.Reflection.Emit.AssemblyBuilderAccess access, System.Security.Policy.Evidence evidence, System.Security.PermissionSet requiredPermissions, System.Security.PermissionSet optionalPermissions, System.Security.PermissionSet refusedPermissions);
[System.Obsolete("Assembly level declarative security is obsolete and is no longer enforced by the CLR by default. See http://go.microsoft.com/fwlink/?LinkID=155570 for more information.")]
public System.Reflection.Emit.AssemblyBuilder DefineDynamicAssembly (System.Reflection.AssemblyName name, System.Reflection.Emit.AssemblyBuilderAccess access, System.Security.Policy.Evidence evidence, System.Security.PermissionSet requiredPermissions, System.Security.PermissionSet optionalPermissions, System.Security.PermissionSet refusedPermissions);
abstract member DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * System.Security.Policy.Evidence * System.Security.PermissionSet * System.Security.PermissionSet * System.Security.PermissionSet -> System.Reflection.Emit.AssemblyBuilder
override this.DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * System.Security.Policy.Evidence * System.Security.PermissionSet * System.Security.PermissionSet * System.Security.PermissionSet -> System.Reflection.Emit.AssemblyBuilder
[<System.Obsolete("Assembly level declarative security is obsolete and is no longer enforced by the CLR by default. See http://go.microsoft.com/fwlink/?LinkID=155570 for more information.")>]
abstract member DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * System.Security.Policy.Evidence * System.Security.PermissionSet * System.Security.PermissionSet * System.Security.PermissionSet -> System.Reflection.Emit.AssemblyBuilder
override this.DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * System.Security.Policy.Evidence * System.Security.PermissionSet * System.Security.PermissionSet * System.Security.PermissionSet -> System.Reflection.Emit.AssemblyBuilder
Public Function DefineDynamicAssembly (name As AssemblyName, access As AssemblyBuilderAccess, evidence As Evidence, requiredPermissions As PermissionSet, optionalPermissions As PermissionSet, refusedPermissions As PermissionSet) As AssemblyBuilder
Parameter
- name
- AssemblyName
Identitas unik rakitan dinamis.
- access
- AssemblyBuilderAccess
Mode di mana rakitan dinamis akan diakses.
- evidence
- Evidence
Bukti yang diberikan untuk rakitan dinamis. Bukti digunakan tidak berubah sebagai set bukti akhir yang digunakan untuk resolusi kebijakan.
- requiredPermissions
- PermissionSet
Permintaan izin yang diperlukan.
- optionalPermissions
- PermissionSet
Permintaan izin opsional.
- refusedPermissions
- PermissionSet
Permintaan izin yang ditolak.
Mengembalikan
Rakitan dinamis dengan nama dan fitur yang ditentukan.
Penerapan
- Atribut
Pengecualian
name
adalah null
.
Properti Name
dari name
adalah null
.
-atau-
Name
Properti dimulai name
dengan spasi putih, atau berisi garis miring ke depan atau mundur.
Operasi ini dicoba pada domain aplikasi yang dibongkar.
Contoh
Sampel berikut menunjukkan DefineDynamicAssembly metode dan AssemblyResolve peristiwa.
Agar contoh kode ini berjalan, Anda harus memberikan nama rakitan yang sepenuhnya memenuhi syarat. Untuk informasi tentang cara mendapatkan nama rakitan yang sepenuhnya memenuhi syarat, lihat Nama Rakitan.
using namespace System;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
ref class Test
{
public:
static void InstantiateMyDynamicType( AppDomain^ domain )
{
try
{
// You must supply a valid fully qualified assembly name here.
domain->CreateInstance( "Assembly text name, Version, Culture, PublicKeyToken", "MyDynamicType" );
}
catch ( Exception^ e )
{
Console::WriteLine( e->Message );
}
}
static Assembly^ MyResolveEventHandler( Object^ sender, ResolveEventArgs^ args )
{
return DefineDynamicAssembly( dynamic_cast<AppDomain^>(sender) );
}
static Assembly^ DefineDynamicAssembly( AppDomain^ domain )
{
// Build a dynamic assembly using Reflection Emit API.
AssemblyName^ assemblyName = gcnew AssemblyName;
assemblyName->Name = "MyDynamicAssembly";
AssemblyBuilder^ assemblyBuilder = domain->DefineDynamicAssembly( assemblyName, AssemblyBuilderAccess::Run );
ModuleBuilder^ moduleBuilder = assemblyBuilder->DefineDynamicModule( "MyDynamicModule" );
TypeBuilder^ typeBuilder = moduleBuilder->DefineType( "MyDynamicType", TypeAttributes::Public );
ConstructorBuilder^ constructorBuilder = typeBuilder->DefineConstructor( MethodAttributes::Public, CallingConventions::Standard, nullptr );
ILGenerator^ ilGenerator = constructorBuilder->GetILGenerator();
ilGenerator->EmitWriteLine( "MyDynamicType instantiated!" );
ilGenerator->Emit( OpCodes::Ret );
typeBuilder->CreateType();
return assemblyBuilder;
}
};
int main()
{
AppDomain^ currentDomain = AppDomain::CurrentDomain;
Test::InstantiateMyDynamicType( currentDomain ); // Failed!
currentDomain->AssemblyResolve += gcnew ResolveEventHandler( Test::MyResolveEventHandler );
Test::InstantiateMyDynamicType( currentDomain ); // OK!
}
using System;
using System.Reflection;
using System.Reflection.Emit;
class DefinedDynamicAssemblySnippet {
public static void Main() {
AppDomain currentDomain = AppDomain.CurrentDomain;
InstantiateMyDynamicType(currentDomain); // Failed!
currentDomain.AssemblyResolve += new ResolveEventHandler(MyResolveEventHandler);
InstantiateMyDynamicType(currentDomain); // OK!
}
static void InstantiateMyDynamicType(AppDomain domain) {
try {
// You must supply a valid fully qualified assembly name here.
domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyDynamicType");
} catch (Exception e) {
Console.WriteLine(e.Message);
}
}
static Assembly MyResolveEventHandler(object sender, ResolveEventArgs args) {
return DefineDynamicAssembly((AppDomain) sender);
}
static Assembly DefineDynamicAssembly(AppDomain domain) {
// Build a dynamic assembly using Reflection Emit API.
AssemblyName assemblyName = new AssemblyName();
assemblyName.Name = "MyDynamicAssembly";
AssemblyBuilder assemblyBuilder = domain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("MyDynamicModule");
TypeBuilder typeBuilder = moduleBuilder.DefineType("MyDynamicType", TypeAttributes.Public);
ConstructorBuilder constructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, null);
ILGenerator ilGenerator = constructorBuilder.GetILGenerator();
ilGenerator.EmitWriteLine("MyDynamicType instantiated!");
ilGenerator.Emit(OpCodes.Ret);
typeBuilder.CreateType();
return assemblyBuilder;
}
}
open System
open System.Reflection
open System.Reflection.Emit
let instantiateMyDynamicType (domain: AppDomain) =
try
// You must supply a valid fully qualified assembly name here.
domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyDynamicType")
|> ignore
with e ->
printfn $"{e.Message}"
let defineDynamicAssembly (domain: AppDomain) =
// Build a dynamic assembly using Reflection Emit API.
let assemblyName = AssemblyName()
assemblyName.Name <- "MyDynamicAssembly"
let assemblyBuilder = domain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run)
let moduleBuilder = assemblyBuilder.DefineDynamicModule "MyDynamicModule"
let typeBuilder = moduleBuilder.DefineType("MyDynamicType", TypeAttributes.Public)
let constructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, null)
let ilGenerator = constructorBuilder.GetILGenerator()
ilGenerator.EmitWriteLine "MyDynamicType instantiated!"
ilGenerator.Emit OpCodes.Ret
typeBuilder.CreateType() |> ignore
assemblyBuilder
let myResolveEventHandler (sender: obj) _ =
defineDynamicAssembly (sender :?> AppDomain)
:> Assembly
let currentDomain = AppDomain.CurrentDomain
instantiateMyDynamicType currentDomain // Failed!
currentDomain.add_AssemblyResolve(ResolveEventHandler myResolveEventHandler)
instantiateMyDynamicType currentDomain // OK!
Imports System.Reflection
Imports System.Reflection.Emit
Module Test
Sub Main()
Dim currentDomain As AppDomain = AppDomain.CurrentDomain
InstantiateMyDynamicType(currentDomain) 'Failed!
AddHandler currentDomain.AssemblyResolve, AddressOf MyResolveEventHandler
InstantiateMyDynamicType(currentDomain) 'OK!
End Sub
Sub InstantiateMyDynamicType(domain As AppDomain)
Try
' You must supply a valid fully qualified assembly name here.
domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyDynamicType")
Catch e As Exception
Console.WriteLine(e.Message)
End Try
End Sub
Function MyResolveEventHandler(sender As Object, args As ResolveEventArgs) As System.Reflection.Assembly
Return DefineDynamicAssembly(DirectCast(sender, AppDomain))
End Function 'MyResolveEventHandler
Function DefineDynamicAssembly(domain As AppDomain) As System.Reflection.Assembly
' Build a dynamic assembly using Reflection Emit API.
Dim assemblyName As New AssemblyName()
assemblyName.Name = "MyDynamicAssembly"
Dim assemblyBuilder As AssemblyBuilder = domain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run)
Dim moduleBuilder As ModuleBuilder = assemblyBuilder.DefineDynamicModule("MyDynamicModule")
Dim typeBuilder As TypeBuilder = moduleBuilder.DefineType("MyDynamicType", TypeAttributes.Public)
Dim constructorBuilder As ConstructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, Nothing)
Dim ilGenerator As ILGenerator = constructorBuilder.GetILGenerator()
ilGenerator.EmitWriteLine("MyDynamicType instantiated!")
ilGenerator.Emit(OpCodes.Ret)
typeBuilder.CreateType()
Return assemblyBuilder
End Function 'DefineDynamicAssembly
End Module 'Test
Keterangan
Permintaan izin yang ditentukan untuk requiredPermissions
, , dan refusedPermissions
hanya digunakan jika evidence
juga disediakan, atau jika rakitan dinamis disimpan dan dimuat ulang optionalPermissions
ke dalam memori.
Catatan
Selama pengembangan kode yang memancarkan rakitan dinamis, disarankan agar Anda menyertakan SecurityPermissionFlag.SkipVerification dalam refusedPermissions
. Termasuk SkipVerification dalam refusedPermissions
parameter memastikan bahwa MSIL diverifikasi. Batasan teknik ini adalah juga menyebabkan SecurityException dilemparkan ketika digunakan dengan kode yang menuntut kepercayaan penuh.
Hanya penelepon yang sepenuhnya tepercaya yang dapat menyediakannya evidence
saat menentukan dinamis Assembly. Runtime akan memetakan Evidence melalui kebijakan keamanan untuk menentukan izin yang diberikan. Penelepon tepercaya sebagian harus menyediakan null evidence
. Jika evidence
adalah null
, runtime menyalin set izin, yaitu, set pemberian dan tolak saat ini, dari pemanggil Assembly hingga dinamis Assembly yang ditentukan dan menandai kebijakan sebagai diselesaikan.
Jika dinamis Assembly disimpan ke disk, beban berikutnya akan mendapatkan hibah berdasarkan kebijakan yang terkait dengan lokasi tempat Assembly penyimpanan.
Metode ini hanya boleh digunakan untuk menentukan rakitan dinamis di domain aplikasi saat ini. Untuk informasi selengkapnya, lihat Load(AssemblyName) metode kelebihan beban.
Berlaku untuk
DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String, Boolean, IEnumerable<CustomAttributeBuilder>)
Menentukan rakitan dinamis menggunakan opsi nama, mode akses, direktori penyimpanan, dan sinkronisasi yang ditentukan.
public:
System::Reflection::Emit::AssemblyBuilder ^ DefineDynamicAssembly(System::Reflection::AssemblyName ^ name, System::Reflection::Emit::AssemblyBuilderAccess access, System::String ^ dir, bool isSynchronized, System::Collections::Generic::IEnumerable<System::Reflection::Emit::CustomAttributeBuilder ^> ^ assemblyAttributes);
public System.Reflection.Emit.AssemblyBuilder DefineDynamicAssembly (System.Reflection.AssemblyName name, System.Reflection.Emit.AssemblyBuilderAccess access, string dir, bool isSynchronized, System.Collections.Generic.IEnumerable<System.Reflection.Emit.CustomAttributeBuilder> assemblyAttributes);
member this.DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * string * bool * seq<System.Reflection.Emit.CustomAttributeBuilder> -> System.Reflection.Emit.AssemblyBuilder
Public Function DefineDynamicAssembly (name As AssemblyName, access As AssemblyBuilderAccess, dir As String, isSynchronized As Boolean, assemblyAttributes As IEnumerable(Of CustomAttributeBuilder)) As AssemblyBuilder
Parameter
- name
- AssemblyName
Identitas unik rakitan dinamis.
- access
- AssemblyBuilderAccess
Mode di mana rakitan dinamis akan diakses.
- dir
- String
Nama direktori tempat rakitan dinamis akan disimpan. Jika dir
adalah null
, direktori saat ini digunakan.
- isSynchronized
- Boolean
true
untuk menyinkronkan pembuatan modul, jenis, dan anggota di perakitan dinamis; jika tidak, false
.
- assemblyAttributes
- IEnumerable<CustomAttributeBuilder>
Daftar atribut yang dapat dijumlahkan untuk diterapkan ke assembly, atau null
jika tidak ada atribut.
Mengembalikan
Rakitan dinamis dengan nama dan fitur yang ditentukan.
Pengecualian
name
adalah null
.
Properti Name
dari name
adalah null
.
-atau-
Name
Properti dimulai name
dengan spasi kosong, atau berisi garis miring ke depan atau mundur.
Operasi ini dicoba pada domain aplikasi yang dibongkar.
Keterangan
Gunakan metode ini kelebihan beban untuk menentukan atribut yang tidak berfungsi dengan benar kecuali jika diterapkan saat rakitan dinamis dibuat. Misalnya, atribut keamanan seperti SecurityTransparentAttribute dan SecurityCriticalAttribute tidak berfungsi dengan benar jika ditambahkan setelah rakitan dinamis dibuat.
Jika isSynchronized
adalah true
, metode berikut dari yang dihasilkan AssemblyBuilder akan disinkronkan: DefineDynamicModule, , DefineResource, AddResourceFileGetDynamicModule, SetEntryPoint, dan Save. Jika dua metode ini dipanggil pada utas yang berbeda, satu akan memblokir sampai yang lain selesai.
Lihat juga
Berlaku untuk
DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, PermissionSet, PermissionSet, PermissionSet)
Perhatian
Assembly level declarative security is obsolete and is no longer enforced by the CLR by default. See http://go.microsoft.com/fwlink/?LinkID=155570 for more information.
Menentukan rakitan dinamis menggunakan nama, mode akses, dan permintaan izin yang ditentukan.
public:
virtual System::Reflection::Emit::AssemblyBuilder ^ DefineDynamicAssembly(System::Reflection::AssemblyName ^ name, System::Reflection::Emit::AssemblyBuilderAccess access, System::Security::PermissionSet ^ requiredPermissions, System::Security::PermissionSet ^ optionalPermissions, System::Security::PermissionSet ^ refusedPermissions);
public System.Reflection.Emit.AssemblyBuilder DefineDynamicAssembly (System.Reflection.AssemblyName name, System.Reflection.Emit.AssemblyBuilderAccess access, System.Security.PermissionSet requiredPermissions, System.Security.PermissionSet optionalPermissions, System.Security.PermissionSet refusedPermissions);
[System.Obsolete("Assembly level declarative security is obsolete and is no longer enforced by the CLR by default. See http://go.microsoft.com/fwlink/?LinkID=155570 for more information.")]
public System.Reflection.Emit.AssemblyBuilder DefineDynamicAssembly (System.Reflection.AssemblyName name, System.Reflection.Emit.AssemblyBuilderAccess access, System.Security.PermissionSet requiredPermissions, System.Security.PermissionSet optionalPermissions, System.Security.PermissionSet refusedPermissions);
abstract member DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * System.Security.PermissionSet * System.Security.PermissionSet * System.Security.PermissionSet -> System.Reflection.Emit.AssemblyBuilder
override this.DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * System.Security.PermissionSet * System.Security.PermissionSet * System.Security.PermissionSet -> System.Reflection.Emit.AssemblyBuilder
[<System.Obsolete("Assembly level declarative security is obsolete and is no longer enforced by the CLR by default. See http://go.microsoft.com/fwlink/?LinkID=155570 for more information.")>]
abstract member DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * System.Security.PermissionSet * System.Security.PermissionSet * System.Security.PermissionSet -> System.Reflection.Emit.AssemblyBuilder
override this.DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * System.Security.PermissionSet * System.Security.PermissionSet * System.Security.PermissionSet -> System.Reflection.Emit.AssemblyBuilder
Public Function DefineDynamicAssembly (name As AssemblyName, access As AssemblyBuilderAccess, requiredPermissions As PermissionSet, optionalPermissions As PermissionSet, refusedPermissions As PermissionSet) As AssemblyBuilder
Parameter
- name
- AssemblyName
Identitas unik dari perakitan dinamis.
- access
- AssemblyBuilderAccess
Mode di mana perakitan dinamis akan diakses.
- requiredPermissions
- PermissionSet
Permintaan izin yang diperlukan.
- optionalPermissions
- PermissionSet
Permintaan izin opsional.
- refusedPermissions
- PermissionSet
Permintaan izin yang ditolak.
Mengembalikan
Rakitan dinamis dengan nama dan fitur yang ditentukan.
Penerapan
- Atribut
Pengecualian
name
adalah null
.
Properti Name
dari name
adalah null
.
-atau-
Properti Name
name
dimulai dengan spasi kosong, atau berisi garis miring ke depan atau mundur.
Operasi ini dicoba pada domain aplikasi yang tidak dimuat.
Contoh
Sampel berikut menunjukkan DefineDynamicAssembly metode dan AssemblyResolve peristiwa.
Agar contoh kode ini berjalan, Anda harus memberikan nama rakitan yang sepenuhnya memenuhi syarat. Untuk informasi tentang cara mendapatkan nama rakitan yang sepenuhnya memenuhi syarat, lihat Nama Rakitan.
using namespace System;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
ref class Test
{
public:
static void InstantiateMyDynamicType( AppDomain^ domain )
{
try
{
// You must supply a valid fully qualified assembly name here.
domain->CreateInstance( "Assembly text name, Version, Culture, PublicKeyToken", "MyDynamicType" );
}
catch ( Exception^ e )
{
Console::WriteLine( e->Message );
}
}
static Assembly^ MyResolveEventHandler( Object^ sender, ResolveEventArgs^ args )
{
return DefineDynamicAssembly( dynamic_cast<AppDomain^>(sender) );
}
static Assembly^ DefineDynamicAssembly( AppDomain^ domain )
{
// Build a dynamic assembly using Reflection Emit API.
AssemblyName^ assemblyName = gcnew AssemblyName;
assemblyName->Name = "MyDynamicAssembly";
AssemblyBuilder^ assemblyBuilder = domain->DefineDynamicAssembly( assemblyName, AssemblyBuilderAccess::Run );
ModuleBuilder^ moduleBuilder = assemblyBuilder->DefineDynamicModule( "MyDynamicModule" );
TypeBuilder^ typeBuilder = moduleBuilder->DefineType( "MyDynamicType", TypeAttributes::Public );
ConstructorBuilder^ constructorBuilder = typeBuilder->DefineConstructor( MethodAttributes::Public, CallingConventions::Standard, nullptr );
ILGenerator^ ilGenerator = constructorBuilder->GetILGenerator();
ilGenerator->EmitWriteLine( "MyDynamicType instantiated!" );
ilGenerator->Emit( OpCodes::Ret );
typeBuilder->CreateType();
return assemblyBuilder;
}
};
int main()
{
AppDomain^ currentDomain = AppDomain::CurrentDomain;
Test::InstantiateMyDynamicType( currentDomain ); // Failed!
currentDomain->AssemblyResolve += gcnew ResolveEventHandler( Test::MyResolveEventHandler );
Test::InstantiateMyDynamicType( currentDomain ); // OK!
}
using System;
using System.Reflection;
using System.Reflection.Emit;
class DefinedDynamicAssemblySnippet {
public static void Main() {
AppDomain currentDomain = AppDomain.CurrentDomain;
InstantiateMyDynamicType(currentDomain); // Failed!
currentDomain.AssemblyResolve += new ResolveEventHandler(MyResolveEventHandler);
InstantiateMyDynamicType(currentDomain); // OK!
}
static void InstantiateMyDynamicType(AppDomain domain) {
try {
// You must supply a valid fully qualified assembly name here.
domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyDynamicType");
} catch (Exception e) {
Console.WriteLine(e.Message);
}
}
static Assembly MyResolveEventHandler(object sender, ResolveEventArgs args) {
return DefineDynamicAssembly((AppDomain) sender);
}
static Assembly DefineDynamicAssembly(AppDomain domain) {
// Build a dynamic assembly using Reflection Emit API.
AssemblyName assemblyName = new AssemblyName();
assemblyName.Name = "MyDynamicAssembly";
AssemblyBuilder assemblyBuilder = domain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("MyDynamicModule");
TypeBuilder typeBuilder = moduleBuilder.DefineType("MyDynamicType", TypeAttributes.Public);
ConstructorBuilder constructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, null);
ILGenerator ilGenerator = constructorBuilder.GetILGenerator();
ilGenerator.EmitWriteLine("MyDynamicType instantiated!");
ilGenerator.Emit(OpCodes.Ret);
typeBuilder.CreateType();
return assemblyBuilder;
}
}
open System
open System.Reflection
open System.Reflection.Emit
let instantiateMyDynamicType (domain: AppDomain) =
try
// You must supply a valid fully qualified assembly name here.
domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyDynamicType")
|> ignore
with e ->
printfn $"{e.Message}"
let defineDynamicAssembly (domain: AppDomain) =
// Build a dynamic assembly using Reflection Emit API.
let assemblyName = AssemblyName()
assemblyName.Name <- "MyDynamicAssembly"
let assemblyBuilder = domain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run)
let moduleBuilder = assemblyBuilder.DefineDynamicModule "MyDynamicModule"
let typeBuilder = moduleBuilder.DefineType("MyDynamicType", TypeAttributes.Public)
let constructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, null)
let ilGenerator = constructorBuilder.GetILGenerator()
ilGenerator.EmitWriteLine "MyDynamicType instantiated!"
ilGenerator.Emit OpCodes.Ret
typeBuilder.CreateType() |> ignore
assemblyBuilder
let myResolveEventHandler (sender: obj) _ =
defineDynamicAssembly (sender :?> AppDomain)
:> Assembly
let currentDomain = AppDomain.CurrentDomain
instantiateMyDynamicType currentDomain // Failed!
currentDomain.add_AssemblyResolve(ResolveEventHandler myResolveEventHandler)
instantiateMyDynamicType currentDomain // OK!
Imports System.Reflection
Imports System.Reflection.Emit
Module Test
Sub Main()
Dim currentDomain As AppDomain = AppDomain.CurrentDomain
InstantiateMyDynamicType(currentDomain) 'Failed!
AddHandler currentDomain.AssemblyResolve, AddressOf MyResolveEventHandler
InstantiateMyDynamicType(currentDomain) 'OK!
End Sub
Sub InstantiateMyDynamicType(domain As AppDomain)
Try
' You must supply a valid fully qualified assembly name here.
domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyDynamicType")
Catch e As Exception
Console.WriteLine(e.Message)
End Try
End Sub
Function MyResolveEventHandler(sender As Object, args As ResolveEventArgs) As System.Reflection.Assembly
Return DefineDynamicAssembly(DirectCast(sender, AppDomain))
End Function 'MyResolveEventHandler
Function DefineDynamicAssembly(domain As AppDomain) As System.Reflection.Assembly
' Build a dynamic assembly using Reflection Emit API.
Dim assemblyName As New AssemblyName()
assemblyName.Name = "MyDynamicAssembly"
Dim assemblyBuilder As AssemblyBuilder = domain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run)
Dim moduleBuilder As ModuleBuilder = assemblyBuilder.DefineDynamicModule("MyDynamicModule")
Dim typeBuilder As TypeBuilder = moduleBuilder.DefineType("MyDynamicType", TypeAttributes.Public)
Dim constructorBuilder As ConstructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, Nothing)
Dim ilGenerator As ILGenerator = constructorBuilder.GetILGenerator()
ilGenerator.EmitWriteLine("MyDynamicType instantiated!")
ilGenerator.Emit(OpCodes.Ret)
typeBuilder.CreateType()
Return assemblyBuilder
End Function 'DefineDynamicAssembly
End Module 'Test
Keterangan
Permintaan izin yang ditentukan untuk requiredPermissions
, optionalPermissions
, dan refusedPermissions
tidak digunakan kecuali rakitan dinamis telah disimpan dan dimuat ulang ke dalam memori. Untuk menentukan permintaan izin untuk rakitan sementara yang tidak pernah disimpan ke disk, gunakan kelebihan beban DefineDynamicAssembly metode yang menentukan bukti serta izin yang diminta, dan berikan Evidence objek.
Catatan
Selama pengembangan kode yang memancarkan rakitan dinamis, disarankan agar Anda menggunakan kelebihan beban DefineDynamicAssembly metode yang menentukan bukti dan izin, berikan bukti yang Anda inginkan untuk dimiliki perakitan dinamis, dan sertakan SecurityPermissionFlag.SkipVerification dalam refusedPermissions
. Termasuk SkipVerification dalam refusedPermissions
parameter memastikan bahwa MSIL diverifikasi. Keterbatasan teknik ini adalah juga menyebabkan SecurityException dilemparkan ketika digunakan dengan kode yang menuntut kepercayaan penuh.
Metode ini hanya boleh digunakan untuk menentukan rakitan dinamis di domain aplikasi saat ini. Untuk informasi selengkapnya, lihat Load(AssemblyName) metode kelebihan beban .
Berlaku untuk
DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String, Evidence)
Perhatian
Methods which use evidence to sandbox are obsolete and will be removed in a future release of the .NET Framework. Please use an overload of DefineDynamicAssembly which does not take an Evidence parameter. See http://go.microsoft.com/fwlink/?LinkId=155570 for more information.
Menentukan rakitan dinamis menggunakan nama, mode akses, direktori penyimpanan, dan bukti yang ditentukan.
public:
virtual System::Reflection::Emit::AssemblyBuilder ^ DefineDynamicAssembly(System::Reflection::AssemblyName ^ name, System::Reflection::Emit::AssemblyBuilderAccess access, System::String ^ dir, System::Security::Policy::Evidence ^ evidence);
public System.Reflection.Emit.AssemblyBuilder DefineDynamicAssembly (System.Reflection.AssemblyName name, System.Reflection.Emit.AssemblyBuilderAccess access, string dir, System.Security.Policy.Evidence evidence);
[System.Obsolete("Methods which use evidence to sandbox are obsolete and will be removed in a future release of the .NET Framework. Please use an overload of DefineDynamicAssembly which does not take an Evidence parameter. See http://go.microsoft.com/fwlink/?LinkId=155570 for more information.")]
public System.Reflection.Emit.AssemblyBuilder DefineDynamicAssembly (System.Reflection.AssemblyName name, System.Reflection.Emit.AssemblyBuilderAccess access, string dir, System.Security.Policy.Evidence evidence);
abstract member DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * string * System.Security.Policy.Evidence -> System.Reflection.Emit.AssemblyBuilder
override this.DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * string * System.Security.Policy.Evidence -> System.Reflection.Emit.AssemblyBuilder
[<System.Obsolete("Methods which use evidence to sandbox are obsolete and will be removed in a future release of the .NET Framework. Please use an overload of DefineDynamicAssembly which does not take an Evidence parameter. See http://go.microsoft.com/fwlink/?LinkId=155570 for more information.")>]
abstract member DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * string * System.Security.Policy.Evidence -> System.Reflection.Emit.AssemblyBuilder
override this.DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * string * System.Security.Policy.Evidence -> System.Reflection.Emit.AssemblyBuilder
Public Function DefineDynamicAssembly (name As AssemblyName, access As AssemblyBuilderAccess, dir As String, evidence As Evidence) As AssemblyBuilder
Parameter
- name
- AssemblyName
Identitas unik dari perakitan dinamis.
- access
- AssemblyBuilderAccess
Mode di mana perakitan dinamis akan diakses.
- dir
- String
Nama direktori tempat assembly akan disimpan. Jika dir
adalah null
, direktori default ke direktori saat ini.
- evidence
- Evidence
Bukti yang disediakan untuk perakitan dinamis. Bukti digunakan tanpa diubah sebagai set bukti akhir yang digunakan untuk resolusi kebijakan.
Mengembalikan
Rakitan dinamis dengan nama dan fitur yang ditentukan.
Penerapan
- Atribut
Pengecualian
name
adalah null
.
Properti Name
dari name
adalah null
.
-atau-
Properti Name
name
dimulai dengan spasi kosong, atau berisi garis miring ke depan atau mundur.
Operasi ini dicoba pada domain aplikasi yang tidak dimuat.
Contoh
Sampel berikut menunjukkan DefineDynamicAssembly metode dan AssemblyResolve peristiwa.
Agar contoh kode ini berjalan, Anda harus memberikan nama rakitan yang sepenuhnya memenuhi syarat. Untuk informasi tentang cara mendapatkan nama rakitan yang sepenuhnya memenuhi syarat, lihat Nama Rakitan.
using namespace System;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
ref class Test
{
public:
static void InstantiateMyDynamicType( AppDomain^ domain )
{
try
{
// You must supply a valid fully qualified assembly name here.
domain->CreateInstance( "Assembly text name, Version, Culture, PublicKeyToken", "MyDynamicType" );
}
catch ( Exception^ e )
{
Console::WriteLine( e->Message );
}
}
static Assembly^ MyResolveEventHandler( Object^ sender, ResolveEventArgs^ args )
{
return DefineDynamicAssembly( dynamic_cast<AppDomain^>(sender) );
}
static Assembly^ DefineDynamicAssembly( AppDomain^ domain )
{
// Build a dynamic assembly using Reflection Emit API.
AssemblyName^ assemblyName = gcnew AssemblyName;
assemblyName->Name = "MyDynamicAssembly";
AssemblyBuilder^ assemblyBuilder = domain->DefineDynamicAssembly( assemblyName, AssemblyBuilderAccess::Run );
ModuleBuilder^ moduleBuilder = assemblyBuilder->DefineDynamicModule( "MyDynamicModule" );
TypeBuilder^ typeBuilder = moduleBuilder->DefineType( "MyDynamicType", TypeAttributes::Public );
ConstructorBuilder^ constructorBuilder = typeBuilder->DefineConstructor( MethodAttributes::Public, CallingConventions::Standard, nullptr );
ILGenerator^ ilGenerator = constructorBuilder->GetILGenerator();
ilGenerator->EmitWriteLine( "MyDynamicType instantiated!" );
ilGenerator->Emit( OpCodes::Ret );
typeBuilder->CreateType();
return assemblyBuilder;
}
};
int main()
{
AppDomain^ currentDomain = AppDomain::CurrentDomain;
Test::InstantiateMyDynamicType( currentDomain ); // Failed!
currentDomain->AssemblyResolve += gcnew ResolveEventHandler( Test::MyResolveEventHandler );
Test::InstantiateMyDynamicType( currentDomain ); // OK!
}
using System;
using System.Reflection;
using System.Reflection.Emit;
class DefinedDynamicAssemblySnippet {
public static void Main() {
AppDomain currentDomain = AppDomain.CurrentDomain;
InstantiateMyDynamicType(currentDomain); // Failed!
currentDomain.AssemblyResolve += new ResolveEventHandler(MyResolveEventHandler);
InstantiateMyDynamicType(currentDomain); // OK!
}
static void InstantiateMyDynamicType(AppDomain domain) {
try {
// You must supply a valid fully qualified assembly name here.
domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyDynamicType");
} catch (Exception e) {
Console.WriteLine(e.Message);
}
}
static Assembly MyResolveEventHandler(object sender, ResolveEventArgs args) {
return DefineDynamicAssembly((AppDomain) sender);
}
static Assembly DefineDynamicAssembly(AppDomain domain) {
// Build a dynamic assembly using Reflection Emit API.
AssemblyName assemblyName = new AssemblyName();
assemblyName.Name = "MyDynamicAssembly";
AssemblyBuilder assemblyBuilder = domain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("MyDynamicModule");
TypeBuilder typeBuilder = moduleBuilder.DefineType("MyDynamicType", TypeAttributes.Public);
ConstructorBuilder constructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, null);
ILGenerator ilGenerator = constructorBuilder.GetILGenerator();
ilGenerator.EmitWriteLine("MyDynamicType instantiated!");
ilGenerator.Emit(OpCodes.Ret);
typeBuilder.CreateType();
return assemblyBuilder;
}
}
open System
open System.Reflection
open System.Reflection.Emit
let instantiateMyDynamicType (domain: AppDomain) =
try
// You must supply a valid fully qualified assembly name here.
domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyDynamicType")
|> ignore
with e ->
printfn $"{e.Message}"
let defineDynamicAssembly (domain: AppDomain) =
// Build a dynamic assembly using Reflection Emit API.
let assemblyName = AssemblyName()
assemblyName.Name <- "MyDynamicAssembly"
let assemblyBuilder = domain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run)
let moduleBuilder = assemblyBuilder.DefineDynamicModule "MyDynamicModule"
let typeBuilder = moduleBuilder.DefineType("MyDynamicType", TypeAttributes.Public)
let constructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, null)
let ilGenerator = constructorBuilder.GetILGenerator()
ilGenerator.EmitWriteLine "MyDynamicType instantiated!"
ilGenerator.Emit OpCodes.Ret
typeBuilder.CreateType() |> ignore
assemblyBuilder
let myResolveEventHandler (sender: obj) _ =
defineDynamicAssembly (sender :?> AppDomain)
:> Assembly
let currentDomain = AppDomain.CurrentDomain
instantiateMyDynamicType currentDomain // Failed!
currentDomain.add_AssemblyResolve(ResolveEventHandler myResolveEventHandler)
instantiateMyDynamicType currentDomain // OK!
Imports System.Reflection
Imports System.Reflection.Emit
Module Test
Sub Main()
Dim currentDomain As AppDomain = AppDomain.CurrentDomain
InstantiateMyDynamicType(currentDomain) 'Failed!
AddHandler currentDomain.AssemblyResolve, AddressOf MyResolveEventHandler
InstantiateMyDynamicType(currentDomain) 'OK!
End Sub
Sub InstantiateMyDynamicType(domain As AppDomain)
Try
' You must supply a valid fully qualified assembly name here.
domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyDynamicType")
Catch e As Exception
Console.WriteLine(e.Message)
End Try
End Sub
Function MyResolveEventHandler(sender As Object, args As ResolveEventArgs) As System.Reflection.Assembly
Return DefineDynamicAssembly(DirectCast(sender, AppDomain))
End Function 'MyResolveEventHandler
Function DefineDynamicAssembly(domain As AppDomain) As System.Reflection.Assembly
' Build a dynamic assembly using Reflection Emit API.
Dim assemblyName As New AssemblyName()
assemblyName.Name = "MyDynamicAssembly"
Dim assemblyBuilder As AssemblyBuilder = domain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run)
Dim moduleBuilder As ModuleBuilder = assemblyBuilder.DefineDynamicModule("MyDynamicModule")
Dim typeBuilder As TypeBuilder = moduleBuilder.DefineType("MyDynamicType", TypeAttributes.Public)
Dim constructorBuilder As ConstructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, Nothing)
Dim ilGenerator As ILGenerator = constructorBuilder.GetILGenerator()
ilGenerator.EmitWriteLine("MyDynamicType instantiated!")
ilGenerator.Emit(OpCodes.Ret)
typeBuilder.CreateType()
Return assemblyBuilder
End Function 'DefineDynamicAssembly
End Module 'Test
Keterangan
Hanya penelepon yang sepenuhnya tepercaya yang dapat menyediakannya evidence
saat menentukan dinamika Assembly. Runtime akan memetakan Evidence melalui kebijakan keamanan untuk menentukan izin yang diberikan. Penelepon yang dipercaya sebagian harus menyediakan null evidence
. Jika evidence
adalah null
, runtime menyalin set izin, yaitu, set pemberian dan tolak saat ini, dari pemanggil Assembly hingga dinamis Assembly yang didefinisikan dan menandai kebijakan sebagai diselesaikan.
Jika dinamis Assembly disimpan ke disk, beban berikutnya akan mendapatkan pemberian berdasarkan kebijakan yang terkait dengan lokasi tempat Assembly penyimpanan.
Metode ini hanya boleh digunakan untuk menentukan rakitan dinamis di domain aplikasi saat ini. Untuk informasi selengkapnya, lihat Load(AssemblyName) metode kelebihan beban.
Catatan
Selama pengembangan kode yang memancarkan rakitan dinamis, disarankan agar Anda menggunakan kelebihan beban DefineDynamicAssembly metode yang menentukan bukti dan izin, berikan bukti yang Anda inginkan untuk dimiliki perakitan dinamis, dan sertakan SecurityPermissionFlag.SkipVerification dalam refusedPermissions
. Termasuk SkipVerification dalam refusedPermissions
parameter memastikan bahwa MSIL diverifikasi. Keterbatasan teknik ini adalah juga menyebabkan SecurityException dilemparkan ketika digunakan dengan kode yang menuntut kepercayaan penuh.
Berlaku untuk
DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, IEnumerable<CustomAttributeBuilder>, SecurityContextSource)
Mendefinisikan rakitan dinamis dengan nama, mode akses, dan atribut kustom yang ditentukan, dan menggunakan sumber yang ditentukan untuk konteks keamanannya.
public:
System::Reflection::Emit::AssemblyBuilder ^ DefineDynamicAssembly(System::Reflection::AssemblyName ^ name, System::Reflection::Emit::AssemblyBuilderAccess access, System::Collections::Generic::IEnumerable<System::Reflection::Emit::CustomAttributeBuilder ^> ^ assemblyAttributes, System::Security::SecurityContextSource securityContextSource);
public System.Reflection.Emit.AssemblyBuilder DefineDynamicAssembly (System.Reflection.AssemblyName name, System.Reflection.Emit.AssemblyBuilderAccess access, System.Collections.Generic.IEnumerable<System.Reflection.Emit.CustomAttributeBuilder> assemblyAttributes, System.Security.SecurityContextSource securityContextSource);
member this.DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * seq<System.Reflection.Emit.CustomAttributeBuilder> * System.Security.SecurityContextSource -> System.Reflection.Emit.AssemblyBuilder
Public Function DefineDynamicAssembly (name As AssemblyName, access As AssemblyBuilderAccess, assemblyAttributes As IEnumerable(Of CustomAttributeBuilder), securityContextSource As SecurityContextSource) As AssemblyBuilder
Parameter
- name
- AssemblyName
Identitas unik dari perakitan dinamis.
- access
- AssemblyBuilderAccess
Mode akses untuk rakitan dinamis.
- assemblyAttributes
- IEnumerable<CustomAttributeBuilder>
Daftar atribut yang dapat dijumlahkan untuk diterapkan ke perakitan, atau null
jika tidak ada atribut.
- securityContextSource
- SecurityContextSource
Sumber konteks keamanan.
Mengembalikan
Rakitan dinamis dengan nama dan fitur yang ditentukan.
Pengecualian
name
adalah null
.
Properti Name
dari name
adalah null
.
-atau-
Properti Name
name
dimulai dengan spasi kosong, atau berisi garis miring ke depan atau mundur.
Operasi ini dicoba pada domain aplikasi yang tidak dimuat.
Nilai securityContextSource
bukan salah satu nilai enumerasi.
Keterangan
Gunakan metode ini kelebihan beban untuk menentukan atribut yang tidak berfungsi dengan benar kecuali jika diterapkan saat rakitan dinamis dibuat. Misalnya, atribut keamanan seperti SecurityTransparentAttribute dan SecurityCriticalAttribute tidak berfungsi dengan benar jika ditambahkan setelah rakitan dinamis dibuat.
Metode ini harus digunakan hanya untuk menentukan rakitan dinamis di domain aplikasi saat ini. Untuk informasi selengkapnya tentang pembatasan ini, lihat Load(AssemblyName) metode kelebihan beban.
Berlaku untuk
DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, String)
Menentukan rakitan dinamis menggunakan nama, mode akses, dan direktori penyimpanan yang ditentukan.
public:
virtual System::Reflection::Emit::AssemblyBuilder ^ DefineDynamicAssembly(System::Reflection::AssemblyName ^ name, System::Reflection::Emit::AssemblyBuilderAccess access, System::String ^ dir);
public System.Reflection.Emit.AssemblyBuilder DefineDynamicAssembly (System.Reflection.AssemblyName name, System.Reflection.Emit.AssemblyBuilderAccess access, string dir);
abstract member DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * string -> System.Reflection.Emit.AssemblyBuilder
override this.DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * string -> System.Reflection.Emit.AssemblyBuilder
Public Function DefineDynamicAssembly (name As AssemblyName, access As AssemblyBuilderAccess, dir As String) As AssemblyBuilder
Parameter
- name
- AssemblyName
Identitas unik dari perakitan dinamis.
- access
- AssemblyBuilderAccess
Mode di mana perakitan dinamis akan diakses.
- dir
- String
Nama direktori tempat assembly akan disimpan. Jika dir
adalah null
, direktori default ke direktori saat ini.
Mengembalikan
Rakitan dinamis dengan nama dan fitur yang ditentukan.
Penerapan
Pengecualian
name
adalah null
.
Properti Name
dari name
adalah null
.
-atau-
Properti Name
name
dimulai dengan spasi kosong, atau berisi garis miring ke depan atau mundur.
Operasi ini dicoba pada domain aplikasi yang tidak dimuat.
Contoh
Sampel berikut menunjukkan DefineDynamicAssembly metode dan AssemblyResolve peristiwa.
Agar contoh kode ini berjalan, Anda harus memberikan nama rakitan yang sepenuhnya memenuhi syarat. Untuk informasi tentang cara mendapatkan nama rakitan yang sepenuhnya memenuhi syarat, lihat Nama Rakitan.
using namespace System;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
ref class Test
{
public:
static void InstantiateMyDynamicType( AppDomain^ domain )
{
try
{
// You must supply a valid fully qualified assembly name here.
domain->CreateInstance( "Assembly text name, Version, Culture, PublicKeyToken", "MyDynamicType" );
}
catch ( Exception^ e )
{
Console::WriteLine( e->Message );
}
}
static Assembly^ MyResolveEventHandler( Object^ sender, ResolveEventArgs^ args )
{
return DefineDynamicAssembly( dynamic_cast<AppDomain^>(sender) );
}
static Assembly^ DefineDynamicAssembly( AppDomain^ domain )
{
// Build a dynamic assembly using Reflection Emit API.
AssemblyName^ assemblyName = gcnew AssemblyName;
assemblyName->Name = "MyDynamicAssembly";
AssemblyBuilder^ assemblyBuilder = domain->DefineDynamicAssembly( assemblyName, AssemblyBuilderAccess::Run );
ModuleBuilder^ moduleBuilder = assemblyBuilder->DefineDynamicModule( "MyDynamicModule" );
TypeBuilder^ typeBuilder = moduleBuilder->DefineType( "MyDynamicType", TypeAttributes::Public );
ConstructorBuilder^ constructorBuilder = typeBuilder->DefineConstructor( MethodAttributes::Public, CallingConventions::Standard, nullptr );
ILGenerator^ ilGenerator = constructorBuilder->GetILGenerator();
ilGenerator->EmitWriteLine( "MyDynamicType instantiated!" );
ilGenerator->Emit( OpCodes::Ret );
typeBuilder->CreateType();
return assemblyBuilder;
}
};
int main()
{
AppDomain^ currentDomain = AppDomain::CurrentDomain;
Test::InstantiateMyDynamicType( currentDomain ); // Failed!
currentDomain->AssemblyResolve += gcnew ResolveEventHandler( Test::MyResolveEventHandler );
Test::InstantiateMyDynamicType( currentDomain ); // OK!
}
using System;
using System.Reflection;
using System.Reflection.Emit;
class DefinedDynamicAssemblySnippet {
public static void Main() {
AppDomain currentDomain = AppDomain.CurrentDomain;
InstantiateMyDynamicType(currentDomain); // Failed!
currentDomain.AssemblyResolve += new ResolveEventHandler(MyResolveEventHandler);
InstantiateMyDynamicType(currentDomain); // OK!
}
static void InstantiateMyDynamicType(AppDomain domain) {
try {
// You must supply a valid fully qualified assembly name here.
domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyDynamicType");
} catch (Exception e) {
Console.WriteLine(e.Message);
}
}
static Assembly MyResolveEventHandler(object sender, ResolveEventArgs args) {
return DefineDynamicAssembly((AppDomain) sender);
}
static Assembly DefineDynamicAssembly(AppDomain domain) {
// Build a dynamic assembly using Reflection Emit API.
AssemblyName assemblyName = new AssemblyName();
assemblyName.Name = "MyDynamicAssembly";
AssemblyBuilder assemblyBuilder = domain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("MyDynamicModule");
TypeBuilder typeBuilder = moduleBuilder.DefineType("MyDynamicType", TypeAttributes.Public);
ConstructorBuilder constructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, null);
ILGenerator ilGenerator = constructorBuilder.GetILGenerator();
ilGenerator.EmitWriteLine("MyDynamicType instantiated!");
ilGenerator.Emit(OpCodes.Ret);
typeBuilder.CreateType();
return assemblyBuilder;
}
}
open System
open System.Reflection
open System.Reflection.Emit
let instantiateMyDynamicType (domain: AppDomain) =
try
// You must supply a valid fully qualified assembly name here.
domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyDynamicType")
|> ignore
with e ->
printfn $"{e.Message}"
let defineDynamicAssembly (domain: AppDomain) =
// Build a dynamic assembly using Reflection Emit API.
let assemblyName = AssemblyName()
assemblyName.Name <- "MyDynamicAssembly"
let assemblyBuilder = domain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run)
let moduleBuilder = assemblyBuilder.DefineDynamicModule "MyDynamicModule"
let typeBuilder = moduleBuilder.DefineType("MyDynamicType", TypeAttributes.Public)
let constructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, null)
let ilGenerator = constructorBuilder.GetILGenerator()
ilGenerator.EmitWriteLine "MyDynamicType instantiated!"
ilGenerator.Emit OpCodes.Ret
typeBuilder.CreateType() |> ignore
assemblyBuilder
let myResolveEventHandler (sender: obj) _ =
defineDynamicAssembly (sender :?> AppDomain)
:> Assembly
let currentDomain = AppDomain.CurrentDomain
instantiateMyDynamicType currentDomain // Failed!
currentDomain.add_AssemblyResolve(ResolveEventHandler myResolveEventHandler)
instantiateMyDynamicType currentDomain // OK!
Imports System.Reflection
Imports System.Reflection.Emit
Module Test
Sub Main()
Dim currentDomain As AppDomain = AppDomain.CurrentDomain
InstantiateMyDynamicType(currentDomain) 'Failed!
AddHandler currentDomain.AssemblyResolve, AddressOf MyResolveEventHandler
InstantiateMyDynamicType(currentDomain) 'OK!
End Sub
Sub InstantiateMyDynamicType(domain As AppDomain)
Try
' You must supply a valid fully qualified assembly name here.
domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyDynamicType")
Catch e As Exception
Console.WriteLine(e.Message)
End Try
End Sub
Function MyResolveEventHandler(sender As Object, args As ResolveEventArgs) As System.Reflection.Assembly
Return DefineDynamicAssembly(DirectCast(sender, AppDomain))
End Function 'MyResolveEventHandler
Function DefineDynamicAssembly(domain As AppDomain) As System.Reflection.Assembly
' Build a dynamic assembly using Reflection Emit API.
Dim assemblyName As New AssemblyName()
assemblyName.Name = "MyDynamicAssembly"
Dim assemblyBuilder As AssemblyBuilder = domain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run)
Dim moduleBuilder As ModuleBuilder = assemblyBuilder.DefineDynamicModule("MyDynamicModule")
Dim typeBuilder As TypeBuilder = moduleBuilder.DefineType("MyDynamicType", TypeAttributes.Public)
Dim constructorBuilder As ConstructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, Nothing)
Dim ilGenerator As ILGenerator = constructorBuilder.GetILGenerator()
ilGenerator.EmitWriteLine("MyDynamicType instantiated!")
ilGenerator.Emit(OpCodes.Ret)
typeBuilder.CreateType()
Return assemblyBuilder
End Function 'DefineDynamicAssembly
End Module 'Test
Keterangan
Metode ini hanya boleh digunakan untuk menentukan rakitan dinamis di domain aplikasi saat ini. Untuk informasi selengkapnya, lihat Load(AssemblyName) metode kelebihan beban.
Catatan
Selama pengembangan kode yang memancarkan rakitan dinamis, disarankan agar Anda menggunakan kelebihan beban DefineDynamicAssembly metode yang menentukan bukti dan izin, berikan bukti yang Anda inginkan untuk dimiliki perakitan dinamis, dan sertakan SecurityPermissionFlag.SkipVerification dalam refusedPermissions
. Termasuk SkipVerification dalam refusedPermissions
parameter memastikan bahwa MSIL diverifikasi. Keterbatasan teknik ini adalah juga menyebabkan SecurityException dilemparkan ketika digunakan dengan kode yang menuntut kepercayaan penuh.
Berlaku untuk
DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, Evidence)
Perhatian
Assembly level declarative security is obsolete and is no longer enforced by the CLR by default. See http://go.microsoft.com/fwlink/?LinkID=155570 for more information.
Mendefinisikan rakitan dinamis menggunakan nama, mode akses, dan bukti yang ditentukan.
public:
virtual System::Reflection::Emit::AssemblyBuilder ^ DefineDynamicAssembly(System::Reflection::AssemblyName ^ name, System::Reflection::Emit::AssemblyBuilderAccess access, System::Security::Policy::Evidence ^ evidence);
public System.Reflection.Emit.AssemblyBuilder DefineDynamicAssembly (System.Reflection.AssemblyName name, System.Reflection.Emit.AssemblyBuilderAccess access, System.Security.Policy.Evidence evidence);
[System.Obsolete("Assembly level declarative security is obsolete and is no longer enforced by the CLR by default. See http://go.microsoft.com/fwlink/?LinkID=155570 for more information.")]
public System.Reflection.Emit.AssemblyBuilder DefineDynamicAssembly (System.Reflection.AssemblyName name, System.Reflection.Emit.AssemblyBuilderAccess access, System.Security.Policy.Evidence evidence);
abstract member DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * System.Security.Policy.Evidence -> System.Reflection.Emit.AssemblyBuilder
override this.DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * System.Security.Policy.Evidence -> System.Reflection.Emit.AssemblyBuilder
[<System.Obsolete("Assembly level declarative security is obsolete and is no longer enforced by the CLR by default. See http://go.microsoft.com/fwlink/?LinkID=155570 for more information.")>]
abstract member DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * System.Security.Policy.Evidence -> System.Reflection.Emit.AssemblyBuilder
override this.DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * System.Security.Policy.Evidence -> System.Reflection.Emit.AssemblyBuilder
Public Function DefineDynamicAssembly (name As AssemblyName, access As AssemblyBuilderAccess, evidence As Evidence) As AssemblyBuilder
Parameter
- name
- AssemblyName
Identitas unik dari perakitan dinamis.
- access
- AssemblyBuilderAccess
Mode di mana perakitan dinamis akan diakses.
- evidence
- Evidence
Bukti yang disediakan untuk perakitan dinamis. Bukti digunakan tanpa diubah sebagai set bukti akhir yang digunakan untuk resolusi kebijakan.
Mengembalikan
Rakitan dinamis dengan nama dan fitur yang ditentukan.
Penerapan
- Atribut
Pengecualian
name
adalah null
.
Properti Name
dari name
adalah null
.
-atau-
Properti Name
name
dimulai dengan spasi kosong, atau berisi garis miring ke depan atau mundur.
Operasi ini dicoba pada domain aplikasi yang tidak dimuat.
Contoh
Sampel berikut menunjukkan DefineDynamicAssembly metode dan AssemblyResolve peristiwa.
Pertama, contoh kode mencoba membuat instans MyDynamicType
dengan memanggil CreateInstance metode dengan nama rakitan yang tidak valid, dan menangkap pengecualian yang dihasilkan.
Contoh kode kemudian menambahkan penanganan aktivitas untuk AssemblyResolve peristiwa, dan sekali lagi mencoba membuat instansMyDynamicType
. Selama panggilan ke CreateInstance, AssemblyResolve peristiwa dinaikkan untuk rakitan yang tidak valid. Penanganan aktivitas membuat rakitan dinamis yang berisi jenis bernama MyDynamicType
, memberi jenis konstruktor tanpa parameter, dan mengembalikan rakitan dinamis baru. Panggilan ke CreateInstance kemudian berhasil diselesaikan, dan konstruktor untuk MyDynamicType
menampilkan pesan di konsol.
using namespace System;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
ref class Test
{
public:
static void InstantiateMyDynamicType( AppDomain^ domain )
{
try
{
// You must supply a valid fully qualified assembly name here.
domain->CreateInstance( "Assembly text name, Version, Culture, PublicKeyToken", "MyDynamicType" );
}
catch ( Exception^ e )
{
Console::WriteLine( e->Message );
}
}
static Assembly^ MyResolveEventHandler( Object^ sender, ResolveEventArgs^ args )
{
return DefineDynamicAssembly( dynamic_cast<AppDomain^>(sender) );
}
static Assembly^ DefineDynamicAssembly( AppDomain^ domain )
{
// Build a dynamic assembly using Reflection Emit API.
AssemblyName^ assemblyName = gcnew AssemblyName;
assemblyName->Name = "MyDynamicAssembly";
AssemblyBuilder^ assemblyBuilder = domain->DefineDynamicAssembly( assemblyName, AssemblyBuilderAccess::Run );
ModuleBuilder^ moduleBuilder = assemblyBuilder->DefineDynamicModule( "MyDynamicModule" );
TypeBuilder^ typeBuilder = moduleBuilder->DefineType( "MyDynamicType", TypeAttributes::Public );
ConstructorBuilder^ constructorBuilder = typeBuilder->DefineConstructor( MethodAttributes::Public, CallingConventions::Standard, nullptr );
ILGenerator^ ilGenerator = constructorBuilder->GetILGenerator();
ilGenerator->EmitWriteLine( "MyDynamicType instantiated!" );
ilGenerator->Emit( OpCodes::Ret );
typeBuilder->CreateType();
return assemblyBuilder;
}
};
int main()
{
AppDomain^ currentDomain = AppDomain::CurrentDomain;
Test::InstantiateMyDynamicType( currentDomain ); // Failed!
currentDomain->AssemblyResolve += gcnew ResolveEventHandler( Test::MyResolveEventHandler );
Test::InstantiateMyDynamicType( currentDomain ); // OK!
}
using System;
using System.Reflection;
using System.Reflection.Emit;
class DefinedDynamicAssemblySnippet {
public static void Main() {
AppDomain currentDomain = AppDomain.CurrentDomain;
InstantiateMyDynamicType(currentDomain); // Failed!
currentDomain.AssemblyResolve += new ResolveEventHandler(MyResolveEventHandler);
InstantiateMyDynamicType(currentDomain); // OK!
}
static void InstantiateMyDynamicType(AppDomain domain) {
try {
// You must supply a valid fully qualified assembly name here.
domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyDynamicType");
} catch (Exception e) {
Console.WriteLine(e.Message);
}
}
static Assembly MyResolveEventHandler(object sender, ResolveEventArgs args) {
return DefineDynamicAssembly((AppDomain) sender);
}
static Assembly DefineDynamicAssembly(AppDomain domain) {
// Build a dynamic assembly using Reflection Emit API.
AssemblyName assemblyName = new AssemblyName();
assemblyName.Name = "MyDynamicAssembly";
AssemblyBuilder assemblyBuilder = domain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("MyDynamicModule");
TypeBuilder typeBuilder = moduleBuilder.DefineType("MyDynamicType", TypeAttributes.Public);
ConstructorBuilder constructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, null);
ILGenerator ilGenerator = constructorBuilder.GetILGenerator();
ilGenerator.EmitWriteLine("MyDynamicType instantiated!");
ilGenerator.Emit(OpCodes.Ret);
typeBuilder.CreateType();
return assemblyBuilder;
}
}
open System
open System.Reflection
open System.Reflection.Emit
let instantiateMyDynamicType (domain: AppDomain) =
try
// You must supply a valid fully qualified assembly name here.
domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyDynamicType")
|> ignore
with e ->
printfn $"{e.Message}"
let defineDynamicAssembly (domain: AppDomain) =
// Build a dynamic assembly using Reflection Emit API.
let assemblyName = AssemblyName()
assemblyName.Name <- "MyDynamicAssembly"
let assemblyBuilder = domain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run)
let moduleBuilder = assemblyBuilder.DefineDynamicModule "MyDynamicModule"
let typeBuilder = moduleBuilder.DefineType("MyDynamicType", TypeAttributes.Public)
let constructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, null)
let ilGenerator = constructorBuilder.GetILGenerator()
ilGenerator.EmitWriteLine "MyDynamicType instantiated!"
ilGenerator.Emit OpCodes.Ret
typeBuilder.CreateType() |> ignore
assemblyBuilder
let myResolveEventHandler (sender: obj) _ =
defineDynamicAssembly (sender :?> AppDomain)
:> Assembly
let currentDomain = AppDomain.CurrentDomain
instantiateMyDynamicType currentDomain // Failed!
currentDomain.add_AssemblyResolve(ResolveEventHandler myResolveEventHandler)
instantiateMyDynamicType currentDomain // OK!
Imports System.Reflection
Imports System.Reflection.Emit
Module Test
Sub Main()
Dim currentDomain As AppDomain = AppDomain.CurrentDomain
InstantiateMyDynamicType(currentDomain) 'Failed!
AddHandler currentDomain.AssemblyResolve, AddressOf MyResolveEventHandler
InstantiateMyDynamicType(currentDomain) 'OK!
End Sub
Sub InstantiateMyDynamicType(domain As AppDomain)
Try
' You must supply a valid fully qualified assembly name here.
domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyDynamicType")
Catch e As Exception
Console.WriteLine(e.Message)
End Try
End Sub
Function MyResolveEventHandler(sender As Object, args As ResolveEventArgs) As System.Reflection.Assembly
Return DefineDynamicAssembly(DirectCast(sender, AppDomain))
End Function 'MyResolveEventHandler
Function DefineDynamicAssembly(domain As AppDomain) As System.Reflection.Assembly
' Build a dynamic assembly using Reflection Emit API.
Dim assemblyName As New AssemblyName()
assemblyName.Name = "MyDynamicAssembly"
Dim assemblyBuilder As AssemblyBuilder = domain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run)
Dim moduleBuilder As ModuleBuilder = assemblyBuilder.DefineDynamicModule("MyDynamicModule")
Dim typeBuilder As TypeBuilder = moduleBuilder.DefineType("MyDynamicType", TypeAttributes.Public)
Dim constructorBuilder As ConstructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, Nothing)
Dim ilGenerator As ILGenerator = constructorBuilder.GetILGenerator()
ilGenerator.EmitWriteLine("MyDynamicType instantiated!")
ilGenerator.Emit(OpCodes.Ret)
typeBuilder.CreateType()
Return assemblyBuilder
End Function 'DefineDynamicAssembly
End Module 'Test
Keterangan
Hanya penelepon yang sepenuhnya tepercaya yang dapat menyediakannya evidence
saat menentukan dinamika Assembly. Runtime akan memetakan Evidence melalui kebijakan keamanan untuk menentukan izin yang diberikan. Penelepon yang dipercaya sebagian harus menyediakan null evidence
. Jika evidence
adalah null
, runtime menyalin set izin, yaitu, set pemberian dan tolak saat ini, dari pemanggil Assembly hingga dinamis Assembly yang didefinisikan dan menandai kebijakan sebagai diselesaikan.
Jika dinamis Assembly disimpan ke disk, beban berikutnya akan mendapatkan pemberian berdasarkan kebijakan yang terkait dengan lokasi tempat Assembly penyimpanan.
Metode ini hanya boleh digunakan untuk menentukan rakitan dinamis di domain aplikasi saat ini. Untuk informasi selengkapnya, lihat Load(AssemblyName) metode kelebihan beban.
Catatan
Selama pengembangan kode yang memancarkan rakitan dinamis, disarankan agar Anda menggunakan kelebihan beban DefineDynamicAssembly metode yang menentukan bukti dan izin, berikan bukti yang Anda inginkan untuk dimiliki perakitan dinamis, dan sertakan SecurityPermissionFlag.SkipVerification dalam refusedPermissions
. Termasuk SkipVerification dalam refusedPermissions
parameter memastikan bahwa MSIL diverifikasi. Keterbatasan teknik ini adalah juga menyebabkan SecurityException dilemparkan ketika digunakan dengan kode yang menuntut kepercayaan penuh.
Berlaku untuk
DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, IEnumerable<CustomAttributeBuilder>)
Menentukan rakitan dinamis dengan nama, mode akses, dan atribut kustom yang ditentukan.
public:
System::Reflection::Emit::AssemblyBuilder ^ DefineDynamicAssembly(System::Reflection::AssemblyName ^ name, System::Reflection::Emit::AssemblyBuilderAccess access, System::Collections::Generic::IEnumerable<System::Reflection::Emit::CustomAttributeBuilder ^> ^ assemblyAttributes);
public System.Reflection.Emit.AssemblyBuilder DefineDynamicAssembly (System.Reflection.AssemblyName name, System.Reflection.Emit.AssemblyBuilderAccess access, System.Collections.Generic.IEnumerable<System.Reflection.Emit.CustomAttributeBuilder> assemblyAttributes);
member this.DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess * seq<System.Reflection.Emit.CustomAttributeBuilder> -> System.Reflection.Emit.AssemblyBuilder
Public Function DefineDynamicAssembly (name As AssemblyName, access As AssemblyBuilderAccess, assemblyAttributes As IEnumerable(Of CustomAttributeBuilder)) As AssemblyBuilder
Parameter
- name
- AssemblyName
Identitas unik dari perakitan dinamis.
- access
- AssemblyBuilderAccess
Mode akses untuk rakitan dinamis.
- assemblyAttributes
- IEnumerable<CustomAttributeBuilder>
Daftar atribut yang dapat dijumlahkan untuk diterapkan ke perakitan, atau null
jika tidak ada atribut.
Mengembalikan
Rakitan dinamis dengan nama dan fitur yang ditentukan.
Pengecualian
name
adalah null
.
Properti Name
dari name
adalah null
.
-atau-
Properti Name
name
dimulai dengan spasi kosong, atau berisi garis miring ke depan atau mundur.
Operasi ini dicoba pada domain aplikasi yang tidak dimuat.
Contoh
Contoh kode berikut menunjukkan cara membuat rakitan dinamis yang memiliki SecurityTransparentAttribute. Atribut harus ditentukan sebagai elemen dari array CustomAttributeBuilder objek.
Langkah pertama dalam membuat CustomAttributeBuilder adalah mendapatkan konstruktor untuk atribut . Konstruktor tidak memiliki parameter, sehingga GetConstructor metode dipanggil dengan array Type objek kosong untuk mewakili jenis parameter. Langkah kedua adalah meneruskan objek yang ConstructorInfo dihasilkan ke konstruktor untuk CustomAttributeBuilder kelas , bersama dengan array jenis Object kosong untuk mewakili argumen.
CustomAttributeBuilder Hasilnya kemudian diteruskan ke DefineDynamicAssembly metode sebagai satu-satunya elemen array.
Kode contoh mendefinisikan modul dan jenis dalam rakitan dinamis baru, lalu menampilkan atribut assembly.
using namespace System;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
using namespace System::Security;
void main()
{
// Create a CustomAttributeBuilder for the assembly attribute.
//
// SecurityTransparentAttribute has a parameterless constructor,
// which is retrieved by passing an array of empty types for the
// constructor's parameter types. The CustomAttributeBuilder is
// then created by passing the ConstructorInfo and an empty array
// of objects to represent the parameters.
//
ConstructorInfo^ transparentCtor =
SecurityTransparentAttribute::typeid->GetConstructor(
Type::EmptyTypes);
CustomAttributeBuilder^ transparent = gcnew CustomAttributeBuilder(
transparentCtor,
gcnew array<Object^> {} );
// Create a dynamic assembly using the attribute. The attribute is
// passed as an array with one element.
AssemblyName^ aName = gcnew AssemblyName("EmittedAssembly");
AssemblyBuilder^ ab = AppDomain::CurrentDomain->DefineDynamicAssembly(
aName,
AssemblyBuilderAccess::Run,
gcnew array<CustomAttributeBuilder^> { transparent } );
ModuleBuilder^ mb = ab->DefineDynamicModule( aName->Name );
TypeBuilder^ tb = mb->DefineType(
"MyDynamicType",
TypeAttributes::Public );
tb->CreateType();
Console::WriteLine("{0}\nAssembly attributes:", ab);
for each (Attribute^ attr in ab->GetCustomAttributes(true))
{
Console::WriteLine("\t{0}", attr);
}
};
/* This code example produces the following output:
EmittedAssembly, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
Assembly attributes:
System.Security.SecurityTransparentAttribute
*/
using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Security;
class CustomAttributeBuilderSnippet
{
static void Main()
{
// Create a CustomAttributeBuilder for the assembly attribute.
//
// SecurityTransparentAttribute has a parameterless constructor,
// which is retrieved by passing an array of empty types for the
// constructor's parameter types. The CustomAttributeBuilder is
// then created by passing the ConstructorInfo and an empty array
// of objects to represent the parameters.
//
ConstructorInfo transparentCtor =
typeof(SecurityTransparentAttribute).GetConstructor(
Type.EmptyTypes);
CustomAttributeBuilder transparent = new CustomAttributeBuilder(
transparentCtor,
new Object[] {} );
// Create a dynamic assembly using the attribute. The attribute is
// passed as an array with one element.
AssemblyName aName = new AssemblyName("EmittedAssembly");
AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly(
aName,
AssemblyBuilderAccess.Run,
new CustomAttributeBuilder[] { transparent } );
ModuleBuilder mb = ab.DefineDynamicModule( aName.Name );
TypeBuilder tb = mb.DefineType(
"MyDynamicType",
TypeAttributes.Public );
tb.CreateType();
Console.WriteLine("{0}\nAssembly attributes:", ab);
foreach (Attribute attr in ab.GetCustomAttributes(true))
{
Console.WriteLine("\t{0}", attr);
}
}
}
/* This code example produces the following output:
EmittedAssembly, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
Assembly attributes:
System.Security.SecurityTransparentAttribute
*/
open System
open System.Reflection
open System.Reflection.Emit
open System.Security
// Create a CustomAttributeBuilder for the assembly attribute.
//
// SecurityTransparentAttribute has a parameterless constructor,
// which is retrieved by passing an array of empty types for the
// constructor's parameter types. The CustomAttributeBuilder is
// then created by passing the ConstructorInfo and an empty array
// of objects to represent the parameters.
//
let transparentCtor =
typeof<SecurityTransparentAttribute>.GetConstructor(Type.EmptyTypes)
let transparent = CustomAttributeBuilder(transparentCtor, [||])
// Create a dynamic assembly using the attribute. The attribute is
// passed as an array with one element.
let aName = AssemblyName "EmittedAssembly"
let ab = AppDomain.CurrentDomain.DefineDynamicAssembly(
aName,
AssemblyBuilderAccess.Run,
[| transparent |])
let mb = ab.DefineDynamicModule aName.Name
let tb = mb.DefineType(
"MyDynamicType",
TypeAttributes.Public )
tb.CreateType() |> ignore
printfn $"{ab}\nAssembly attributes:"
for attr in ab.GetCustomAttributes true do
printfn $"\t{attr}"
(* This code example produces the following output:
EmittedAssembly, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
Assembly attributes:
System.Security.SecurityTransparentAttribute
*)
Imports System.Reflection
Imports System.Reflection.Emit
Imports System.Security
Module Example
Sub Main()
' Create a CustomAttributeBuilder for the assembly attribute.
'
' SecurityTransparentAttribute has a parameterless constructor,
' which is retrieved by passing an array of empty types for the
' constructor's parameter types. The CustomAttributeBuilder is
' then created by passing the ConstructorInfo and an empty array
' of objects to represent the parameters.
'
Dim transparentCtor As ConstructorInfo = _
GetType(SecurityTransparentAttribute).GetConstructor( _
Type.EmptyTypes)
Dim transparent As New CustomAttributeBuilder( _
transparentCtor, _
New Object() {} )
' Create a dynamic assembly Imports the attribute. The attribute is
' passed as an array with one element.
Dim aName As New AssemblyName("EmittedAssembly")
Dim ab As AssemblyBuilder = _
AppDomain.CurrentDomain.DefineDynamicAssembly( _
aName, _
AssemblyBuilderAccess.Run, _
New CustomAttributeBuilder() { transparent } )
Dim mb As ModuleBuilder = ab.DefineDynamicModule( aName.Name )
Dim tb As TypeBuilder = mb.DefineType( _
"MyDynamicType", _
TypeAttributes.Public )
tb.CreateType()
Console.WriteLine("{0}" & vbLf & "Assembly attributes:", ab)
For Each attr As Attribute In ab.GetCustomAttributes(True)
Console.WriteLine(vbTab & "{0}", attr)
Next
End Sub
End Module
' This code example produces the following output:
'
'EmittedAssembly, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
'Assembly attributes:
' System.Security.SecurityTransparentAttribute
Keterangan
Gunakan metode ini kelebihan beban untuk menentukan atribut yang tidak berfungsi dengan benar kecuali jika diterapkan saat rakitan dinamis dibuat. Misalnya, atribut keamanan seperti SecurityTransparentAttribute dan SecurityCriticalAttribute tidak berfungsi dengan benar jika ditambahkan setelah rakitan dinamis dibuat.
Metode ini harus digunakan hanya untuk menentukan rakitan dinamis di domain aplikasi saat ini. Untuk informasi selengkapnya tentang pembatasan ini, lihat Load(AssemblyName) metode kelebihan beban.
Metode ini diperkenalkan dalam .NET Framework 3.5.
Lihat juga
Berlaku untuk
DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess)
Menentukan rakitan dinamis dengan nama dan mode akses yang ditentukan.
public:
virtual System::Reflection::Emit::AssemblyBuilder ^ DefineDynamicAssembly(System::Reflection::AssemblyName ^ name, System::Reflection::Emit::AssemblyBuilderAccess access);
public System.Reflection.Emit.AssemblyBuilder DefineDynamicAssembly (System.Reflection.AssemblyName name, System.Reflection.Emit.AssemblyBuilderAccess access);
abstract member DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess -> System.Reflection.Emit.AssemblyBuilder
override this.DefineDynamicAssembly : System.Reflection.AssemblyName * System.Reflection.Emit.AssemblyBuilderAccess -> System.Reflection.Emit.AssemblyBuilder
Public Function DefineDynamicAssembly (name As AssemblyName, access As AssemblyBuilderAccess) As AssemblyBuilder
Parameter
- name
- AssemblyName
Identitas unik dari perakitan dinamis.
- access
- AssemblyBuilderAccess
Mode akses untuk rakitan dinamis.
Mengembalikan
Rakitan dinamis dengan nama dan mode akses yang ditentukan.
Penerapan
Pengecualian
name
adalah null
.
Properti Name
dari name
adalah null
.
-atau-
Properti Name
name
dimulai dengan spasi kosong, atau berisi garis miring ke depan atau mundur.
Operasi ini dicoba pada domain aplikasi yang tidak dimuat.
Contoh
Sampel berikut menunjukkan DefineDynamicAssembly metode dan AssemblyResolve peristiwa.
Agar contoh kode ini berjalan, Anda harus memberikan nama rakitan yang sepenuhnya memenuhi syarat. Untuk informasi tentang cara mendapatkan nama rakitan yang sepenuhnya memenuhi syarat, lihat Nama Rakitan.
using namespace System;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
ref class Test
{
public:
static void InstantiateMyDynamicType( AppDomain^ domain )
{
try
{
// You must supply a valid fully qualified assembly name here.
domain->CreateInstance( "Assembly text name, Version, Culture, PublicKeyToken", "MyDynamicType" );
}
catch ( Exception^ e )
{
Console::WriteLine( e->Message );
}
}
static Assembly^ MyResolveEventHandler( Object^ sender, ResolveEventArgs^ args )
{
return DefineDynamicAssembly( dynamic_cast<AppDomain^>(sender) );
}
static Assembly^ DefineDynamicAssembly( AppDomain^ domain )
{
// Build a dynamic assembly using Reflection Emit API.
AssemblyName^ assemblyName = gcnew AssemblyName;
assemblyName->Name = "MyDynamicAssembly";
AssemblyBuilder^ assemblyBuilder = domain->DefineDynamicAssembly( assemblyName, AssemblyBuilderAccess::Run );
ModuleBuilder^ moduleBuilder = assemblyBuilder->DefineDynamicModule( "MyDynamicModule" );
TypeBuilder^ typeBuilder = moduleBuilder->DefineType( "MyDynamicType", TypeAttributes::Public );
ConstructorBuilder^ constructorBuilder = typeBuilder->DefineConstructor( MethodAttributes::Public, CallingConventions::Standard, nullptr );
ILGenerator^ ilGenerator = constructorBuilder->GetILGenerator();
ilGenerator->EmitWriteLine( "MyDynamicType instantiated!" );
ilGenerator->Emit( OpCodes::Ret );
typeBuilder->CreateType();
return assemblyBuilder;
}
};
int main()
{
AppDomain^ currentDomain = AppDomain::CurrentDomain;
Test::InstantiateMyDynamicType( currentDomain ); // Failed!
currentDomain->AssemblyResolve += gcnew ResolveEventHandler( Test::MyResolveEventHandler );
Test::InstantiateMyDynamicType( currentDomain ); // OK!
}
using System;
using System.Reflection;
using System.Reflection.Emit;
class DefinedDynamicAssemblySnippet {
public static void Main() {
AppDomain currentDomain = AppDomain.CurrentDomain;
InstantiateMyDynamicType(currentDomain); // Failed!
currentDomain.AssemblyResolve += new ResolveEventHandler(MyResolveEventHandler);
InstantiateMyDynamicType(currentDomain); // OK!
}
static void InstantiateMyDynamicType(AppDomain domain) {
try {
// You must supply a valid fully qualified assembly name here.
domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyDynamicType");
} catch (Exception e) {
Console.WriteLine(e.Message);
}
}
static Assembly MyResolveEventHandler(object sender, ResolveEventArgs args) {
return DefineDynamicAssembly((AppDomain) sender);
}
static Assembly DefineDynamicAssembly(AppDomain domain) {
// Build a dynamic assembly using Reflection Emit API.
AssemblyName assemblyName = new AssemblyName();
assemblyName.Name = "MyDynamicAssembly";
AssemblyBuilder assemblyBuilder = domain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("MyDynamicModule");
TypeBuilder typeBuilder = moduleBuilder.DefineType("MyDynamicType", TypeAttributes.Public);
ConstructorBuilder constructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, null);
ILGenerator ilGenerator = constructorBuilder.GetILGenerator();
ilGenerator.EmitWriteLine("MyDynamicType instantiated!");
ilGenerator.Emit(OpCodes.Ret);
typeBuilder.CreateType();
return assemblyBuilder;
}
}
open System
open System.Reflection
open System.Reflection.Emit
let instantiateMyDynamicType (domain: AppDomain) =
try
// You must supply a valid fully qualified assembly name here.
domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyDynamicType")
|> ignore
with e ->
printfn $"{e.Message}"
let defineDynamicAssembly (domain: AppDomain) =
// Build a dynamic assembly using Reflection Emit API.
let assemblyName = AssemblyName()
assemblyName.Name <- "MyDynamicAssembly"
let assemblyBuilder = domain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run)
let moduleBuilder = assemblyBuilder.DefineDynamicModule "MyDynamicModule"
let typeBuilder = moduleBuilder.DefineType("MyDynamicType", TypeAttributes.Public)
let constructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, null)
let ilGenerator = constructorBuilder.GetILGenerator()
ilGenerator.EmitWriteLine "MyDynamicType instantiated!"
ilGenerator.Emit OpCodes.Ret
typeBuilder.CreateType() |> ignore
assemblyBuilder
let myResolveEventHandler (sender: obj) _ =
defineDynamicAssembly (sender :?> AppDomain)
:> Assembly
let currentDomain = AppDomain.CurrentDomain
instantiateMyDynamicType currentDomain // Failed!
currentDomain.add_AssemblyResolve(ResolveEventHandler myResolveEventHandler)
instantiateMyDynamicType currentDomain // OK!
Imports System.Reflection
Imports System.Reflection.Emit
Module Test
Sub Main()
Dim currentDomain As AppDomain = AppDomain.CurrentDomain
InstantiateMyDynamicType(currentDomain) 'Failed!
AddHandler currentDomain.AssemblyResolve, AddressOf MyResolveEventHandler
InstantiateMyDynamicType(currentDomain) 'OK!
End Sub
Sub InstantiateMyDynamicType(domain As AppDomain)
Try
' You must supply a valid fully qualified assembly name here.
domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyDynamicType")
Catch e As Exception
Console.WriteLine(e.Message)
End Try
End Sub
Function MyResolveEventHandler(sender As Object, args As ResolveEventArgs) As System.Reflection.Assembly
Return DefineDynamicAssembly(DirectCast(sender, AppDomain))
End Function 'MyResolveEventHandler
Function DefineDynamicAssembly(domain As AppDomain) As System.Reflection.Assembly
' Build a dynamic assembly using Reflection Emit API.
Dim assemblyName As New AssemblyName()
assemblyName.Name = "MyDynamicAssembly"
Dim assemblyBuilder As AssemblyBuilder = domain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run)
Dim moduleBuilder As ModuleBuilder = assemblyBuilder.DefineDynamicModule("MyDynamicModule")
Dim typeBuilder As TypeBuilder = moduleBuilder.DefineType("MyDynamicType", TypeAttributes.Public)
Dim constructorBuilder As ConstructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, Nothing)
Dim ilGenerator As ILGenerator = constructorBuilder.GetILGenerator()
ilGenerator.EmitWriteLine("MyDynamicType instantiated!")
ilGenerator.Emit(OpCodes.Ret)
typeBuilder.CreateType()
Return assemblyBuilder
End Function 'DefineDynamicAssembly
End Module 'Test
Keterangan
Metode ini hanya boleh digunakan untuk menentukan rakitan dinamis di domain aplikasi saat ini. Untuk informasi selengkapnya, lihat Load(AssemblyName) metode kelebihan beban.
Catatan
Selama pengembangan kode yang memancarkan rakitan dinamis, disarankan agar Anda menggunakan kelebihan beban DefineDynamicAssembly metode yang menentukan bukti dan izin, berikan bukti yang Anda inginkan untuk dimiliki perakitan dinamis, dan sertakan SecurityPermissionFlag.SkipVerification dalam refusedPermissions
. Termasuk SkipVerification dalam refusedPermissions
parameter memastikan bahwa MSIL diverifikasi. Keterbatasan teknik ini adalah juga menyebabkan SecurityException dilemparkan ketika digunakan dengan kode yang menuntut kepercayaan penuh.