Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

Blazor: Invalid argument type match exception when using dynamic for EventCallback

$
0
0

I have custom component, in which having multiple events and each one have unique T type. Then JSInvokable method is common one (Entry point), from where i need to invoke the exact event functional handler.

While doing so, i need to convert the argument and Function handler in appropriate type. but due to type casting issue, i used dynamic type.

But am getting below issue in run time eventhough passed a proper argument type.

please check the error thrown screenshot:

enter image description here

Comp.razor

@using Typecasting@using System.Threading.Tasks;@using Newtonsoft.Json;@inherits Base;<input id="gencomp" type="text" />@code {    [Parameter]    public EventCallback<ChangeEventArgs> ValueChange    {        get { return (EventCallback<ChangeEventArgs>)this.GetEvent("change"); }        set { this.SetEvent<ChangeEventArgs>("change", value); }    }    [Parameter]    public EventCallback<FocusOutEventArgs> FocusOut    {        get { return (EventCallback<FocusOutEventArgs>)this.GetEvent("blur"); }        set { this.SetEvent<FocusOutEventArgs>("blur", value); }    }    public async Task<string> DummyCall()    {        // dummy async action method to show case the issue        return await Task.Run(() => { return "data"; });    }    [JSInvokable]    // this is entry point    public override object Trigger(string eventName, string arg)    {        EventData data = this.DelegateList[eventName];        var eventarg = JsonConvert.DeserializeObject(arg, data.ArgumentType);        dynamic fn = data.Handler;        // here am getting the issue        fn.InvokeAsync(eventarg);        return eventarg;    }}

base.cs

public Dictionary<string, EventData> DelegateList = new Dictionary<string, EventData>();internal virtual void SetEvent<T>(string name, EventCallback<T> eventCallback){    if (this.DelegateList.ContainsKey(name))    {        this.DelegateList[name] = new EventData().Set<T>(eventCallback, typeof(T));    }    else    {        this.DelegateList.Add(name, new EventData().Set<T>(eventCallback, typeof(T)));    }}internal object GetEvent(string name){    if (this.DelegateList.ContainsKey(name) == false)    {        return null;    }    return this.DelegateList[name].Handler;}------

EventData class

public class EventData{    public object Handler { get; set; }    public Type ArgumentType { get; set; }    public EventData Set<T>(EventCallback<T> action, Type type)    {        this.Handler = action;        this.ArgumentType = type;        return this;    }}

you can find the issue reproducing sample from github.com.

whether this is an issue with eventCallback<T> conversion with dynamic type? any other work around for this?


Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>