9

I am debugging an assembly which I loaded dynamically with Assembly.Load(Byte[]), but I am facing some problems.

First of all, I can't move the yellow arrow in Visual Studio 2010 to step into other lines of code, and also I am getiing exceptions ("Cannot find the method on the object instance.") when trying to do a quick watch on objects from a third party library (controls from Infragistics for example.)

Dim data = My.Computer.FileSystem.ReadAllBytes(file.FullName)
Assembly.Load(data)

When using Assembly.Load(String), everything works fine, and there are no problems.

Assembly.Load(IO.Path.GetFileNameWithoutExtension(file.Name))

Any idea why the behaviour is that much different? Anyway to fix this?

I tried loading the debugging symbols for my assembly with Assembly.Load(byte[]. byte[]), but I still get exceptions when trying to debug objects from third party libraries.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501

2 Answers2

15

The debug symbols for your assembly are not being loaded into the application domain. When you use the string variety, .NET automatically looks for a .PDB alongside the filename you specify.

To load an assembly and its symbols from byte arrays, use Assembly.Load(byte[], byte[]), like so:

Dim data = My.Computer.FileSystem.ReadAllBytes(file.FullName)
Dim pdbData = My.Computer.FileSystem.ReadAllBytes(pdbFile.FullName)
Assembly.Load(data, pdbData)
ladenedge
  • 13,197
  • 11
  • 60
  • 117
  • I tried this, and the symbols got loaded, but I have still problems with classes/objects from dependencies of this assembly. –  Oct 20 '11 at 14:04
  • You will have to manually load dependencies as well. Subscribing to the [AssemblyResolve](http://stackoverflow.com/questions/22012/loading-assemblies-and-its-dependencies/22026#22026) event may be helpful in your case. – ladenedge Oct 20 '11 at 14:08
1

When you pass it an array of bytes there's no way to know what file it comes from (or if it even comes from a file) so it can't locate the PDB file with the source code line information.

You can fix this by saving the byte[] as a file and making sure there's a PDB for it with the same file name.

Mark Cidade
  • 98,437
  • 31
  • 224
  • 236