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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了为什么C#反射抛出这个BadImageFormatException相关的知识,希望对你有一定的参考价值。

这是我的应用程序代码。简单一点。我是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.
答案

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

警告:不会在LoadFrom上下文中探测本机映像。原生图像只能在默认加载上下文中进行探测,就像使用Assembly.Load()一样。

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

以上是关于为什么C#反射抛出这个BadImageFormatException的主要内容,如果未能解决你的问题,请参考以下文章

java中的反射怎么用c实现

有关反射和简单工厂模式的简单实现

java 反射工具包reflections

使用 java 反射运行时,HDFS Parquet 文件阅读器抛出 DistributedFileSystem.class 未找到

运行 fat jar 的 ktor 抛出 java.lang.UnsupportedOperationException::Kotlin 反射中尚不支持包和文件外观

java开发--反射技术