0

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...

сами J.D.
  • 423
  • 1
  • 5
  • 15
  • 1
    If it throws an exception, it is not working – Matias Cicero Aug 05 '15 at 03:42
  • 1
    Clearly you have not configured your resources correctly for the code you are using to retrieve the data. You can call `GetManifestResourceNames()` as a diagnostic technique to investigate what resources exist and what their names are. Note that `Embedded Resource` is typically the build action you want for embedded resources. For further diagnostic help, check the advice provided in the duplicate question. – Peter Duniho Aug 05 '15 at 07:11
  • Thanks, will take a look. – сами J.D. Aug 05 '15 at 11:01

0 Answers0