المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Problem loading assembly from byte[]



C# Programming
04-21-2009, 02:40 PM
Hi,

I've got this issue with loading an assembly from a byte array.

What happens?
First I create an assembly from C#-code which is generated. Then I stream the assembly to a byte[] and store this array in the database (column type = image, MSSQL 2000).

When I need the assembly, I retrieve the byte[] from the db and want to load it. I try to load the assembly using Assembly.Load(array), and then it breaks. For some strange reason my VS decided to give me a Dutch error while my ******** is set to English:

{"Could not load file or assembly '169 bytes loaded from FormuleCompiler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. Poging om een programma te laden met een onjuiste indeling."}

Which I think translates to:
{"Could not load file or assembly '169 bytes loaded from FormuleCompiler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. An attempt was made to load a program with an incorrect format."}

I'm having trouble finding a good solution for it, as I don't know where it goes wrong. It all happens in the same namespace, by the same package. So the creation, streaming and loading happens in the same dll.

Source I use is below:
public CompiledFormula GetCompiledFormula(string cstext)
{
// Create assembly by compile
Assembly ass = Compile(cstext);

CompiledFormula result = (CompiledFormula)ass.CreateInstance("Compiler.COMPILEDFORMULA");
return result;
}
The byte conversion as follows:
public byte[] GetArrayFromAssembly(Assembly formula)
{
byte[] assembly;

try
{
using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
{
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter =
new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();

formatter.Serialize(stream, formula);
assembly = stream.ToArray();

}
catch(Exception ex)
{
throw ex;
}
}
And this is the loading routine:

public CompiledFormula LoadFormuleFromAssembly(byte[] array)
{
try
{
Assembly assembly = Assembly.Load(array);

CompiledFormula formule = (CompiledFormula)ass.CreateInstance("Compiler.COMPILEDFORMULA");

return formule;
}
catch (System.Exception ex)
{
throw ex;
}
}
Anyone has an idea? Also, tell me if I make a horrible mistake here http://www.barakasoft.com/script/Forums/Images/smiley_wink.gif

The consumer isn't a moron; she is your wife.