dimanche 3 juin 2018

Cannot create delegate from generic deriving event to generic method of another class

Alright Buddies, I have the following Issue:

I want to hook up events of generic derived classes to a non generic master class' generic method.

Problem is, it tells me the signatures don't fit:

ArgumentException: Cannot bind to the target method because its signature differs from that of the delegate type

here are some logs:

  • when event is a delegate: Denis<T1>() where T1 : IFieldUpdater;

    handlerType: _FieldUpdater`1+Denis`1[IFieldUpdater,IFieldUpdater]
    methodInfo: Void UpdateSubscribers[T2]()
    
    
  • when event is a delegate: Denis()

    handlerType: _FieldUpdater`1+Denis[IFieldUpdater]
    methodInfo: Void UpdateSubscribers[T2]()
    
    

And if I'd know how to make expandable/collapsable Boxes in here, that would be great, so I can put in those relevant lines:

public static class FieldUpdateMaster
{

    private static void HookUpdaters()
    {
        Type updater = typeof(_FieldUpdater<IFieldUpdater>);
        object instance = updater.GetProperty(nameof(_FieldUpdater<IFieldUpdater>.Instance),
                BindingFlags.FlattenHierarchy |
                BindingFlags.Static |
                BindingFlags.Public)
            ?.GetValue(null); // get static Singleton Instance

        EventInfo eventInfo = updater.GetEvent(nameof(_FieldUpdater<IFieldUpdater>.UpdateSubscribers));

        MethodInfo methodInfo = typeof(FieldUpdateMaster).GetMethod(nameof(UpdateSubscribers),
            BindingFlags.NonPublic | BindingFlags.Static);

            Debug.Log($"handlerType: {eventInfo.EventHandlerType}\n" +
                      $"methodInfo: {methodInfo}");
        Delegate del = Delegate.CreateDelegate(eventInfo.EventHandlerType, methodInfo);
        eventInfo.AddMethod.Invoke(instance, new object[] {del});
    }

    private static void UpdateSubscribers<T2>() where T2 : IFieldUpdater{...}
}

//_AbstractSingleton creates an Instance of the First SubType of _FieldUpdater<T>
public abstract class _FieldUpdater<T> : _AbstractSingleton<_FieldUpdater<T>> where T : IFieldUpdater
{   
    protected internal abstract Type[] Types { get; }

    public event Action OnFieldUpdate;

    public delegate void Denis<T1>() where T1 : IFieldUpdater;

    public event Denis<T> UpdateSubscribers;
}

Thing is I have absolutely no clue what to do now. HELP^^





Aucun commentaire:

Enregistrer un commentaire