为什么我加载程序集时会显示:
Unhandled Exception: System.Reflection.TargetInvocationException: Exception has
been thrown by the target of an invocation. ---> System.TypeInitializationExcept
ion: The type initializer for 'T???????????????????' threw an exception. ---> Sy
stem.TypeInitializationException: The type initializer for 'System.Management.Ma
nagementPath' threw an exception. ---> System.OutOfMemoryException: Exception of
type 'System.OutOfMemoryException' was thrown.
at System.Threading.Thread.StartInternal(IPrincipal principal, StackCrawlMark
& stackMark)
at System.Threading.Thread.Start(StackCrawlMark& stackMark)
at System.Threading.Thread.Start()
at System.Management.MTAHelper.InitWorkerThread()
at System.Management.MTAHelper.CreateInMTA(Type type)
at System.Management.ManagementPath.CreateWbemPath(String path)
at System.Management.ManagementPath..cctor()
--- End of inner exception stack trace ---
at System.Management.ManagementScope._Clone(ManagementScope scope, Identifier
ChangedEventHandler handler)
at System.Management.ManagementObjectSearcher..ctor(ManagementScope scope, Ob
jectQuery query, EnumerationOptions options)
at System.Management.ManagementObjectSearcher..ctor(String queryString)
at T???????????????????..cctor()
--- End of inner exception stack trace ---
at T???????????????????.get_Is64Bit()
at ????????????????????.????????????????????()
at ????????????????????.????????????????????()
at ????????????????????.Main(String[] args)
--- End of inner exception stack trace ---
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments,
Signature sig, Boolean constructor)
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Objec
t[] parameters, Object[] arguments)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invoke
Attr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
at skip.Program.RunInternal(Byte[] encode, String pass)
at skip.Program.Main()
在我的例子中,首先我删除了我的pe格式的标题:
[DllImport("kernel32.dll")]
private static extern IntPtr ZeroMemory(IntPtr addr, IntPtr size);
[DllImport("kernel32.dll")]
private static extern IntPtr VirtualProtect(IntPtr lpAddress, IntPtr dwSize, IntPtr flNewProtect, ref IntPtr lpflOldProtect);
[DllImport("kernel32.dll")]
public static extern IntPtr GetModuleHandle(string lpModuleName);
public static void EraseHeader()
{
IntPtr address = GetModuleHandle(null);
IntPtr dwOld = default(IntPtr);
VirtualProtect(address, (IntPtr)4096, (IntPtr)0x40, ref dwOld);
ZeroMemory(address, (IntPtr)4096);
}
static void Main(string[] args)
{
Console.WriteLine("");
EraseHeader();
while(true)
Console.WriteLine("");
//Console.ReadKey();
}
然后我加载我的程序集:
Assembly exeAssembly = Assembly.Load(Buffer);
object[] parameters = new object[1];
exeAssembly.EntryPoint.Invoke(null, parameters);
结果显示上一个错误:
--- End of inner exception stack trace ---
我想了解研究目的,为什么要告诉我这个错误。
答案 0 :(得分:1)
考虑这个代码:
namespace StackOverflow44227962StackTraces
{
using System;
public class Program
{
static void Main(string[] args)
{
var a = new ClassA();
try
{
a.DoSomething();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
}
namespace StackOverflow44227962StackTraces
{
using System;
public class ClassA
{
public void DoSomething()
{
try
{
CallClassB();
}
catch (Exception ex)
{
throw new Exception("This is the outermost exception", ex);
}
}
private void CallClassB()
{
var b = new ClassB();
b.DoSomething();
}
}
}
namespace StackOverflow44227962StackTraces
{
using System;
public class ClassB
{
public void DoSomething()
{
var c = new ClassC();
try
{
c.DoSomething(null);
}
catch (ArgumentNullException ex)
{
throw new InvalidOperationException("This is the exception containing the innermost exception", ex);
}
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace StackOverflow44227962StackTraces
{
public class ClassC
{
public void DoSomething(object something)
{
if (something == null)
{
throw new ArgumentNullException(nameof(something), "This is the innermost exception");
}
}
}
}
这会抛出一个 ArgumentNullException
,它被包裹在一个 InvalidOperationException
中,而 Exception
又被包裹在一个 Exception cex = ex;
while (cex != null)
{
Console.WriteLine(cex.GetType().Name);
Console.WriteLine(cex.Message);
Console.WriteLine(cex.StackTrace);
Console.WriteLine();
cex = cex.InnerException;
}
中(我并不是建议这是什么时候开始的最佳实践)使用那些异常类型,我只是将它们用作示例),并且最外层异常的 ToString() 方法返回以下内容。
System.Exception: This is the outermost exception ---> System.InvalidOperationException: This is the exception containing the innermost exception ---> System.ArgumentNullException: This is the innermost exception (Parameter 'something') at StackOverflow44227962StackTraces.ClassC.DoSomething(Object something) in C:\Users\Simon\source\repos\StackOverflow44227962StackTraces\ClassC.cs:line 15 at StackOverflow44227962StackTraces.ClassB.DoSomething() in C:\Users\Simon\source\repos\StackOverflow44227962StackTraces\ClassB.cs:line 12 --- End of inner exception stack trace --- at StackOverflow44227962StackTraces.ClassB.DoSomething() in C:\Users\Simon\source\repos\StackOverflow44227962StackTraces\ClassB.cs:line 16 at StackOverflow44227962StackTraces.ClassA.CallClassB() in C:\Users\Simon\source\repos\StackOverflow44227962StackTraces\ClassA.cs:line 23 at StackOverflow44227962StackTraces.ClassA.DoSomething() in C:\Users\Simon\source\repos\StackOverflow44227962StackTraces\ClassA.cs:line 11 --- End of inner exception stack trace --- at StackOverflow44227962StackTraces.ClassA.DoSomething() in C:\Users\Simon\source\repos\StackOverflow44227962StackTraces\ClassA.cs:line 15 at StackOverflow44227962StackTraces.Program.Main(String[] args) in C:\Users\Simon\source\repos\StackOverflow44227962StackTraces\Program.cs:line 12
粗体文本与最里面的异常有关,斜体文本与包裹最里面的异常的异常有关,而纯文本与最外面的异常有关,一个被抓到了 Program 课上。这条消息“--- End of internal exception stack trace ---”只是意味着“这是关于内部异常的信息的结束,这之后的内容与围绕它的异常有关”。
如果您愿意,可以像这样报告异常及其内部异常:
mxGraphHandler.prototype.previewColor = 'red';
这将输出
Exception This is the outermost exception at StackOverflow44227962StackTraces.ClassA.DoSomething() in C:\Users\Simon\source\repos\StackOverflow44227962StackTraces\ClassA.cs:line 15 at StackOverflow44227962StackTraces.Program.Main(String[] args) in C:\Users\Simon\source\repos\StackOverflow44227962StackTraces\Program.cs:line 12 InvalidOperationException This is the exception containing the innermost exception at StackOverflow44227962StackTraces.ClassB.DoSomething() in C:\Users\Simon\source\repos\StackOverflow44227962StackTraces\ClassB.cs:line 16 at StackOverflow44227962StackTraces.ClassA.CallClassB() in C:\Users\Simon\source\repos\StackOverflow44227962StackTraces\ClassA.cs:line 23 at StackOverflow44227962StackTraces.ClassA.DoSomething() in C:\Users\Simon\source\repos\StackOverflow44227962StackTraces\ClassA.cs:line 11 ArgumentNullException This is the innermost exception (Parameter 'something') at StackOverflow44227962StackTraces.ClassC.DoSomething(Object something) in C:\Users\Simon\source\repos\StackOverflow44227962StackTraces\ClassC.cs:line 15 at StackOverflow44227962StackTraces.ClassB.DoSomething() in C:\Users\Simon\source\repos\StackOverflow44227962StackTraces\ClassB.cs:line 12
您可能会觉得更容易阅读。