开发者

Theoretical: Delegates passed to constructor

So I was wondering if something like this is possible:

I am a program, I am about to create an instance of one of my objects. I fire an event letting any subscribers know that I am about to create an object. As a part of this event I pass through a Hashtable that I will later pass to the constructor of my object.

Subscribers would then be able to modify this Hashtable yes? They could add entries for a delegate type with a delegate of that type as the value? Once the subscriber finishes doing their business, the pre-create event finishes. Would the changes made to the Hashtable be represented in the object that fired the event? So then if this Hashtable was passed to the constructor of an object, that object could potentially contain methods from an outside source?

So essentiall开发者_如何学Cy -

Go to create object

Fire request delegate event

Use delegate hashtable modified by event in constructor of object

Object uses methods from hashtable in place of own if exists delegate of same type

Now object runs custom code on standard functions

Again, I really don't know if what I'm saying is even possible, or makes sense.


It's possible, but I would recommend against sending a HashSet<T> openly. The reason being that any subscriber could modify the values in the HashSet<T>.

Instead, I don't know that an event is the right course of action here. I would still use delegates, but I would not expose them as events. A pub/sub pattern might be better here (where an object that wants to be notified would pass the delegate through Sub/Unsub methods).

This allows you to modify the delegate signature so that the implementation will return the data values to you when you make the call (instead of passing around the HashSet<T>).

Then, instead of just making one call, you would call GetInvocationList to get each individual delegate. You would then loop through and collect the values that are returned.

From that point, you would aggregate the values appropriately and then pass that aggregate to the constructor of your objects.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜