如何将Assembly转换为byte []? [英] How to convert Assembly to byte[]?

查看:135
本文介绍了如何将Assembly转换为byte []?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好.
我有一个程序集,我需要将其转换为byte []或Stream.
我使用了以下代码:

Hi all.
I have an assembly and I need to convert it to byte[] or Stream.
I used following code:

byte[] bytes;  
 
MemoryStream stream = new MemoryStream();  
BinaryFormatter formatter = new BinaryFormatter();  
formatter.AssemblyFormat = 
    System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Full;
formatter.Serialize(stream, results.CompiledAssembly);  
bytes = stream.ToArray();  


但是,加载失败并显示以下错误消息.

{错误的IL格式."}无法加载文件或程序集"从Orion.ExpressionEngine加载的169个字节,版本= 3.5.0.0,文化=中性,公钥=空,"或其依赖项之一.尝试了此操作加载格式不正确的程序."

对不起,我的英语


However, the load fails with the following error message.

{"Bad IL format."} "Could not load file or assembly ''169 bytes loaded from Orion.ExpressionEngine, Version=3.5.0.0, Culture=neutral, PublicKeyToken=null'' or one of its dependencies. An attempt was made to load a program with an incorrect format."

Sorry for my english

推荐答案

这将有助于知道哪个行给出了错误.为什么需要在运行时执行此操作?您不是真的试图将exe转换为字节数组吗?为什么不从HDD读取它?
It would help to know what line gives the error. Why do you need to do this at runtime ? Aren''t you really trying to turn your exe into a byte array ? Why not read it from the HDD ?


>>>为什么需要在运行时执行此操作?您不是真的试图将exe转换为字节数组吗?为什么不从HDD读取它呢?


他之所以想这样做有很多原因:
1/如果从文件加载失败(通常在代码被混淆,类型不显示,GetTypes失败以及加载的情况下失败),则使用Assembly的byte []版本在单独的AppDomain中加载程序集.逐个模块运行,并使用try .. catch仅选择可以加载的模块.
>>> Why do you need to do this at runtime ? Aren''t you really trying to turn your exe into a byte array ? Why not read it from the HDD ?


There are many reasons why he would want to do it this way:
1/ loading an assembly in a separate AppDomain using the byte[] version of the Assembly if load from file has failed (typically when code is obfuscated, Types don''t show up and GetTypes fails, as well as Load. You have to go module by module and use try.. catch to select only those modules you can load.


这篇关于如何将Assembly转换为byte []?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆