简体   繁体   English

Blazor:对EventCallback使用动态参数时,无效的参数类型匹配异常<T>

[英]Blazor: Invalid argument type match exception when using dynamic for EventCallback<T>

I have custom component, in which having multiple events and each one have unique T type. 我有一个自定义组件,其中有多个事件,每个事件都有唯一的T类型。 Then JSInvokable method is common one (Entry point), from where i need to invoke the exact event functional handler. 然后JSInvokable方法是常见的方法(入口点),在这里我需要从中调用确切的事件函数处理程序。

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: 请检查抛出的错误截图:

![image][ https://user-images.githubusercontent.com/12065858/62135248-84725200-b2ff-11e9-8624-cbcae3193151.png] ![image] [ https://user-images.githubusercontent.com/12065858/62135248-84725200-b2ff-11e9-8624-cbcae3193151.png]

Comp.razor 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 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 EventData类

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 here. 您可以从此处找到问题重现示例。

https://github.com/gurunathancs1991/BlazorEventhandler https://github.com/gurunathancs1991/BlazorEventhandler

whether this is an issue with eventCallback conversion with dynamic type? 动态类型的eventCallback转换是否存在问题? any other work around for this? 还有其他解决方法吗?

change: 更改:

get { return (EventCallback<ChangeEventArgs>)this.GetEvent("change"); }

To: 至:

get { return (EventCallback<ChangeEventArgs>)(object) this.GetEvent("change"); }

Perhaps now you don't need to use dynamic... 也许现在您不需要使用动态...

Good luck... 祝好运...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM