Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Blazor:使用dynamic for EventCallback时参数类型匹配异常无效<;T>;_C#_Asp.net Core_Dynamic_Blazor - Fatal编程技术网

C# Blazor:使用dynamic for EventCallback时参数类型匹配异常无效<;T>;

C# Blazor:使用dynamic for EventCallback时参数类型匹配异常无效<;T>;,c#,asp.net-core,dynamic,blazor,C#,Asp.net Core,Dynamic,Blazor,我有一个自定义组件,其中有多个事件,每个事件都有唯一的T类型。然后JSInvokable方法是公共方法(入口点),我需要从这里调用确切的事件函数处理程序 在执行此操作时,我需要将参数和函数处理程序转换为适当的类型。但由于类型转换的问题,我使用了动态类型 但在运行时事件中,即使传递了正确的参数类型,我也会遇到以下问题 请检查抛出的错误屏幕截图: !![图片][ 复合剃须刀 @using Typecasting @using System.Threading.Tasks; @using Newto

我有一个自定义组件,其中有多个事件,每个事件都有唯一的T类型。然后JSInvokable方法是公共方法(入口点),我需要从这里调用确切的事件函数处理程序

在执行此操作时,我需要将参数和函数处理程序转换为适当的类型。但由于类型转换的问题,我使用了动态类型

但在运行时事件中,即使传递了正确的参数类型,我也会遇到以下问题

请检查抛出的错误屏幕截图:

!![图片][

复合剃须刀


@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;
    }    

}

@使用打字
@使用System.Threading.Tasks;
@使用Newtonsoft.Json;
@继承基础;
@代码{
[参数]
公共事件和价值变化
{
获取{return(EventCallback)this.GetEvent(“change”);}
set{this.SetEvent(“change”,value);}
}
[参数]
公共事件回调焦点输出
{
获取{return(EventCallback)this.GetEvent(“blur”);}
set{this.SetEvent(“blur”,value);}
}
公共异步任务DummyCall()
{
//用于向案例显示问题的虚拟异步操作方法
return wait Task.Run(()=>{return“data”;});
}
[可调用]
//这是入口点
公共重写对象触发器(字符串eventName,字符串arg)
{
EventData data=this.DelegateList[eventName];
var eventarg=JsonConvert.DeserializeObject(arg,data.ArgumentType);
动态fn=data.Handler;
//这就是问题所在
fn.InvokeAsync(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;
        }

------

public Dictionary DelegateList=new Dictionary();
内部虚拟void SetEvent(字符串名称,EventCallback EventCallback)
{
if(this.DelegateList.ContainsKey(name))
{
this.DelegateList[name]=new EventData().Set(eventCallback,typeof(T));
}
其他的
{
this.DelegateList.Add(name,neweventdata().Set(eventCallback,typeof(T));
}
}
内部对象GetEvent(字符串名称)
{
if(this.DelegateList.ContainsKey(name)==false)
{
返回null;
}
返回此.DelegateList[name].Handler;
}
------
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;
        }

    }
公共类事件数据
{
公共对象处理程序{get;set;}
公共类型ArgumentType{get;set;}
公共EventData集(EventCallback操作,类型)
{
this.Handler=action;
this.ArgumentType=type;
归还这个;
}
}
你可以在这里找到复制样品的问题

这是否是动态类型的eventCallback转换的问题?是否有其他解决方法?

更改:

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

get{return(EventCallback)(object)this.GetEvent(“change”);}
也许现在您不需要使用动态


祝你好运…

将错误消息添加为文本,它必须是谷歌的。即使修改了动态变量并使用了适当的类型,也会出现同样的问题。但是引发异常。我上面向你展示的是如何从一种类型的EventCallback转换为另一种类型。但是,使用通用EventCallback仍然存在问题,可能问题就在这里。我没有办法运行你的应用程序,建议你参考github中的问题,关于generic EventCallback的问题。。。
get { return (EventCallback<ChangeEventArgs>)(object) this.GetEvent("change"); }