Delegate.Cast throw ArgumentException Error
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Delegate.Cast throw ArgumentException Error

  1. #1
    Join Date
    Aug 2011
    Posts
    2

    Exclamation Delegate.Cast throw ArgumentException Error

    I have to use some functions included in a 3rd party assembly (by DevExpress) prior to the one I use in 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)
    return Delegate.CreateDelegate(type, delegates[0].Target, delegates[0].Method);

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

    for (int nDelegate = 0; nDelegate < delegates.Length; nDelegate++)
    // the line where it's raised error ArgumentException: Error binding to target method
    **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

  2. #2
    Join Date
    Jan 2010
    Posts
    1,099

    Re: Delegate.Cast throw ArgumentException Error

    Welcome to the forums.
    And, both of your name-conflicting assemblies have their classes in the DevExpress.Xpf.Printing namespace? Seems a bit unlikely...

    If the namespaces are different, than it's easy - just use the fully qualified names.

    P.S. Please use the [code][/code] tags when posting code, as people here will find it easier to read and understand it if you do, and they'll be more likely to help.

  3. #3
    Join Date
    Aug 2011
    Posts
    2

    Smile Re: Delegate.Cast throw ArgumentException Error

    Hello!

    Yes, it's sadly so, because the classes I need are in Xpf.Printing v 10.1, while the whole project uses Xpf.Printing v 10.2, and there's no backward compatibility.
    Thanks for the tip about tags

    Bye
    Nino

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,255

    Re: Delegate.Cast throw ArgumentException Error

    Rather than using inheritance, use composition when creating your wrapper classes.

    For example, instead of:
    Code:
    public class ClassEvento : DevExpress.Xpf.Printing.CreateAreaEventArgs
    {
    
    }
    Try
    Code:
    public class ClassEvento : EventArgs
    {
      // Create an instance of  DevExpress.Xpf.Printing.CreateAreaEventArgs
      // and expose any properties, methods as necessary.
      // Also supply any conversion constructors or methods
      // internally to this class, use fully qualified name to reference CreateAreasEventArgs.
    }

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center