Click here to Skip to main content
15,922,512 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
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();  

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
Posted
Updated 18-Mar-10 20:24pm
v3

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 ?
 
Share this answer
 
>>> 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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900