首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Assembly.Load()与名称为‘’的类型重复

Assembly.Load()与名称为‘’的类型重复
EN

Stack Overflow用户
提问于 2016-04-25 01:54:01
回答 1查看 206关注 0票数 2

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

我的代码是:

代码语言:javascript
复制
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
                {

                }
EN

回答 1

Stack Overflow用户

发布于 2016-04-25 02:30:36

首先:

//从应用程序可执行文件中读取字节FileStream fs = FileStream(filePath,FileMode.Open);BinaryReader br =新BinaryReader(fs);byte[] bin = br.ReadBytes(Convert.ToInt32(fs.Length));fs.Close();br.Close();//将字节加载到程序集a= Assembly.Load(bin);

可以替换为:

代码语言:javascript
复制
var a = Assembly.LoadFile(filePath);

关于这个问题,你似乎从同一个二进制文件中加载了两个不同的版本。从加载文件的位置加倍。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36826810

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档