这在某种程度上是对我之前提出的问题的跟进,尽管我现在能够提供更多代码来改进我的问题并进一步展示我在这方面的问题。
我这里有三个例程。其中两个例程一起工作 - 如果成功,将使用 System.Reflection 将程序集加载到内存中。如果文件没有正确加载到内存中,我希望这些例程返回错误,但由于某种原因,这些 try-catch 语句根本无法按我想要的方式工作。
注意:要使该例程工作,该文件必须是 .net 程序集。例如,如果文件是在 VB6 中编程的,则会引发错误。这是我试图返回给我的错误。
private void ExecuteDataIntoMemory(string filePath)
{
byte[] bytes = File.ReadAllBytes(filePath);
try
{
ExecFile(bytes);
MessageBox.Show("successfully loaded this file into memory");
}
catch
{
MessageBox.Show("Could not load this file into memory");
}
}
private static void ExecFile(byte[] data)
{
try
{
//Work around for "SetCompatibleTextRenderingDefault"
System.Threading.Thread T = new System.Threading.Thread(ExecFile);
//Set STA to support drag/drop and dialogs?
T.SetApartmentState(System.Threading.ApartmentState.STA);
T.Start(data);
}
catch
{
MessageBox.Show("caught some error ...");
}
}
private static void ExecFile(object o)
{
System.Reflection.MethodInfo T = System.Reflection.Assembly.Load((byte[])o).EntryPoint;
if (T.GetParameters().Length == 1)
T.Invoke(null, new object[] { new string[] { } });
else
T.Invoke(null, null);
}
如有必要,我可以澄清更多,但我不确定此时要包括哪些其他信息。