[Mono-bugs] [Bug 479763] New: Delegate.CreateDelegate throws ArgumentException under unusual circumstances

bugzilla_noreply at novell.com bugzilla_noreply at novell.com
Wed Feb 25 21:49:53 EST 2009


https://bugzilla.novell.com/show_bug.cgi?id=479763


           Summary: Delegate.CreateDelegate throws ArgumentException under
                    unusual circumstances
    Classification: Mono
           Product: Mono: Class Libraries
           Version: SVN
          Platform: Macintosh
        OS/Version: Mac OS X 10.5
            Status: NEW
          Severity: Normal
          Priority: P5 - None
         Component: System
        AssignedTo: mono-bugs at lists.ximian.com
        ReportedBy: dmitchell at logos.com
         QAContact: mono-bugs at lists.ximian.com
          Found By: ---


User-Agent:       Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_6; en-us)
AppleWebKit/525.27.1 (KHTML, like Gecko) Version/3.2.1 Safari/525.27.1

Here's some code:

---
using System;

public class Foo
{
    public event EventHandler Event;

    public void RaiseEvent()
    {
        Event(this, new EventArgs());
    }

    public void AddHandler<T>(string target)
    {
        Action<object, EventArgs> fn = (sender, e) =>
Console.WriteLine(target);
        EventHandler handler = Delegate.CreateDelegate(typeof(EventHandler),
fn.Target, fn.Method) as EventHandler;

        Event += handler;
    }
}

public static class Program
{
    public static void Main()
    {
        var thing = new Foo();

        thing.AddHandler<Type>("hello");
        thing.RaiseEvent();
        thing.AddHandler<Type>("there");
    }
}
---

When compiled and run under Mono, this code causes an exception to be thrown
the second time that CreateDelegate is called. The same assembly throws no
exception when run under Microsoft's .NET implementation.

Reproducible: Always

Steps to Reproduce:
1. Build the included code
2. Run it
3.
Actual Results:  
the second call to Delegate.CreateDelegate() throws an ArgumentException under
circumstances virtually identical to those in the first call

Expected Results:  
Delegate.CreateDelegate() should not throw an ArgumentException under these
circumstances.

Many things, when changed, will allow the code to function properly:

1) The generic parameter may be removed from AddHandler
2) A value type (or object) may be specified for AddHandler's generic parameter
3) The call to thing.RaiseEvent() may be omitted
4) fn may be defined in such a way as to not reference target

Also of note is that the second call to AddHandler need not be made against
thing--any instance of Foo will do, as long as both AddHandler and RaiseEvent
have been called on some instance of Foo.

This particular bit of code also fails on 2.2; some lengthier bits of code that
I generated while tracking this down would fail on the svn head, but not in
2.2.

-- 
Configure bugmail: https://bugzilla.novell.com/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the QA contact for the bug.
You are the assignee for the bug.


More information about the mono-bugs mailing list