Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hot reload notification/event

Is there a way to get notified of a hot reload event? I've looked on the dotnet/sdk repo but couldn't find anything. I was hoping for either an event, a way to register a callback function or maybe even just a "version" of sorts that I could compare to realize the code changed. I'm mostly interested in getting notified for resetting a few things, and perhaps reload an AssemblyLoadContext.

like image 972
Philippe Paré Avatar asked Oct 31 '25 23:10

Philippe Paré


1 Answers

Add this somewhere in the global namespace:

[assembly: System.Reflection.Metadata.MetadataUpdateHandler( typeof(MyNameSpace.MyHandler) )]

Then, write a class like this:

namespace MyNameSpace;

internal static class MyHandler
{
    public static void UpdateApplication( Type[]? _ )
    {
        System.Windows.Application.Current!.Dispatcher.BeginInvoke( myHandlerMethod );
    }

    private static void myHandlerMethod()
    {
        Debug.WriteLine( "Hot Reload!" );
    }
}

DotNet will automagically invoke UpdateApplication() despite the fact that MyHandler does not extend any known class, nor does it implement any known interface. It will call the method by virtue of it having this particular signature.

It appears that DotNet invokes UpdateApplication() in some arbitrary thread, so BeginInvoke() is necessary in order to bring the call into the UI thread. This particular example of how to bring the call into the UI thread assumes you are using WPF; if not, you may have to make some slight adjustments.

like image 76
Mike Nakis Avatar answered Nov 03 '25 21:11

Mike Nakis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!