为什么C#反射抛出这个BadImageFormatException

时间:2017-12-20 18:39:23

标签: c# reflection

这是我的应用程序代码。简单一点。我是C#的新手。我尝试了其他完美的例子。但是例外情况“尝试加载格式不正确的程序”。我不知道为什么。请告诉我。有没有一种方法,Core.dll不允许动态加载和反射调用。

           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 });

我收到以下异常: -

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.

1 个答案:

答案 0 :(得分:1)

错误消息确切地告诉您错误:

  

警告:不会在LoadFrom上下文中探测本机映像。仅在默认加载上下文中探测本机映像,例如Assembly.Load()。

您的程序集不是.NET IL 程序集,并且无法在您尝试加载它时加载。您需要改为使用Assembly.Load()