jeudi 23 juin 2022

How to create a compatible delegate from its type?

I'm trying to create a Standard library with common types for WPF/UWP etc.
Since many types are not in Standard, sometimes you have to use reflection.
In this connection, the task of creating a DispatchedHandler from an Action arose.
In UWP, this is easy: new DispatchedHandler(action);.
But in Standard I only have Type from DispatchedHandler.

    private object GetDispatchedHandler(Action action)
    {
        // Some code 

        dispatchedHandlerType = windowsAssembly.GetType("Windows.UI.Core.DispatchedHandler");

        // Next, you need to transform the `action` and return it from the method.
        return Activator.CreateInstance(dispatchedHandlerType, action);
    }

This code throws an exception: "Constructor on type 'Windows.UI.Core.DispatchedHandler' not found".

But can get a constructor from a type: dispatchedHandlerType.GetConstructors()[0].
The constructor has the following signature: {Void .ctor(System.Object, IntPtr)}.
The first parameter, I think, should be passed action.
What should be passed as the second parameter?
dispatchedHandlerConsructor.Invoke(new object[] { action, ??? });





Aucun commentaire:

Enregistrer un commentaire