1

Let's say you envision some extension method that would transform an object in some way into something useful for debugging purposes. One is example is a sqlCommand.ToScript() extension method that would return a string of the T-SQL script version of what it is about execute (it would declare parameters at the top of the script etc.) You'd like that extension method (or at least some function / method) to be available while debugging, but you don't want to actually define it in your code base as it would feel like clutter to the team.

I don't think Immediate Window can define functions, can it? There is the C# Interactive Window, but can that interact with the currently executing stack after I hit a breakpoint?

I've heard Reflection.Emit() can add new code at run time. Can I use that somehow?

15
  • 2
    #if DEBUG Commented Feb 7, 2020 at 17:26
  • 3
    Then you can group the debug methods into another parts of your class using partial classes. Commented Feb 7, 2020 at 17:32
  • 1
    Actually I forgot that an extension method goes in a separate static class that could be tucked away somewhere , out of view. My original concern was cluttering up an existing class definition. However, the best in my mind would be if you didn't have to make any changes to your solution at all to get it to work Commented Feb 7, 2020 at 17:45
  • 1
    @unnknown You can also make it a bit better, by putting the codes into another assembly and add reference them conditionally. See the example. Commented Feb 7, 2020 at 18:33
  • 1
    So you may want to take a look at this or this like later. Commented Feb 7, 2020 at 19:12

1 Answer 1

2

Apart from extending Immediate Window or creating new extension for VS, I can think of the following solutions to let you have some debug methods:

  • Using #if DEBUG + Partial Classes or Extension Methods
  • Using Conditional Reference + Extension Methods
  • Using Assembly.Load in Immediate Window + Extension Methods

I'll share example for each of the above solutions.

Using #if DEBUG + Partial Classes or Extension Methods

You can use #if DEBUG. For types in your code base, you can group the debug methods into another parts of your class using partial classes, like this:

public partial class MyClass
{
#if DEBUG
    public string SaySomething()
    {
        return "Something!";
    }
#endif 
}

For types which doesn't belong to you , you can use extension methods like this (You can also use this solution for the types which belongs to you):

public static class SqlCommandExtensions
{
#if DEBUG
    public static string SaySomething(this SqlCommand command)
    {
        return "Something!";
    }
#endif
}

Using Conditional Reference + Extension Methods

If you would like to put all these code in a different library, you car create a class library and put the extension classes in global namespace (no namespace) and then add a conditional reference:

<ItemGroup Condition=" '$(Configuration)' == 'Debug' ">
  <Reference Include="GlobalExtensions">
    <HintPath>..\Somewhere\GlobalExtensions.dll</HintPath>
  </Reference>
</ItemGroup>

In this solution, you don't need if debug.

public static class SqlCommandExtensions
{
    public static string SaySomething(this SqlCommand command)
    {
        return "Something!";
    }
}

Using Assembly.Load in Immediate Window + Extension Methods

As another option, you can create another assembly and then put these extension methods in global namespace (no namespace) in that assembly. Then without adding reference, and just at debug time, in the immediate window you can use them:

Assembly.LoadFile(@"C:\Somewhere\GlobalExtensions.dll")
yourSqlCommand.SaySomething()

In this solution, you don't need if debug, you also don't need any add reference and it's only available at debug time and immediate window:

public static class SqlCommandExtensions
{
    public static string SaySomething(this SqlCommand command)
    {
        return "Something!";
    }
}

You will not have intellisense for it in immediate Window, but it works.

Sign up to request clarification or add additional context in comments.

10 Comments

I don't think that addresses his "clutter" concerns, though it depends on exactly what he means by "clutter".
@BradleyUffner maybe "clutter" is a bit opinion based because OP can group the debug methods into another parts of the class using partial classes, and it's not "clutter" IMO.
These are good points. Still am curious if it's possible to to inject code after I hit a breakpoint though
You can add code after hitting a breakpoint but that means you'll be adding and removing code manually each time you debug.
I'm also thinking this solution wouldn't be useable in the specific example used by OP of adding a method to SqlCommand, since you can only create partial classes for classes that you "own".
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.