I don't understand why this exception is thrown. I've seen similar topics but haven't been able to sort it out. Here is my code:
static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("mydll.dll"))
{
byte[] assemblyData = new byte[stream.Length];
stream.Read(assemblyData, 0, assemblyData.Length);
return Assembly.Load(assemblyData);
}
}
stream
is always null when I do new byte[stream.Length];
and throws exception. Funny thing, this works. But I want to have all the code clean. I've tried checking if the var is null before doing anything or a while (stream == null) {}
, initialize variables first... but in these cases the dll won't load.
How do I do it?
EDIT: The dll is added to my project and its build action
is Content
. As I say, it works, but throws exception...