Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify Namespace for a type created through Reflection.Emit?

Apologies if I'm missing something obvious, but when I create a new type with Reflection.Emit, how do I specify what namespace it should be in?

ie..

AssemblyName assemblyName = new AssemblyName();
assemblyName.Name = "TestDynamic";

AssemblyBuilder assemblyBuilder = 
    AppDomain.CurrentDomain.DefineDynamicAssembly(
        assemblyName,   
        AssemblyBuilderAccess.Save);

ModuleBuilder moduleBuilder =    
    AssemblyBuilder.DefineDynamicModule("TestDynamic", "TestDynamic.dll");

TypeBuilder myTestInterface =
    moduleBuilder.DefineType("MyTestInterface", 
    TypeAttributes.Public | TypeAttributes.Interface, typeof(object));

How do I give a namespace for myTestInterface?

like image 534
Binary Worrier Avatar asked Nov 18 '10 12:11

Binary Worrier


People also ask

What is emit in c#?

Emit is a powerful namespace in which we can dynamically emit transient and persisting assemblies at runtime. Reflection. Emit produces a low-level, language-neutral MSIL. Normally, we create an assembly by saving its source code to disk and then compiling that source code.

Which of the following namespace is required to create a dynamic assembly and optionally generate PE file on a disk?

Reflection. Emit namespace that allow a compiler or tool to emit metadata and Microsoft intermediate language (MSIL) at run time and optionally generate a portable executable (PE) file on disk. Script engines and compilers are the primary users of this namespace.

Which CLR API can be used to create dynamic assemblies System assembly generate system runtime create system reflection emit system dynamic?

A dynamic assembly is an assembly that is created using the Reflection Emit APIs. You can use AssemblyBuilder to generate dynamic assemblies in memory and execute their code during the same application run.


1 Answers

Define it where you define the type:

moduleBuilder.DefineType("MyNamespace.MyTestInterface",
               TypeAttributes.Public | TypeAttributes.Interface,
                typeof(object));
like image 120
Aliostad Avatar answered Oct 07 '22 00:10

Aliostad