Consider building x86 from the built-in "Any CPU" application on an x64-bit OS

I have a .Net application that is compiled as "Any CPU". I run it on x64 OS, so it works like 64bit. The application loads other assemblies that the user provides. It uses reflection, of course, to read types from a user-supplied assembly. Everything works fine if the user assembly is compiled as "Any processor." But if the assembly compiles as x86, I get an exception "this is not a Win32 application" during reflection. This is obvious due to the fact that the host application runs on a 64-bit basis.

My question is, how can I get around this? Any thoughts / ideas?

thanks

+3
source share
3 answers

Ok I understood. For my purposes, which were just a simple type discovery for an assembly, but without instantiating, using Assembly.ReflectionOnlyLoad works if the assembly is 32-bit.

You load Assembly.ReflectionOnlyLoad and you are allowed to reflect types. You must also connect to AppDomain.CurrentDomain.ReflectionOnlyLoadResolve.

To get the attribute names, you need to use the CustomAttributeData.GetCustomAttributes attributes for the type, method or module.

 static void Main(string[] args)
    {
        AppDomain.CurrentDomain.ReflectionOnlyAssemblyResolve += new ResolveEventHandler(CurrentDomain_ReflectionOnlyAssemblyResolve);
        Assembly assm = Assembly.ReflectionOnlyLoadFrom("TestProject1.dll");

        Type t = assm.GetType("TestProject1.ProgramTest");
        MethodInfo m = t.GetMethod("MainTest");

        IList<CustomAttributeData> data = CustomAttributeData.GetCustomAttributes(t);


    }

    static Assembly CurrentDomain_ReflectionOnlyAssemblyResolve(object sender, ResolveEventArgs args)
    {
        return Assembly.ReflectionOnlyLoad(args.Name);
    }
+5
source

You can copy the file and change the bit.

0
source

, Mono.Cecil, , , .

dll, corflags , 32- , .

, , . - (dll , , .

. 32-, . 64-?

0

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


All Articles