Howto debug a dll loaded at runtime?

F

Fabio Cannizzo

I load an assembly DLL dynamically using
System.AppDomain.CurrentDomain.Load(... ).
I create the istance of an object contained in the assembly and invoke one
of its method.
How can I make the debugger stop within the code of this DLL?

If my question is not clear, please have a look below... Thanks a lot


In ClassLibrary1.dll I have

interface MyInterface {
int MyMethod();
}

In ClassLibrary2.dll I have

public MyClass : MyInterface {
int MyMethod() { retrun 10; );
}


In Main.exe I have

.....
FileStream fs = new FileStream(@"ClassLibrary2.dll", FileMode.Open);
byte[] buffer = new byte[(int) fs.Length];
fs.Read(buffer, 0, buffer.Length);
fs.Close();
Assembly asm = System.AppDomain.CurrentDomain.Load( buffer ); // load the
assembly
Type[] tp = asm.GetTypes();
foreach ( Type t in tp )
if (null != t.GetInterface( "MyInterface" ) ) { // scan for types
which implement MyInterface
// call default constructor with no parameters
Object obj = t.InvokeMember(null, BindingFlags.DeclaredOnly |
BindingFlags.Public | BindingFlags.Instance | BindingFlags.CreateInstance,
null, null, null);
// invole a method
int i = (int) ((MyInterface)obj).MyMethod(); // I would like the
debugger to stop during the execution of MyMethod. How can I do???
}
.....
 
J

Joshua Flanagan

Assuming you have the ClassLibrary2.dll project loaded in Visual Studio
(along with ClassLibrary1), you should be able to step into the code if
you put a breakpoint on the call to MyMethod.

Curious - do you need to load the assembly as a stream?

If not, your code might be simpler load the assembly like this:

Assembly asm = Assembly.Load("ClassLibrary2");



Joshua Flanagan
http://flimflan.com/blog
 
F

Fabio Cannizzo

Hi Joshua.

Thanks for the tip. I had to use AssemblyLoadFile(..) instead of
Assembly.Load(), but now it seems to work (also the debugger).

Regards,
Fabio
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top