3

How can you deploy an assembly so that you can call methods within a utility library from the "Immediate Window", without having to add solution project references to MyCompany.DebuggingTools.dll?

CONTEXT

I have written a utility library (lets call it: MyCompany.DebuggingTools.dll) that includes methods which are useful for debugging applications during the development process.

Ideally I would like to be able to:

  1. use the "Immediate Window" to call methods in my utility library
    • it is implied that the Visual Studio debugger will be attached to the application under test
  2. the application under test will not have any solution project references to MyCompany.DebuggingTools.dll
    • In essence, I am trying to extend the capabilities of the "Immediate Window".

MORE INFORMATION

  • Visual Studio 2017
  • Windows 10
  • the project...
    • is a simple C# class library
    • has been signed using a key file generated by Visual Studio
  • scenarios:
    1. MyCompany.DebuggingTools.dll is added as a reference to the project under test
      • I can call the tools library from the "Immediate Window". It works.
      • The project under test should not have a reference to the tools assembly.
    2. MyCompany.DebuggingTools.dll has not been added as a project reference
      • When I try to call one of the tools from the "Immediate Window", get the following error:
        • error CS0246: The type or namespace name 'MyCompany' could not be found (are you missing a using directive or an assembly reference?)
Pressacco
  • 2,815
  • 2
  • 26
  • 45
  • Was your assembly or pab file loaded in your VS as you said that you want to use the Immediate window? http://www.adamtuliper.com/2009/11/debugging-in-gac-all-ways-to-accomplish.html, the simple way is that we need to add reference to it, and then debug it in VS, but if you didn't add it as the reference, one issue is that how you really load this assembly or call a method in your project. – Jack Zhai Oct 04 '18 at 03:30
  • @JackZhai-MSFT : Thank you for your feedback. The challenge here is I *do not* want to explicitly add a reference to `MyCompany.DebuggingTools.dll` in my project solution. Think of the debugging tools like a third-party library that has no business being associated with the project under test. – Pressacco Oct 05 '18 at 13:33
  • Why don't you want to add a reference? How do you plan on using this assembly without adding a reference? GAC assemblies are not "callable" the GAC just acts as a global cache of assemblies that you can reference. So what your trying to do doesn't make a lot of sense to me – Liam Oct 05 '18 at 13:54
  • You should [read this](https://stackoverflow.com/a/14894758/542251) *The GAC is a runtime implementation detail* – Liam Oct 05 '18 at 13:56
  • @Liam : My comment in the original post about the GAC is misleading, and has been removed. – Pressacco Oct 05 '18 at 14:14
  • I don't see how that makes any difference. I'm pretty sure your fundamentally misunderstanding what that GAC is. – Liam Oct 05 '18 at 14:15
  • 1
    @Liam: I have a requirement that the debugging tools **must be** completely independent of the project under test. This is why I cannot simply add a reference to the project. Effectively, I am trying to extend the capabilities of the "Immediate Window". – Pressacco Oct 05 '18 at 14:17
  • 1
    It sounds like your trying to develop a visual studio plugin then. I'd do some research in this direction. For fear of repeating myself (a lot), like I said, your fundamentally misunderstanding what that GAC is. Putting something in the GAC doesn't mean you can call it in the immediate window. The immediate window will make calls into the **attached process** and what ever **that process references**. Adding something to the GAC doesn't make the process reference it – Liam Oct 05 '18 at 14:20
  • I think your thinking it works like an environment variable for a bat file or something. That's not what it is or how it works. – Liam Oct 05 '18 at 14:24
  • 2
    @Liam : I could be completely wrong, but it is my understanding that the _Immediate Window_ is more than that. For example you can manipulate the IDE (while the debugger is attached) by issuing commands like: `> Debug.Disassembly`. When I saw this I thought: perhaps there is a way to extend the _Immediate Window_ without having to write formal _Visual Studio_ extensions? Maybe there is a way to load the `MyCompany.DebuggingTools.dll` assembly while _Visual Studio_ is initializing? This is the Genesis of my original post. – Pressacco Oct 05 '18 at 14:45
  • Ok, one last time. This isn't going to happen. :) – Liam Oct 05 '18 at 15:10

1 Answers1

3

I was looking for something similar - this is a complete and utter hack, but works in the Visual Studio 2017 Immediate Window and might be worth doing if you're using it for a particularly gnarly debugging session:

dynamic xx = System.Activator.CreateInstance(System.Reflection.Assembly.LoadFile(@"C:\dev\ImmediateWindowHelpers.dll").GetType("FullyQualifiedNamespace.Helpers"));

Where the Helpers class is non-static. Now, I can call methods on it:

xx.PrintParameters(sqlParameterList);

Since that would return a string, it'll simply display in the immediate window. I tested this and it works for me. Again, this instance will only live for the debugging session and will need to be loaded each time. Not that big of a deal once you get used to it, it's a single line.

I would imagine a Visual Studio extension of some kind would be a bit more of an ideal situation, having it load the assembly for you whenever you are debugging, or what if there was a build-time inclusion of the assembly into the app domain? I've never looked into this stuff, but it seems worth doing if you spend a lot of time debugging.

Jason Bunting
  • 58,249
  • 14
  • 102
  • 93
  • 1
    Very interesting! I could be wrong, but I think you can create an alias for your code that loads the helper library. I think the syntax is: `alias `. You can definitely alias commands that are exposed by Visual Studio (e.g. anything in the menu)... I’m not sure if you can alias CSharp code. Unfortunately I don’t have access to a computer to try it right now. Thanks for your post Jason. – Pressacco Jan 19 '19 at 23:46
  • 1
    I wasn't able to get the `alias` to work. If I figure it out, I will post an update. – Pressacco Jan 21 '19 at 19:21
  • @Pressacco - okay. Again, it's fun to mess with. I work on an app that requires, unfortunately, tons of time in the debugger to support it. So, having help via methods such as these is very nice. I'll let you know if I develop anything additional, I'd like to make this as easy as possible. Again, maybe a VS plugin/extension that allowed you to specify in a configuration the DLLs to load whenever the debugger runs would be nice. – Jason Bunting Jan 22 '19 at 18:29