简体   繁体   中英

Is it possible to declare C# method / function while debugging?

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM