WeakEvent Type parameter

Oct 6, 2012 at 2:41 PM


when I  try to instantiate a WeakEvent<EventHandler<EventArgs>>, I get this exception at runtime:

System.ArgumentException: The second delegate parameter must be derived from type 'EventArgs'. Type is System.EventArgs

Is this really intended? If I try WeakEvent<EventHandler<MyOwnEventArgs>> it works well.


Oct 12, 2012 at 7:39 AM

I took that WeakEvent class from Daniel Grunwald atricle on codeproject, you may have to ask him about this : http://www.codeproject.com/Articles/29922/Weak-Events-in-C

Oct 12, 2012 at 5:32 PM

I had a look at Daniels FastSmartWeakEvent.cs and found that the one line, that checks the parameter, is different from the one in Cinch. After changing the WeakEvent constructor to:

// Cinch V2 (68724)
//if (!argsParameter.ParameterType.IsSubclassOf(typeof(EventArgs)))

// From http://www.codeproject.com/script/Articles/ViewDownloads.aspx?aid=29922
// (FastSmartWeakEvent.cs) Allows type of exactly System.EventArgs
if (!(typeof(EventArgs).IsAssignableFrom(argsParameter.ParameterType)))
    throw new ArgumentException("The second delegate parameter must be derived from type 'EventArgs'. Type is " + argsParameter.ParameterType);

the WeakEvent<EventHandler<EventArgs>> works well. I guess this is the intended way (why should EventArgs be avoided?).



Oct 17, 2012 at 12:02 PM

I am not sure what you are saying here, are you saying you got code from Daniels article that fixed your issue, as it allowed you to use EventArgs?

I never changed that code, so I guess Daniel must have changed him mind at some stage. Does your new line of code also work with Subclasses of EventArgs?

I have never had an issue with it to be honest. Perhaps if you could just clarify I could update the codebase.

Oct 17, 2012 at 5:51 PM

Yes, I found it in Daniels article: http://www.codeproject.com/script/Articles/ViewDownloads.aspx?aid=29922.

Yes, it fixes the issue that only classes derived of EventArgs could be used. With the changed line of code the EventArgs class itself can be used, too.

Yes, classes derived of EventArgs still work well for me.

Maybe nobody has tried to use EventArgs directly ;-)

Oct 17, 2012 at 6:32 PM

Ah cool Ill try and get that fixed in Cinch. Probably as you say no has tried EventArgs. Ill fix it

Dec 20, 2012 at 12:32 PM

Sorry its taken me so long to do this, but I finally fixed that line of code in there