为什么 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()改用。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

C ++为什么会抛出?

为什么这个 C 程序会崩溃?

为什么这个计数函数会抛出错误“必须声明标量变量“@MyColumn”。” C#

为什么这个C ++函数会产生混乱的输出?

为什么这个 C 代码会产生无限循环?

为什么这个 partialFilterExpression 会抛出错误?

为什么这个映射声明会抛出类型错误?

为什么这个 activerecord find 查询会抛出错误?

为什么这个程序会抛出错误?

Reflection.Emit抛出BadImageFormatException

为什么在这个方法中声明这个变量会覆盖我的类成员(C++)?

为什么这个新创建的 MVC 应用程序会抛出这个错误?

这个 C 指针错误是什么意思?为什么会这样

为什么C ++没有反射?

当我从 C# 代码调用导入的 C++ 函数时,为什么会抛出 AccessViolationException?

为什么这个'C'代码的输出是这样的?

为什么这个C程序的输出是这样的?

为什么 C 不截断这个?

转到库中的C#| BadImageFormatException

pinvoke c函数-System.BadImageFormatException

为什么在 C# 中实例化 COM 对象会抛出异常

为什么添加这个构造函数会导致我的 API 在 C# 中工作?

为什么这个C ++函数会产生如此多的分支错误预测?

为什么这个 C 结构体初始化代码会产生总线错误?

这个 C 程序可以工作,但编译会产生错误。为什么?

为什么打印这个浮点数会导致 c 中的答案错误

为什么我在 C 中使用 strcat 会遇到这个问题?

为什么这个 css 会抛出一个语法错误?

为什么会这样?(增加用户)C ++