使用Assembly.Load()函数从内存中提取NET EXE文件,但是当我将字节数组提供给该函数时,在汇编中得到名称为...的BadImageFomatException重复类型。

c# - Assembly.Load()重复的名称为“”的类型-LMLPHP

我的代码是:

try{
                    // prepare the Form to show balloontip
                    frmDefault frm = new frmDefault();

                    // prepare to load the application into memory (using Assembly.Load)

                    // read the bytes from the application exe file
                    FileStream fs = new FileStream(filePath, FileMode.Open);
                    BinaryReader br = new BinaryReader(fs);
                    byte[] bin = br.ReadBytes(Convert.ToInt32(fs.Length));
                    fs.Close();
                    br.Close();

                    // load the bytes into Assembly
                    Assembly a = Assembly.Load(bin);
                    // search for the Entry Point
                    MethodInfo method = a.EntryPoint;
                    if (method != null)
                    {

                        // create an istance of the Startup form Main method
                        object o = a.CreateInstance(method.Name);
                        Console.Write("Application started!");
                        method.Invoke(o, null);

                    }
                    else
                    {
                        // impossible to launch the application
                        Console.Write("Application error!");
                    }
                }
                catch
                {

                }

最佳答案

第一:


 // read the bytes from the application exe file
 FileStream fs = new FileStream(filePath, FileMode.Open);
 BinaryReader br = new BinaryReader(fs);
 byte[] bin = br.ReadBytes(Convert.ToInt32(fs.Length));
 fs.Close();
 br.Close();

 // load the bytes into Assembly
 Assembly a = Assembly.Load(bin);



可以替换为:

var a = Assembly.LoadFile(filePath);


关于此问题,看来您正在从同一二进制文件加载2个不同的版本。从加载文件的位置翻倍。

关于c# - Assembly.Load()重复的名称为“”的类型,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36826810/

10-13 06:05