开发者

Delegate.CreateDelegate causes ArgumentException

I have to use some functions included in a 3rd party assembly (by DevExpress) prior to the one I use in 开发者_StackOverflow社区my main WPF application. I tried to use it through Reflection and by external aliasing in order to avoid conflict between classes, events (and so on) having the same name between the two assemblies.

Since the fact I had problems in using these solutions, finally I decided to create a wrapper external to my main project having in its references the "old" assembly, and by declaring the classes I need just inheriting from the assembly classes.

This solution seemed to work properly, but I'm facing a big problem concerning event handling. I have to handle an event defined in both assemblies, so I tried to follow the same way I described before.

In my wrapper application I have:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using DevExpress.Xpf.Printing;


    namespace WrapperSimpleLink101
    {
    //declaration of my event
public delegate void EventHandlerNew<ClassEvento>(object obj, ClassEvento ClEv);
    //declaration of a working class
public class Class1 : DevExpress.Xpf.Printing.SimpleLink
{
    public ColumnDirection vDirezione;

    public Class1(ColumnDirection direzione)
    {
        vDirezione = direzione;
    }
    public ColumnDirection direzione
    {
        get { return vDirezione; }
        set { vDirezione = value; }
    }
    //event raising DelegateCasting
    public void SetCreateDetail(EventHandlerNew<ClassEvento> pD)
    {
        this.CreateDetail += DelegateUtility.Cast<EventHandler<CreateAreaEventArgs>>(pD);
    }
}
    //my event
public class ClassEvento : DevExpress.Xpf.Printing.CreateAreaEventArgs
{
    public ClassEvento(int detailindex): base(detailindex)
    {
        this.detailIndex = detailindex;
    }

    public int detailIndex { get; set; }        
}
    //utility to cast eventhandlernew<ClassEvento> to EventHandler<CreateAreaEventArgs>
public static class DelegateUtility
{

    public static T Cast<T>(Delegate source) where T : class
    {
        return Cast(source, typeof(T)) as T;
    }

    public static Delegate Cast(Delegate source, Type type)
    {
        if (source == null)
            return null;

        Delegate[] delegates = source.GetInvocationList();

        if (delegates.Length == 1)
    // the line where it's raised error ArgumentException: Error binding to target method
            **return Delegate.CreateDelegate(type, delegates[0].Target, delegates[0].Method);**

        Delegate[] delegatesDest = new Delegate[delegates.Length];

        for (int nDelegate = 0; nDelegate < delegates.Length; nDelegate++)
                delegatesDest[nDelegate] = Delegate.CreateDelegate(type,
                delegates[nDelegate].Target, delegates[nDelegate].Method);

        return Delegate.Combine(delegatesDest);

    }

}
    }

In Main WPF Application:

    using WrapperSimpleLink101;
    [...]
    Class1 slink = new Class1(ColumnDirection.AcrossThenDown);
    [...]
    //invocation of eventhandlernew
    slink.SetCreateDetail(new EventHandlerNew<ClassEvento>(slink_CreateDetail));
    [...]
    public void slink_CreateDetail(object sender, ClassEvento e)
    {
        e.Data = this.scoreCards[e.DetailIndex];
    }

I have to use casting delegates to handle this event because I cannot pass directly (in main application) to slink.CreateDetail my ClassEvento (instead of CreateAreaEventArgs) even if ClassEvento inherits from CreateAreaEventArgs (and I could argue also the reason), but I have the error shown before.

Making deeper debugging I noticed that the EventHandler passed to my external wrapper threw an exception in type System.InvalidOperationException, exactly "Method may only be called on a Type for which Type.IsGenericParameter is true", but I'm not sure that the main error and this one are connected.

I tried to solve by myself and through MSDN/Internet, but without luck, so I'm writing here to ask the community help. If you have suggests, they will be very appreciated. If something is not clear, please ask me further info. Thanks in advance for replies. Nino

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜