Note that there are some explanatory texts on larger screens.

plurals
  1. POEmulate IDispatchEx in C#
    text
    copied!<p>C# 3.0 Extension methods add extensions to the base Type making calling that method on all <em>instances</em> of that Type legal.</p> <p>Now, JavaScript I know implements IDispatchEx through which it's possible to add methods to a specific instance.</p> <p>So how do I add a set of methods to an 'instance' of a C# class? I know this is a Dynamic vs. Static Languages holy war territory. :) Let me clarify my intention is NOT that.</p> <p>I just want to be able to add a set of events to an interface depending on the class implementing that interface.</p> <p>I was able to do that using Generics </p> <pre><code>inteface ISample&lt;T&gt; { T SupportedEvents; } class Sample : ISample&lt;UIWidgetEvent&gt; { } class Sample2 : ISample&lt;NonVisualUIWidget&gt; { } class UIWidgetEvent { public EventHandler Clicked; } class NonVisualUIWidget {public EventHandler Expired;} class TestSample { public void Test() { new Sample().SupportedEvents.Clicked += ... new Sample2().SupportedEvents.Expired += ... } } </code></pre> <p>Then I didn't like <code>SupportedEvents</code> I want to be able to say </p> <pre><code>new Sample().Clicked +=... </code></pre> <p>Then I thought JavaScript (I know <code>C#</code> is not <code>JS</code> :))... AND <code>IDispatchEx</code>, <code>IL Weaving</code>, <code>Reflection.Emit</code> etc. etc. and thought may be there's a way to do this... [Design time support would be nice but I can live without]</p> <p>Yes, I probably could do this "instance augmentation" with a Visitor pattern. [Not sure if I could get the syntatic sugar though]</p> <p>Comments?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload