Skip to content

Code containing async lambda fails to update when defined in a reloadable type #63103

@tmat

Description

@tmat

Originally reported as VS Feedback https://dev.azure.com/devdiv/DevDiv/_workitems/edit/1580286, bug https://dev.azure.com/devdiv/DevDiv/_workitems/edit/1581287.

F5 the following console app (targeting net7.0), update Console.WriteLine('.'); to Console.WriteLine(':'); and apply changes (Hot Reload)

using System.Data;
using System.Reflection.Metadata;
using System.Runtime.CompilerServices;
[assembly: MetadataUpdateHandlerAttribute(typeof(Handler))]

class Handler
{
    static void ClearCache(Type[]? updatedTypes)
    {
        
    }

    static void UpdateApplication(Type[]? updatedTypes)
    {
        if (updatedTypes != null)
        {
            Console.WriteLine("Update: " + String.Join(",", updatedTypes.Select(t => t.Name)));

            var newType = updatedTypes.FirstOrDefault(t => t.Name.StartsWith("C#"));
            if (newType != null)
            {
                C.Instance = (I)Activator.CreateInstance(newType)!;
            }
        }
    }
}

interface I
{
    Task F();
}

[CreateNewOnMetadataUpdate]
class C : I
{
    public static I Instance = new C();

    public static void Main()
    {
        while (true)
        {
            Instance.F();
            Thread.Sleep(1000);
        }
    }

    public void F()
    {
        Console.WriteLine('.');
        G(() => { });  // exception
    }

    public void G(Action f) => f();
}

System.ArgumentException: 'Delegate to an instance method cannot have null 'this'.'

>	System.Private.CoreLib.dll!System.MulticastDelegate.ThrowNullThisInDelegateToInstance() Line 570	C#
 	System.Private.CoreLib.dll!System.MulticastDelegate.CtorClosed(object target, nint methodPtr) Line 577	C#
 	ConsoleApp26.dll!C#1.F() Line 71	C#
 	ConsoleApp26.dll!C.Main() Line 55	C#

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions