Skip to content

Blazor: Typecasting issue with Eventcallback<T> and it's argument #12689

@gurunathancs1991

Description

@gurunathancs1991

Issue details.

Am using Eventcallback<T> type for events in my custom component . I need to do some action like type casting for argument before call to user component functional handler. hence it's need to be done in intermediate.

First, Trigger is a method which is JSinvokable one. Getting the data from JS end, arguments are serialized to string. now we need to convert it to proper type and invokeAsync to user component function handler.

Here, we cannot use the type directly. but having type details in a variable.

Here am facing some type casting issue. please find the code snippets.

    public class EventData
    {

       // Holding the function handler
        public object Handler { get; set; }
        // type of function handler, argument 
        public Type ArgumentType { get; set; }
        // set the event data details in a collection 
        public EventData Set<T>(EventCallback<T> action, Type type)
        {
            this.Handler = action;
            this.ArgumentType = type;
            return this;
        }
    }
// collection variable to hold this details. 
 protected Dictionary<string, EventData> DelegateList = new Dictionary<string, EventData>();

image

In above image, you can error. how can i resolve and do proper casting ?

I have already tried the Generic invoke method.

 public object InvokeGenericMethod(string name, Type type, Type parentType, params 
           object[] arguments)
           {
            MethodInfo method = parentType.GetMethod(name, BindingFlags.Instance | 
            BindingFlags.NonPublic);
            return method.MakeGenericMethod(type).Invoke(this, arguments);
            }

But this is sync action. so we are getting issues to return the data to JS end

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions