这是我的应用程序代码。简单的一个。我是 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] 删除。
我来说两句