Is that the only way? That sample didn't really help, as it required
knowledge about the "ListChangedEventHandler" that derived Delegate. The
module I'm working with doesn't have this kind of knowledge. All it knows is
System.Delegate, so if I fetch the InvocationList, what I"m left with is
still a Delegate, which doesn't have the Invoke method.
Maybe if I explain what I'm trying to do, maybe you can answer that...
Let me start by explaining why I'm writing this module...
In the full .net framework, (and I suspect the cf.net as well, because of
the _target member), when you register for an event, the event handler you
are passing contains a strong reference back to your object, such that your
object will never be collected by the garbage collector, until you unregister
the event callback.
In many instances, this undesirable, because you want to receive events for
the duration of your object, but you do not wish the events themselves to
determine the lifetime of your object. In an ideal world, you would have a
method on your object indicating when you are done doing what it is you're
doing, so you can unregister your events. However, that is akin to reference
counting, and defeats the purpose of having a garbage collector.
So I wrote a "WeakEvent" class, that sits in the "add" and "remove" portion
of your regular event type declaration. It basically grabs the delegate, and
stores a weak reference to your object, and grabs the necessary information,
so it can invoke you when the event fires. In the case of regular .net, it
used the methodinfo property.
In the full .net framework, this has worked very well for the past several
years. However, when I was trying to run it on cf.net, it completely broke. I
managed to obtain the object reference, but there is no way to get a
methodinfo anymore from the delegate.
So I was going to rely on DynamicallyInvoking the delegate. Actually, I was
going to hack it (I saw lots of precedence for hacking in cf.net), because if
I needed to now keep the delgate around, I couldn't let it keep a strong
reference back to the object, so I forcefully set the _target private member
to null, and was going to set it back to the object, via my weak reference to
that object, when it was time to invoke.
So in any case, this class doesn't really know what the 'actual' delegate
type is, because it's a "generic" class, that I used with lots of different
eventhandler types. The closest I can get, is obtaining the type, at
registration time, but I don't think that would help me, because I still
would need to typecast the delegate to this type, to gain access to the
invoke method, but I don't think you can typecast without using the type name
as a literal, which I still wouldn't have.
Alex Feinman said:
Take a look at
http://www.opennetcf.org/PermaLink.aspx?guid=4cf118ee-5668-452e-af2c-f3a0c6d43a7f
--
Alex Feinman
---
Visit
http://www.opennetcf.org
Star_Screamer said:
I have some code, that relies on DynamicInvoke of a delegate. I see that
this
isn't supported on CF.NET.
I found online that:
Brian Smith [MSFT] (VIP)
A generic mechanism that should work is:
del.GetType().GetMethod("Invoke").Invoke(del, params));
However, when I do this, I get an "InvalidProgramException". Is there a
way
around this? I can't typecast to the derived delegate and call directly,
because this particular module is not aware of them.
thanks,