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.
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.