Raising event with struct parameter fails

During some tinkering around with delegates and events I was confronted with a System.Exception.
The exception seems to occur when an event with a struct object as one of the parameters is raised.
The event has to have at least two subscriptions. The error is reproducable with the following code:


class Foo
{
    public delegate void ClassEventHandler(MyClass o);
    public event ClassEventHandler ClassEvent;

    public delegate void StructEventHandler(MyStruct o);
    public event StructEventHandler StructEvent;

    public void RaiseEvents()
    {
        ClassEvent(new MyClass());
        StructEvent(new MyStruct());
    }

    public class MyClass { }
    public struct MyStruct { }
}

void ProgramStarted()
{
    var foo = new Foo();

    Debug.Print("Raising events with one subscription to each event...");
    foo.ClassEvent += s => Debug.Print("ClassEvent (1st subscription)");
    foo.StructEvent += s => Debug.Print("StructEvent (1st subscription)");
    foo.RaiseEvents(); // Works without problems

    Debug.Print("Raising events with two subscriptions to each event...");
    foo.ClassEvent += s => Debug.Print("ClassEvent (2nd subscription)");
    foo.StructEvent += s => Debug.Print("StructEvent (2nd subscription)");
    foo.RaiseEvents(); // Works for ClassEvent only, exception when raising StructEvent
}

The output is:

[quote]
Raising events with one subscription to each event…
ClassEvent (1st subscription)
StructEvent (1st subscription)
Raising events with two subscriptions to each event…
ClassEvent (1st subscription)
ClassEvent (2nd subscription)
#### Exception System.Exception - CLR_E_WRONG_TYPE (1) ####
#### Message:
#### GadgeteerApp1.Program::b__1 [IP: 0000] ####
#### GadgeteerApp1.Program+Foo+StructEventHandler::Invoke [IP: 20db6636] ####
#### GadgeteerApp1.Program+Foo::RaiseEvents [IP: 001a] ####
#### GadgeteerApp1.Program::ProgramStarted [IP: 0084] ####
#### GadgeteerApp1.Program::Main [IP: 0015] ####[/quote]

I assured the problem only occurs in NETMF, not in .NET (Standard) Framework.
Seems to be a bug to me but I’m not sure… Any ideas?

(First post here btw. :D)

perhaps the cause of the problem is a struct with no content?

Nope. I just reduced the code to the bare minimum. In my project I’m using a struct filled with properties and I even tried it with the Gadgeteer.Color struct. Same error.

Interesting. You will get the same result when running this test on emulator. The main difference between class and struct is that class is a reference type and struct is a value type. I don’t know however if this has anything to do with this effect.

What is that ‘s’ standing for in your lambda expressions?

EDIT: I see it is your class/struct argument. I was used to do it like this (s) =>, but seems like s => would also work.

Welcome to the forum!

@ WouterH - You don’t have to use brackets if you have only one argument. Also you don’t have to use brackets in your anonymous method body if you have one instruction. But this is just a side note. I think this should be reported as an issue one codeplex don’t you think ?

Sure, it should be reported. Just can’t think of what could go wrong under the hood that would cause this difference…

Go into the source code and find it :wink: It took me two hours to spot a bug in net mf source code regarding the File.Exists method not working correctly. the problem here seems more complicated so i don’t think I’m able to spot the problem…

I’m not in a position to test this now but I wonder if this comment from Delegate.CreateDelegate Method (System) | Microsoft Learn is related to this problem…

[quote]
Important

If method is static (Shared in Visual Basic) and its first parameter is of type Object or ValueType, then firstArgument can be a value type. In this case firstArgument is automatically boxed. Automatic boxing does not occur for any other arguments, as it would in a C# or Visual Basic function call. [/quote]
Maybe try making things static in the test program and see what happens?

Or I could be totally off in left field - certainly wouldn’t be the first time…

edit: no… tried messing with this in the emulator and couldn’t make anything work… never mind.

Sorry for the late answer but I was very busy yesterday.
I reported the issue on Codeplex here: http://netmf.codeplex.com/workitem/1668

[quote=“ddurant”]no… tried messing with this in the emulator and couldn’t make anything work… never mind.
[/quote]
Thanks ddurant for testing, I came to the same result.

Challenge accepted :smiley: I might have a look at the source code later.