How to load dll during debugging in VS2013

I have a code

var aa = a(); b(aa); 

During debugging, I set a breakpoint on the b() call. Then, going to the nearest window, I would like to execute the code from the DLL, which is in my project, but not yet loaded. Let's say I need a new Boo and a call to Foo() . The code is located in the Baz namespace in the dll Spongle.dll .

When i type

 >> new Baz.Boo().Foo(aa) 

I get an error: The type or name of the Baz namespace is not valid in this area.

If I change my code so that my Boo already loaded, it works fine.

 new Boo(); // dummy to ensure loading var aa = a(); b(aa); 

Is it possible to load a DLL from a window during debugging so that I can call my code despite loading it (for now) ?. I could use new Boo() as a static initializer of the main class of my application, but then I have problems during unit testing, since it does not have to include a class with this static initializer.

+6
source share
1 answer

Although heavy, you can, of course, use reflection to load the assembly for this test.

The following steps will not work:

 var obj = new Newtonsoft.Json.Linq.JObject(); 

since the assembly does not exist yet. However, if I explicitly load it first through reflection and the absolute path to my mailbox, I can instantiate the object just fine.

 var assembly = System.Reflection.Assembly.LoadFile("C:\\AbsolutePath\\bin\\Debug\\Newtonsoft.Json.dll"); var obj = new Newtonsoft.Json.Linq.JObject(); 

The reason for this need from the Immediate window is that when your application (or unit test application with the application extension in this case) loads, it searches for links in all code and loads the necessary assemblies to meet your needs. In your case, you do not have an explicit reference to the assembly in your code, so it does not load. The immediate window has no context and therefore you must explicitly load it.

To programmatically reference potential assemblies for download, you can use the bin directory of the downloaded assembly. This allows you to drag the absolute path at runtime.

 var filePath = new Uri(this.GetType().Assembly.CodeBase).LocalPath; var bin = System.IO.Path.GetDirectoryName(filePath); var assembly = System.Reflection.Assembly.LoadFile(bin + "\\Newtonsoft.Json.dll"); 
+2
source

Source: https://habr.com/ru/post/985823/


All Articles