При этом мне нужно преобразовать аргумент и функцию обработчик соответствующего типа. но из-за проблемы с приведением типов я использовал динамический тип.
Но во время выполнения я получаю следующую проблему, хотя передал правильный тип аргумента.
пожалуйста проверьте скриншот с ошибкой:

Comp.razor
Код: Выделить всё
@using Typecasting
@using System.Threading.Tasks;
@using Newtonsoft.Json;
@inherits Base;
@code {
[Parameter]
public EventCallback ValueChange
{
get { return (EventCallback)this.GetEvent("change"); }
set { this.SetEvent("change", value); }
}
[Parameter]
public EventCallback FocusOut
{
get { return (EventCallback)this.GetEvent("blur"); }
set { this.SetEvent("blur", value); }
}
public async Task 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;
}
}
Код: Выделить всё
public Dictionary DelegateList = new Dictionary();
internal virtual void SetEvent(string name, EventCallback eventCallback)
{
if (this.DelegateList.ContainsKey(name))
{
this.DelegateList[name] = new EventData().Set(eventCallback, typeof(T));
}
else
{
this.DelegateList.Add(name, new EventData().Set(eventCallback, typeof(T)));
}
}
internal object GetEvent(string name)
{
if (this.DelegateList.ContainsKey(name) == false)
{
return null;
}
return this.DelegateList[name].Handler;
}
------
Код: Выделить всё
public class EventData
{
public object Handler { get; set; }
public Type ArgumentType { get; set; }
public EventData Set(EventCallback action, Type type)
{
this.Handler = action;
this.ArgumentType = type;
return this;
}
}
является ли это проблемой с преобразованием eventCallback с помощью динамический тип? какой-нибудь другой обходной путь для этого?
Подробнее здесь: https://stackoverflow.com/questions/572 ... ventcallba
Мобильная версия