Creating a dynamic enumeration and attempting to reference it is not performed using BindingFailure

I need to create a dynamic enumeration and then get the type using Type.GetType (). Is it possible?

In the code below, a dynamic enumeration and an attempt to use its qualified name will be created. This is fine if I store the assembly first (using AssemblyBuilderAccess.RunAndSave). However, this is not possible if I use only AssemblyBuilderAccess.Run; a BindingFailure error occurs; could not find assembly. My impression was that the Run option allowed me to create and use assemblies without actually storing (or have access to various builders).

(Note: the use of the Type.GetType () code below is not mine. I cannot change this code.)

How can I create a dynamic enumeration and reference it without saving the assembly?

        private Type CreateType()
        {          
        // Define the assembly.
        System.Reflection.Emit.AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(new System.Reflection.AssemblyName("temporaryAssembly"), AssemblyBuilderAccess.Run);

        // Actually create it.
        System.Reflection.Emit.ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("temporaryAssembly");

        // Create the enum.
        System.Reflection.Emit.EnumBuilder enumBuilder = moduleBuilder.DefineEnum("Temp", System.Reflection.TypeAttributes.Public, typeof(int));

        /* Populate the enum. */

                    return enumBuilder.CreateType();
        }

        private void DoStuff()
        {
                    Type t = CreateType();
                    Type createAnotherOfSameType = Type.GetType(t.AssemblyQualifiedName);
        }/
+3
source share
2 answers

This is actually documented on MSDN :

GetType , . GetType , , System.Reflection.Emit, . , , , RunAndSave Save System.Reflection.Emit.AssemblyBuilderAccess. [...] GetType, null. GetType ; , GetType null.

, , AppDomain.AssemblyResolve .

+2

, . : 1) Type, , ; , . 2) IIRC, AssemblyQualifiedName null / .

-Oisin

0

Source: https://habr.com/ru/post/1719399/


All Articles