Why C# reflection throw this BadImageFormatException

0

Here is my application code. Just simple one. I'm new in C#. I tried other examples which work perfectly. But the exception "An attempt was made to load a program with an incorrect format." I have no clue why. Please let me know. Is there a way that the Core.dll won't allow loading dynamically and call by refection.

           Assembly asm = null;
            try
            {
                // Excepition in this line
                 asm = Assembly.LoadFrom(@"C:\myproj\Core.dll");
            }
            catch (Exception e)
            {
                Console.WriteLine(e.GetBaseException());
                Console.WriteLine(e.Message);

            }

        MethodInfo staticMethodInfo = t.GetMethod("Create");
        if (staticMethodInfo == null)
        {

        }
        List<Assembly> assemblies = new List<Assembly>();
        staticMethodInfo.Invoke(null, new object[] { typeof(string), typeof(JObject), assemblies });

I get the following exception:-

System.BadImageFormatException: Could not load file or assembly 'file:///C:\myproj\Core.dll' or one of its dependencies. An attempt was made to load a program with an incorrect format.
File name: 'file:///C:\myproj\Core.dll'
   at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
   at System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
   at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
   at System.Reflection.RuntimeAssembly.InternalLoadFrom(String assemblyFile, Evidence securityEvidence, Byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm, Boolean forIntrospection, Boolean suppressSecurityChecks, StackCrawlMark& stackMark)
   at System.Reflection.Assembly.LoadFrom(String assemblyFile)
   at MyApp.Program.Main(String[] args) in C:\practice\csharp\MyApp\MyApp\Program.cs:line 61

=== Pre-bind state information ===
LOG: Where-ref bind. Location = C:\myproj\Core.dll
LOG: Appbase = file:///C:/practice/csharp/MyApp/MyApp/bin/Debug/
LOG: Initial PrivatePath = NULL
Calling assembly : (Unknown).
===
LOG: This bind starts in LoadFrom load context.
WRN: Native image will not be probed in LoadFrom context. Native image will only be probed in default load context, like with Assembly.Load().
LOG: Using application configuration file: C:\practice\csharp\MyApp\MyApp\bin\Debug\MyApp.vshost.exe.Config
LOG: Using host configuration file:
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Attempting download of new URL file:///C:/myproj/Core.dll.
ERR: Failed to complete setup of assembly (hr = 0x8007000b). Probing terminated.

Could not load file or assembly 'file:///C:\myproj\Core.dll' or one of its dependencies. An attempt was made to load a program with an incorrect format.
c#
reflection
asked on Stack Overflow Dec 20, 2017 by masiboo • edited Dec 20, 2017 by dbc

1 Answer

1

The error message tells you exactly what's wrong:

WRN: Native image will not be probed in LoadFrom context. Native image will only be probed in default load context, like with Assembly.Load().

Your assembly is not a .NET IL assembly, and cannot be loaded as you're trying to load it. You need to use Assembly.Load() instead.

answered on Stack Overflow Dec 20, 2017 by GalacticCowboy • edited Dec 20, 2017 by GalacticCowboy

User contributions licensed under CC BY-SA 3.0