Wednesday 15 January 2014

c# - How do I work with explicit interface events? -



c# - How do I work with explicit interface events? -

so made few interfaces this:

public interface idrawactions : isimpledrawable { action<gametime> predrawaction { get; set; } action<gametime> postdrawaction { get; set; } event eventhandler predrawactionchanged; event eventhandler postdrawactionchanged; }

any classes implement (or several) of these interfaces got kinda cluttered, thought create sense utilize explicit interface implementation hide uncommon events , properties away. but, doing so, got compiler error:

an explicit interface implementation of event must utilize event accessor syntax

and googling lead me this rather helpful blog post:

this hints @ 1 of primary reasons writing own add together , remove accessors: provide own underlying info store. 1 reason might want if have lots of exposed events on class, in such way few typically in utilize on instance @ point in time. in such scenario, there can important memory overhead associated maintaining delegate field each event.

how save on resources? seems delegate invocation list event null, how , when instantiated if you're using own custom handlers? hidden away!

the text in bold refers memory optimization extremely useful when have many events, or many object instances, many events. basic back upwards creating events in c# utilize event keyword. keyword syntactic sugar next generated code:

a field contain delegate. delegates form linked lists. head of list, , additions inserted @ head. event accessors, add together method inserts linked list using delegate field, , remove method removes linked list. linked list add-on , removal has syntactic sugar hiding it, see "+=" , "-=" add together to, or remove from, delegate list.

in sense, event keyword produces code similar generated code c# auto-implemented properties.

the overhead comes in maintaining separate field each event. not necessary, not necessary maintain separate field info backing each property exposed class. can virtualize both event fields , property fields.

how eliminate overhead events specifically? utilize method in libraries such vg.net, , microsoft uses similar methods in code: maintain collection of events in single field. in cases, few instances have many event subscribers. simplest collection linked list of class instances. each element in collection consists of class instance containing next properties:

an event identifier. there 1 unique identifier per unique type of event. best utilize small, byte or integer, since unlikely have millions of event types, across huge library. a delegate. delegate can weakly typed (delegate).

when need add together event handler subscriber, delegate in collection, using unique event type identifier. first time up, collection not contain it. in case of event handler additions, add together element collection, , within element, add together delegate stored there, using delegate.combine. remove handler, utilize delegate.remove.

here illustration real code in vg.net:

private static readonly int mousedownevent = eventsproperty.createeventkey(); public event elementmouseeventhandler mousedown { add together { addhandler(mousedownevent, value); } remove { removehandler(mousedownevent, value); } } public virtual void onmousedown(elementmouseeventargs args) { elementmouseeventhandler handler = findhandler(mousedownevent) elementmouseeventhandler; if (handler != null) handler(this, args); } internal void addhandler(int key, delegate value) { eventsproperty p = (eventsproperty)getorinsertproperty(eventsproperty.key); p.addhandler(key, value); } internal void removehandler(int key, delegate value) { eventsproperty p = (eventsproperty)getproperty(eventsproperty.key); if (p == null) return; p.removehandler(key, value); } internal delegate findhandler(int key) { eventsproperty p = (eventsproperty)getproperty(eventsproperty.key); if (p == null) homecoming null; homecoming p[key]; }

we virtualized not events, properties well. vg.net, events contained in 1 virtual property (eventproperty), , public properties virtualized, although bundle property values used together. enables provide many properties , events on instances, while there 0 memory used these properties or events per instance, unless:

for properties, property set non-default value. for events, subscribes event.

these types of optimization create vg.net efficient when there millions of graphical objects in memory, if running on low-end hardware.

ideally, should have programming tools not forcefulness optimize info structures explicitly. specifying how objects laid out in memory burden improve handled profiler or smart run-time system. still in stone age in respect, in every programming language have ever worked in.

c# events interface delegates explicit-interface

No comments:

Post a Comment