TAGS :Viewed: 5 - Published at: a few seconds ago

[ Assembly.Load() Dublicate type with name ' ' ]

NET EXE file from memory with Assembly.Load() function but when i give my byte array to this function i get a BadImageFomatException Duplicate type with name ... in assembly.

enter image description here

my code is :

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
                {

                }

Answer 1


First:

 // 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);

Can be replaced with:

var a = Assembly.LoadFile(filePath);

About the problem, it seems you are loading 2 diferent versions from same binary file. Double from where you are loading the files.