BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News .NET Hot Reload Adds Support for Modifying Generics

.NET Hot Reload Adds Support for Modifying Generics

In the .NET 8 Preview 4 release, .NET Hot Reload implements support for modifying generic methods, reducing the number of changes that require restarting for the change to take effect.

With Hot Reload it’s possible to edit your .NET application's code while the application is running and apply those changes without restarting it. Hot Reload supports many of the common C# features that developers use but didn’t support generics until .NET 8 Preview 4.

Adding support for modifying generics at runtime required changes in coreclr, so developers need Visual Studio 17.7 preview and .NET 8 preview 4 to try these features.

In .NET, 8 Hot Reload supports modifying generic code in the following four scenarios: adding a new method to a generic type, adding a new generic method to a (non)generic type, editing an existing method in a generic type, and editing an existing generic method in a (non)generic type.

The following sample shows a code snippet for formatting any `IFormattable` as a string:

static class FormattingHelpers
{
    public static string ToString<T>(this T item, string format) where T : IFormattable
    {
       return item.ToString(format, CultureInfo.InvariantCulture);
    }
}

With Hot Reload now supporting generics, developers can add a new method that supports formatting nullable values, without stopping the application. For example:

static class FormattingHelpers
{
    public static string ToString<T>(this T item, string format) where T : IFormattable
    {
       return item.ToString(format, CultureInfo.InvariantCulture);
    }

    public static string ToString<T>(this T? item, string format) where T : struct, IFormattable
    {
       return item.Value.ToString(format, CultureInfo.InvariantCulture);
    }
}

As editing an existing method is supported as well, the new method can be extended on the fly to accept null values too:

static class FormattingHelpers
{
    public static string ToString<T>(this T item, string format) where T : IFormattable
    {
          return item.ToString(format, CultureInfo.InvariantCulture);
    }

    public static string ToString<T>(this T? item, string format) where T : struct, IFormattable
    {
       if (item == null)
       {
           return "";
       }
       return item.Value.ToString(format, CultureInfo.InvariantCulture);
    }
}

The Microsoft article provides a more real-world example: In a Blazor project with first name, email, password and verify password fields, removing the following markup for the First Name field and a helper text results in a change to a generated generic method:

<MudTextField Label="First name" HelperText="Max. 8 characters" @bind-Value="model.Username" For="@(() => model.Username)"/>

Source: https://devblogs.microsoft.com/dotnet/hot-reload-generics/

Before .NET 8, this change required restarting the application for the change to take effect.

Source: https://github.com/dotnet/aspnetcore/issues/46100

The changes can be now applied by clicking the Hot Reload icon in Visual Studio.

The developer community on the .NET subreddit welcomed the improvements.

This is great! It would be cool to see a comprehensive overview of what still is not supported so we can track progress. But maybe that already exists and I just haven't seen it yet.

Sounds great! Does this also mean modifying lambdas on Linq works now? Usually, my workaround for it is to remove the whole method chain call, then do a hot reload, then add it back with the new edits so it doesn't cause a recompile.

Support for the asynchronous programming model remains the top most requested feature on the subreddit:

Now have it support async/await state machine changes, which you know, kinda takes over a codebase virally.

The .NET team calls on developers to test the new feature and share feedback or to suggest on GitHub new scenarios that they want Hot Reload to support.

 

About the Author

Rate this Article

Adoption
Style

BT