OYIon / LiveSharp

Public repository for the LiveSharp project
96 stars 4 forks source link

ArgumentException/VerificationException: GenericArguments violates the constraint of type 'TComponent' #106

Closed warappa closed 1 year ago

warappa commented 3 years ago

On a private project 1.6.36 an update causes an exception regarding generic arguments:

17:53:35.804: error: System.ArgumentException: GenericArguments[0], 'LiveSharp.Runtime.Virtual.VirtualType0', on 'Void OpenComponent[TComponent](Int32)' violates the constraint of type 'TComponent'.
 ---> System.Security.VerificationException: Method Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder.OpenComponent: type argument 'LiveSharp.Runtime.Virtual.VirtualType0' violates the constraint of type parameter 'TComponent'.

Also TypeInitializationException and ArgumentException exceptions are thrown directly after this:

error: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
 ---> System.TypeInitializationException: The type initializer for 'LiveSharp.<>33554449Assets' threw an exception.
 ---> System.TypeInitializationException: The type initializer for 'LiveSharp.<>DelegateCache' threw an exception.
 ---> System.ArgumentException: Object of type 'System.Action`2[System.Object,Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder]' cannot be converted to type 'LiveSharp.<>DelegateCache+LiveSharp.<>33554453Index+LiveSharp.<>100663478_BuildRenderTree'.

Unfortunately I don't have a repo yet, but I will try to provide you with one.

Full output

17:53:35.726: Received C# update
17:53:35.729: Received update for field Assets+<>c__DisplayClass0_0.<>9__25
...
17:53:35.803: Received update for method AssetManager.Web.UI.Blazor.Client.Pages.Assets+<<BuildRenderTree>b__0_9>d.MoveNext
17:53:35.804: error: System.ArgumentException: GenericArguments[0], 'LiveSharp.Runtime.Virtual.VirtualType0', on 'Void OpenComponent[TComponent](Int32)' violates the constraint of type 'TComponent'.
 ---> System.Security.VerificationException: Method Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder.OpenComponent: type argument 'LiveSharp.Runtime.Virtual.VirtualType0' violates the constraint of type parameter 'TComponent'.
   at System.RuntimeMethodHandle.GetStubIfNeeded(RuntimeMethodHandleInternal method, RuntimeType declaringType, RuntimeType[] methodInstantiation)
   at System.Reflection.RuntimeMethodInfo.MakeGenericMethod(Type[] methodInstantiation)
   --- End of inner exception stack trace ---
   at System.RuntimeType.ValidateGenericArguments(MemberInfo definition, RuntimeType[] genericArguments, Exception e)
   at System.Reflection.RuntimeMethodInfo.MakeGenericMethod(Type[] methodInstantiation)
   at LiveSharp.Runtime.IL.MethodMetadata.GetCompatibleMethod(IEnumerable`1 methodList, String methodName, Type[] parameterTypes, Int32[] parameterTypeTokens, Type returnType, Type[] genericTypeArguments, Boolean exactArgumentTypes, Boolean throwIfNotFound)
   at LiveSharp.Runtime.IL.MethodMetadata.ResolveMethodMember(Type type, String memberName, Type returnType, String parameterTypesValue, String genericArgumentsValue)
   at LiveSharp.Runtime.IL.MethodMetadata.DeserializeMembers(IEnumerable`1 memberElements)
   at LiveSharp.Runtime.IL.MethodMetadata..ctor(DocumentMetadata documentMetadata, XElement methodElement, VirtualAssembly virtualAssembly, VirtualMethodInfo virtualMethodInfo, ILogger logger)
   at LiveSharp.Runtime.Virtual.VirtualMethodInfo.CreateMethodMetadata()
   at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode)
   at System.Lazy`1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor)
   at System.Lazy`1.CreateValue()
   at LiveSharp.Runtime.Virtual.VirtualMethodInfo.get_Metadata()
   at LiveSharp.Runtime.IL.DocumentMetadata..ctor(XElement documentElement, VirtualAssembly virtualAssembly, ILogger logger, Func`2 methodFilter)
   at LiveSharp.Runtime.LiveSharpRuntime.UpdateDocument(XElement element, Func`2 methodFilter)
   at LiveSharp.Runtime.LiveSharpRuntime.LiveSharpMessageReceived(String messageContent, Byte contentType, Int32 groupId)
17:53:35.808: error: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
 ---> System.TypeInitializationException: The type initializer for 'LiveSharp.<>33554449Assets' threw an exception.
 ---> System.TypeInitializationException: The type initializer for 'LiveSharp.<>DelegateCache' threw an exception.
 ---> System.ArgumentException: Object of type 'System.Action`2[System.Object,Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder]' cannot be converted to type 'LiveSharp.<>DelegateCache+LiveSharp.<>33554453Index+LiveSharp.<>100663478_BuildRenderTree'.
   at System.RuntimeType.TryChangeType(Object value, Binder binder, CultureInfo culture, Boolean needsSpecialCast)
   at System.Reflection.RtFieldInfo.SetValue(Object obj, Object value, BindingFlags invokeAttr, Binder binder, CultureInfo culture)
   at System.Reflection.FieldInfo.SetValue(Object obj, Object value)
   at LiveSharp.Runtime.Virtual.VirtualAssembly.AddDelegateFieldMapping(Type declaringType, String methodName, String methodIdentifier, Type fieldHost, String fieldName, Type returnType)
   at LiveSharp.Runtime.LiveSharpRuntime.AddDelegateFieldMapping(Type declaringType, String methodName, String methodIdentifier, Type fieldHost, String fieldName, Type returnType)
   at LiveSharp.<>DelegateCache..cctor()
   --- End of inner exception stack trace ---
   at LiveSharp.<>DelegateCache.LiveSharp.<>33554449Assets..cctor()
   --- End of inner exception stack trace ---
   --- End of inner exception stack trace ---
   at System.RuntimeFieldHandle.SetValue(RtFieldInfo field, Object obj, Object value, RuntimeType fieldType, FieldAttributes fieldAttr, RuntimeType declaringType, Boolean& domainInitialized)
   at System.Reflection.RtFieldInfo.SetValue(Object obj, Object value, BindingFlags invokeAttr, Binder binder, CultureInfo culture)
   at System.Reflection.FieldInfo.SetValue(Object obj, Object value)
   at LiveSharp.Runtime.Virtual.VirtualAssembly.TryUpdateDelegateField(MethodMetadata methodMetadata, Delegate delegate)
   at LiveSharp.Runtime.IL.MethodMetadata.InitializeDelegate()
   at LiveSharp.Runtime.IL.DocumentMetadata..ctor(XElement documentElement, VirtualAssembly virtualAssembly, ILogger logger, Func`2 methodFilter)
   at LiveSharp.Runtime.LiveSharpRuntime.UpdateDocument(XElement element, Func`2 methodFilter)
   at LiveSharp.Runtime.LiveSharpRuntime.LiveSharpMessageReceived(String messageContent, Byte contentType, Int32 groupId)
ionoy commented 3 years ago

I feel like it has something to do with generics, which are not supported yet by LiveSharp. I'll need to see specific code though.

vertonghenb commented 3 years ago

@ionoy just create a generic component as described here: https://docs.microsoft.com/en-us/aspnet/core/blazor/components/templated-components?view=aspnetcore-5.0#generic-typed-components