Skip to main content

Hot Reload Supports Modifying Generics!

Hot Reload is a powerful .NET feature that lets you edit your code while your app is running, and implements those changes without completely rebuilding your app. Hot Reload supports many of the common edits .NET developers make, but we always look at adding new capabilities with each new .NET version. After making a change to your code, the compiler analyzes the edit to see if the runtime supports it. If the change is unsupported, the compiler or Visual Studio tells you that your app needs to be restarted for the change to take effect – we call this a “Rude Edit”. Decreasing the amount of Rude Edits is one of the ways we make Hot Reload more versatile for all our .NET developers – which is why we’re excited to announce that in .NET 8, Hot Reload supports modifying Generics!

Supported Generic Scenarios

The supported generic scenarios for Hot Reload are:

  • Add new (static, instance) method to a (non)generic type.
  • Add new (static, instance) generic method to a (non)generic type.
  • Edit existing (static, instance) method on a (non)generic type.
  • Edit existing (static, instance) generic method on a (non)generic type.

To test out these features yourself, you can download the latest Visual Studio 17.7 preview or the latest .NET 8 preview. Find out what else is coming for developers in Announcing .NET 8 Preview 4 and other posts on the .NET blog. (add links) Hot Reload is available for use anywhere you use .NET.

Example 1 – Add new static generic method to a non-generic type

Adding a generic method to a non-generic type is supported in Hot Reload as of .NET 8.

using System;
public class AddGenericMethod1
{
    //The edit is removing the comment below
    //static void Foo<T>()
    //{
    //   Console.WriteLine(1);
    //}

    public static void Main()
    {
        Foo<int>();
        Console.WriteLine(2);
    }
}

Example 2 – Add new instance generic method to generic type

This could be in a running console app. A new generic method is added to type G by removing the commented code. Hot Reload can now integrate the new method into the console app without completely reloading it.

using System;
public class AddGenericMethodOnGenericType
{
    class G<T>
    {
        public static string AsString<U>(U u)
        {
            var s = u.ToString();
            Console.WriteLine(s);
            return s;
        }
        // The edit is removing the two comment groups below
        // Comment group 1
        // public string AsString2<U>(U u)
        // {
        //     var s = u.ToString();
        //     Console.WriteLine(""-"");
        //     return s;
        // }
    }
    public static void Main()
    {
        G<string>.AsString<int>(1);
        G<int>.AsString<string>(""2"");
        // Comment group 2
        // G<string>.AsString2<int>(1);
        // G<int>.AsString2<string>(""2"");
        Console.WriteLine(3);
    }
}

Example 3 – Real World Example

This is an example of editing an existing static generic method.

The following issue was filed in GitHub: Blazor Hot Reload generic. Start by creating a Blazor webpage. A webpage that has First Name, Email, Password and Verify Password lines. You can see that there are four lines on the webpage: first name, email, password and verify password. This code, <MudTextField Label="First name" HelperText="Max. 8 characters" @bind-Value="model.Username" For="@(() => model.Username)"/>, creates the First Name line with the helper text underneath that says Max 8 characters. Before generics were supported in .NET 8, if you removed the code and then used Hot Reload to attempt to refresh the webpage, an error would tell you to restart the entire webpage for the change to take effect. A message saying the app needs to restart for changes to take effect with code behind it. We call this error a “Rude Edit”. The Rude Edit happened because behind the scenes, some code changes in Blazor result in changes to generated generic methods. A picture showing generic code was modified Now that Hot Reload supports modifying generics in .NET 8, you can remove the same code, use Hot Reload to refresh your app, by pressing the Hot Reload icon, Visual Studio with the Hot Reload fire icon highlighted. and the updates are seen right away. A webpage that has only Email, Password and Verify Password lines. There are now only three lines on the webpage: email, password and verify password.

Want More?

Try this feature out today using .NET 8 preview 4 and Visual Studio preview 17.7 and let us know what other Rude Edits you want us to tackle via Help > Send Feedback > Suggest a Feature in Visual Studio, or on our GitHub!

The post Hot Reload Supports Modifying Generics! appeared first on .NET Blog.



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

Comments

Popular posts from this blog

Fake CVR Generator Denmark

What Is Danish CVR The Central Business Register (CVR) is the central register of the state with information on all Danish companies. Since 1999, the Central Business Register has been the authoritative register for current and historical basic data on all registered companies in Denmark. Data comes from the companies' own registrations on Virk Report. There is also information on associations and public authorities in the CVR. As of 2018, CVR also contains information on Greenlandic companies, associations and authorities. In CVR at Virk you can do single lookups, filtered searches, create extracts and subscriptions, and retrieve a wide range of company documents and transcripts. Generate Danish CVR For Test (Fake) Click the button below to generate the valid CVR number for Denmark. You can click multiple times to generate several numbers. These numbers can be used to Test your sofware application that uses CVR, or Testing CVR APIs that Danish Govt provide. Generate

How To Iterate Dictionary Object

Dictionary is a object that can store values in Key-Value pair. its just like a list, the only difference is: List can be iterate using index(0-n) but not the Dictionary . Generally when we try to iterate the dictionary we get below error: " Collection was modified; enumeration operation may not execute. " So How to parse a dictionary and modify its values?? To iterate dictionary we must loop through it's keys or key - value pair. Using keys

How To Append Data to HTML5 localStorage or sessionStorage?

The localStorage property allows you to access a local Storage object. localStorage is similar to sessionStorage. The only difference is that, while data stored in localStorage has no expiration time untill unless user deletes his cache, data stored in sessionStorage gets cleared when the originating window or tab get closed. These are new HTML5 objects and provide these methods to deal with it: The following snippet accesses the current domain's local Storage object and adds a data item to it using Storage.setItem() . localStorage.setItem('myFav', 'Taylor Swift'); or you can use the keyname directly as : localStorage.myFav = 'Taylor Swift'; To grab the value set in localStorage or sessionStorage, we can use localStorage.getItem("myFav"); or localStorage.myFav There's no append function for localStorage or sessionStorage objects. It's not hard to write one though.The simplest solution goes here: But we can kee