<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>marklio - Delegates</title>
    <link>http://www.marklio.com/marklio/</link>
    <description>better than the original</description>
    <image>
      <url>http://photos1.flickr.com/buddyicons/12488026@N00.jpg?1105565509</url>
      <title>marklio - Delegates</title>
      <link>http://www.marklio.com/marklio/</link>
    </image>
    <language>en-us</language>
    <copyright>Mark Miller</copyright>
    <lastBuildDate>Mon, 25 Feb 2008 19:12:15 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.3.9074.18820</generator>
    <managingEditor>mark@marklio.com</managingEditor>
    <webMaster>mark@marklio.com</webMaster>
    <item>
      <trackback:ping>http://www.marklio.com/marklio/Trackback.aspx?guid=9491e8e3-6bbc-4c3e-a106-7944495db527</trackback:ping>
      <pingback:server>http://www.marklio.com/marklio/pingback.aspx</pingback:server>
      <pingback:target>http://www.marklio.com/marklio/PermaLink,guid,9491e8e3-6bbc-4c3e-a106-7944495db527.aspx</pingback:target>
      <dc:creator>Mark</dc:creator>
      <wfw:comment>http://www.marklio.com/marklio/CommentView,guid,9491e8e3-6bbc-4c3e-a106-7944495db527.aspx</wfw:comment>
      <wfw:commentRss>http://www.marklio.com/marklio/SyndicationService.asmx/GetEntryCommentsRss?guid=9491e8e3-6bbc-4c3e-a106-7944495db527</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Some of my posts are really reactions to search queries that have previously landed
on my blog.  If they did a search that got to my blog, but I know they didn't
find what they were looking for, chances are they (or someone else) will do the same
again.  And, if I <strong><em>HAVE</em></strong> the information they are looking
for, it makes sense to just add the information, even if it's what I would consider
well-known or common sense information. (common sense for software developers, that
is)
</p>
        <p>
One general search query I see again and again is something like "What is Action&lt;T&gt;
for?" or "What is Func&lt;T&gt;?"
</p>
        <p>
These are framework-provided, generic delegate types.  If you'll recall, delegates
can be thought of as type-safe function pointers.  A delegate type really just
captures a signature as "callable" object.  Leveraging generics to define delegate
types that can capture common signatures is goodness, since they are very flexible
and can be used by anyone.  This also aids in interop between different components,
since a general signature is far more interopable than custom delegate types.
</p>
        <p>
In v2.0, several functional-looking APIs were added that took delegates as arguments
(think List&lt;T&gt;), so instead of adding a special delegate type for each API,
several "generic" delegates were added to capture the "essence" of a signature such
as Action&lt;T&gt; which takes T and does some action (returning void), Predicate&lt;T&gt;
which takes T and returns bool (presumably doing some test against T), Comparer&lt;T&gt;
which compares 2 T's, etc.
</p>
        <p>
In v3.5, even more generalized functional patterns were introduced (used heavily in
Linq).  And we added a bunch more Action&lt;&gt; "overloads" for functions returning
void, and added Func&lt;&gt; "overloads" for functions with a return value. 
(I use overload loosely since these are classes and not methods) These patterns dropped
the semantic "meaning" of the delegate, and just went straight to the idea of capturing
a signature.
</p>
        <p>
These framework-provided delegates are useful for using in your own code rather than
creating your own.  Whether you leverage the Linq-centric, super-generic Action/Func
pattern, or opt to consume the more meaningful v2.0 Predicate, Comparer, etc. is up
to you.
</p>
        <img width="0" height="0" src="http://www.marklio.com/marklio/aggbug.ashx?id=9491e8e3-6bbc-4c3e-a106-7944495db527" />
      </body>
      <title>What are the generic Delegates in the framework for?</title>
      <guid isPermaLink="false">http://www.marklio.com/marklio/PermaLink,guid,9491e8e3-6bbc-4c3e-a106-7944495db527.aspx</guid>
      <link>http://www.marklio.com/marklio/PermaLink,guid,9491e8e3-6bbc-4c3e-a106-7944495db527.aspx</link>
      <pubDate>Mon, 25 Feb 2008 19:12:15 GMT</pubDate>
      <description>&lt;p&gt;
Some of my posts are really reactions to search queries that have previously landed
on my blog.&amp;nbsp; If they did a search that got to my blog, but I know they didn't
find what they were looking for, chances are they (or someone else) will do the same
again.&amp;nbsp; And, if I &lt;strong&gt;&lt;em&gt;HAVE&lt;/em&gt;&lt;/strong&gt; the information they are looking
for, it makes sense to just add the information, even if it's what I would consider
well-known or common sense information. (common sense for software developers, that
is)
&lt;/p&gt;
&lt;p&gt;
One general search query I see again and again is something like "What is Action&amp;lt;T&amp;gt;
for?" or "What is Func&amp;lt;T&amp;gt;?"
&lt;/p&gt;
&lt;p&gt;
These are framework-provided, generic delegate types.&amp;nbsp; If you'll recall, delegates
can be thought of as type-safe function pointers.&amp;nbsp; A delegate type really just
captures a signature as "callable" object.&amp;nbsp; Leveraging generics to define delegate
types that can capture common signatures is goodness, since they are very flexible
and can be used by anyone.&amp;nbsp; This also aids in interop between different components,
since a general signature is far more interopable than custom delegate types.
&lt;/p&gt;
&lt;p&gt;
In v2.0, several functional-looking APIs were added that took delegates as arguments
(think List&amp;lt;T&amp;gt;), so instead of adding a special delegate type for each API,
several "generic" delegates were added to capture the "essence" of a signature such
as Action&amp;lt;T&amp;gt; which takes T and does some action (returning void), Predicate&amp;lt;T&amp;gt;
which takes T and returns bool (presumably doing some test against T), Comparer&amp;lt;T&amp;gt;
which compares 2 T's, etc.
&lt;/p&gt;
&lt;p&gt;
In v3.5, even more generalized functional patterns were introduced (used heavily in
Linq).&amp;nbsp; And we added a bunch more Action&amp;lt;&amp;gt; "overloads" for functions returning
void, and added Func&amp;lt;&amp;gt; "overloads" for functions with a return value.&amp;nbsp;
(I use overload loosely since these are classes and not methods) These patterns dropped
the semantic "meaning" of the delegate, and just went straight to the idea of capturing
a signature.
&lt;/p&gt;
&lt;p&gt;
These framework-provided delegates are useful for using in your own code rather than
creating your own.&amp;nbsp; Whether you leverage the Linq-centric, super-generic Action/Func
pattern, or opt to consume the more meaningful v2.0 Predicate, Comparer, etc. is up
to you.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.marklio.com/marklio/aggbug.ashx?id=9491e8e3-6bbc-4c3e-a106-7944495db527" /&gt;</description>
      <comments>http://www.marklio.com/marklio/CommentView,guid,9491e8e3-6bbc-4c3e-a106-7944495db527.aspx</comments>
      <category>CLR</category>
      <category>Delegates</category>
      <category>Fun</category>
      <category>Software Development</category>
    </item>
    <item>
      <trackback:ping>http://www.marklio.com/marklio/Trackback.aspx?guid=b12bddf4-a3a8-47d9-8e03-82e54a8f7c4c</trackback:ping>
      <pingback:server>http://www.marklio.com/marklio/pingback.aspx</pingback:server>
      <pingback:target>http://www.marklio.com/marklio/PermaLink,guid,b12bddf4-a3a8-47d9-8e03-82e54a8f7c4c.aspx</pingback:target>
      <dc:creator>Mark</dc:creator>
      <wfw:comment>http://www.marklio.com/marklio/CommentView,guid,b12bddf4-a3a8-47d9-8e03-82e54a8f7c4c.aspx</wfw:comment>
      <wfw:commentRss>http://www.marklio.com/marklio/SyndicationService.asmx/GetEntryCommentsRss?guid=b12bddf4-a3a8-47d9-8e03-82e54a8f7c4c</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
An interesting pattern I've seen emerge since the early releases of Orcas is what
I might refer to as "delegate properties".  What I mean by that is a property
(or field, I suppose) that returns a delegate.  This pattern has some interesting
implications.
</p>
        <p>
First, in a language that treats delegates as directly callable objects, this pattern
looks just like a method (<font face="Courier New">someInstance.TheProperty(args)</font>). 
You can't tell the difference (although VS gives you different intellisense) by looking
at a callsite like this. Among other things, this leads to some interesting naming
issues.  Do you name it like you would a method?
</p>
        <p>
Second, it opens up opportunities to do some really powerful (and slightly insane) hybrid
inheritance models.  Think about a virtual delegate property that has both a
getter and a setter, now think about trying to predict what that delegate will
do when you call it.  It doesn't sound like something to recommend as part of
a public API, but I think there are some interesting scenarios there.
</p>
        <p>
If I come up with something interesting and useful, I'll let you know.
</p>
        <img width="0" height="0" src="http://www.marklio.com/marklio/aggbug.ashx?id=b12bddf4-a3a8-47d9-8e03-82e54a8f7c4c" />
      </body>
      <title>Delegate Properties</title>
      <guid isPermaLink="false">http://www.marklio.com/marklio/PermaLink,guid,b12bddf4-a3a8-47d9-8e03-82e54a8f7c4c.aspx</guid>
      <link>http://www.marklio.com/marklio/PermaLink,guid,b12bddf4-a3a8-47d9-8e03-82e54a8f7c4c.aspx</link>
      <pubDate>Thu, 26 Jul 2007 20:56:27 GMT</pubDate>
      <description>&lt;p&gt;
An interesting pattern I've seen emerge since the early releases of Orcas is what
I might refer to as "delegate properties".&amp;nbsp; What I mean by that is a property
(or field, I suppose)&amp;nbsp;that returns a delegate.&amp;nbsp; This pattern has some interesting
implications.
&lt;/p&gt;
&lt;p&gt;
First, in a language that treats delegates as directly callable objects, this pattern
looks just like a method (&lt;font face="Courier New"&gt;someInstance.TheProperty(args)&lt;/font&gt;).&amp;nbsp;
You can't tell the difference (although VS gives you different intellisense) by looking
at a callsite like this.&amp;nbsp;Among other things, this leads to some interesting naming
issues.&amp;nbsp; Do you name it like you would a method?
&lt;/p&gt;
&lt;p&gt;
Second, it opens up opportunities to do some really powerful (and slightly insane)&amp;nbsp;hybrid
inheritance models.&amp;nbsp; Think about a virtual delegate property that has both a
getter and a setter,&amp;nbsp;now think about trying to predict what that delegate will
do when you call it.&amp;nbsp; It doesn't sound like something to recommend as part of
a public API, but I think there are some interesting scenarios there.
&lt;/p&gt;
&lt;p&gt;
If I come up with something interesting and useful, I'll let you know.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.marklio.com/marklio/aggbug.ashx?id=b12bddf4-a3a8-47d9-8e03-82e54a8f7c4c" /&gt;</description>
      <comments>http://www.marklio.com/marklio/CommentView,guid,b12bddf4-a3a8-47d9-8e03-82e54a8f7c4c.aspx</comments>
      <category>CLR</category>
      <category>Delegates</category>
      <category>Fun</category>
      <category>Software Development</category>
      <category>Technical</category>
    </item>
    <item>
      <trackback:ping>http://www.marklio.com/marklio/Trackback.aspx?guid=25ff5a1e-4369-4872-a992-bd271700d862</trackback:ping>
      <pingback:server>http://www.marklio.com/marklio/pingback.aspx</pingback:server>
      <pingback:target>http://www.marklio.com/marklio/PermaLink,guid,25ff5a1e-4369-4872-a992-bd271700d862.aspx</pingback:target>
      <dc:creator>Mark</dc:creator>
      <wfw:comment>http://www.marklio.com/marklio/CommentView,guid,25ff5a1e-4369-4872-a992-bd271700d862.aspx</wfw:comment>
      <wfw:commentRss>http://www.marklio.com/marklio/SyndicationService.asmx/GetEntryCommentsRss?guid=25ff5a1e-4369-4872-a992-bd271700d862</wfw:commentRss>
      <slash:comments>2</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
If you have been following my series on delegates, you may have experimented with
open-instance delegates and perhaps found it difficult to create an open-instance
delegate for a value type.
</p>
        <p>
If you'll recall, an open-instance delegate has an extra first parameter, used to
pass the instance used for the invocation.  What's not made explicitly clear
is that this first parameter must be passed by reference.
</p>
        <p>
For reference types, you've automatically got a reference, but for value types, this
must be a "ref" parameter.  For instance, a delegate type used as an open-instance
delegate for Int32.CompareTo would have to be defined something like:
</p>
        <blockquote>
          <p>
            <font face="Courier New">delegate int IntCompareToDelegate(<font color="#0000ff">ref</font> int
instance, int other);</font>
          </p>
        </blockquote>
        <p>
Otherwise, you'll get a System.ArgumentException when you try to bind the method to
the delegate, giving you the ever-helpful error message: "Error binding to target
method".
</p>
        <p>
There are lots of underlying reasons for this, both from a calling convention perspective,
as well as a side-effect perspective.  But, you can simplify it by thinking about
modifications to the instance.  If you passed by value (creating a copy that
the method acted on), any changes made to the instance by the method would be lost
because they happened to a copy.
</p>
        <p>
In most cases, value types are immutable in the framework, but you could run into
issues with your own types.  And, again, this isn't the only reason for this
restriction (take a look at the IL generated for a value-type method call to get some
more ideas).  It's just the easiest to understand.
</p>
        <p>
If you'll recall, Orcas extension methods, which are similar in concept to this, <a href="http://www.marklio.com/marklio/ct.ashx?id=25ff5a1e-4369-4872-a992-bd271700d862&amp;url=http%3a%2f%2fwww.marklio.com%2fmarklio%2fPermaLink%2cguid%2cf246bcec-5a5c-4869-b9c1-671d8f0daa4c.aspx" target="_blank">do
not follow this pattern</a> and are subject to the infamous value type copying problems.
</p>
        <img width="0" height="0" src="http://www.marklio.com/marklio/aggbug.ashx?id=25ff5a1e-4369-4872-a992-bd271700d862" />
      </body>
      <title>Open-instance delegates for value types?</title>
      <guid isPermaLink="false">http://www.marklio.com/marklio/PermaLink,guid,25ff5a1e-4369-4872-a992-bd271700d862.aspx</guid>
      <link>http://www.marklio.com/marklio/PermaLink,guid,25ff5a1e-4369-4872-a992-bd271700d862.aspx</link>
      <pubDate>Mon, 16 Jul 2007 19:24:58 GMT</pubDate>
      <description>&lt;p&gt;
If you have been following my series on delegates, you may have experimented with
open-instance delegates and perhaps found it difficult to create an open-instance
delegate for a value type.
&lt;/p&gt;
&lt;p&gt;
If you'll recall, an open-instance delegate has an extra first parameter, used to
pass the instance used for the invocation.&amp;nbsp; What's not made explicitly clear
is that this first parameter must be passed by reference.
&lt;/p&gt;
&lt;p&gt;
For reference types, you've automatically got a reference, but for value types, this
must be a "ref" parameter.&amp;nbsp; For instance, a delegate type used as an open-instance
delegate for Int32.CompareTo would have to be defined something like:
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
&lt;font face="Courier New"&gt;delegate int IntCompareToDelegate(&lt;font color=#0000ff&gt;ref&lt;/font&gt; int
instance, int other);&lt;/font&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
Otherwise, you'll get a System.ArgumentException when you try to bind the method to
the delegate, giving you the ever-helpful error message: "Error binding to target
method".
&lt;/p&gt;
&lt;p&gt;
There are lots of underlying reasons for this, both from a calling convention perspective,
as well as a side-effect perspective.&amp;nbsp; But, you can simplify it by thinking about
modifications to the instance.&amp;nbsp; If you passed by value (creating a copy that
the method acted on), any changes made to the instance by the method would be lost
because they happened to a copy.
&lt;/p&gt;
&lt;p&gt;
In most cases, value types are immutable in the framework, but you could run into
issues with your own types.&amp;nbsp; And, again, this isn't the only reason for this
restriction (take a look at the IL generated for a value-type method call to get some
more ideas).&amp;nbsp; It's just the easiest to understand.
&lt;/p&gt;
&lt;p&gt;
If you'll recall, Orcas extension methods, which are similar in concept to this,&amp;nbsp;&lt;a href="http://www.marklio.com/marklio/ct.ashx?id=25ff5a1e-4369-4872-a992-bd271700d862&amp;amp;url=http%3a%2f%2fwww.marklio.com%2fmarklio%2fPermaLink%2cguid%2cf246bcec-5a5c-4869-b9c1-671d8f0daa4c.aspx" target=_blank&gt;do
not follow this pattern&lt;/a&gt; and are subject to the infamous value type copying problems.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.marklio.com/marklio/aggbug.ashx?id=25ff5a1e-4369-4872-a992-bd271700d862" /&gt;</description>
      <comments>http://www.marklio.com/marklio/CommentView,guid,25ff5a1e-4369-4872-a992-bd271700d862.aspx</comments>
      <category>CLR</category>
      <category>Fun</category>
      <category>Technical</category>
      <category>Delegates</category>
    </item>
    <item>
      <trackback:ping>http://www.marklio.com/marklio/Trackback.aspx?guid=6e5f5bbf-bad3-44b8-a4ae-208c2f39be6d</trackback:ping>
      <pingback:server>http://www.marklio.com/marklio/pingback.aspx</pingback:server>
      <pingback:target>http://www.marklio.com/marklio/PermaLink,guid,6e5f5bbf-bad3-44b8-a4ae-208c2f39be6d.aspx</pingback:target>
      <dc:creator>Mark</dc:creator>
      <wfw:comment>http://www.marklio.com/marklio/CommentView,guid,6e5f5bbf-bad3-44b8-a4ae-208c2f39be6d.aspx</wfw:comment>
      <wfw:commentRss>http://www.marklio.com/marklio/SyndicationService.asmx/GetEntryCommentsRss?guid=6e5f5bbf-bad3-44b8-a4ae-208c2f39be6d</wfw:commentRss>
      <slash:comments>2</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I was helping a friend with a problem recently.  He was taking a set of serial
web service calls and doing them in parallel to save time, and was not up-to-speed
on the best approach for that.  Once he settled on an approach, he
realized that since his web service calls were being wrapped in an abstraction layer,
he didn't have the Begin/End asynchronous call methods that are provided by the proxy
class.
</p>
        <p>
"No problem, just wrap them in a delegate".  The compiler automatically gives
you Begin/EndInvoke methods in addition to the synchronous Invoke method.  And,
you're guaranteed not to mess up the implementation because it's all provided by the
CLR!  Just one of those things you might forget if you find yourself in the same
situation.
</p>
        <img width="0" height="0" src="http://www.marklio.com/marklio/aggbug.ashx?id=6e5f5bbf-bad3-44b8-a4ae-208c2f39be6d" />
      </body>
      <title>Using delegates to get asynchronous invocation for free</title>
      <guid isPermaLink="false">http://www.marklio.com/marklio/PermaLink,guid,6e5f5bbf-bad3-44b8-a4ae-208c2f39be6d.aspx</guid>
      <link>http://www.marklio.com/marklio/PermaLink,guid,6e5f5bbf-bad3-44b8-a4ae-208c2f39be6d.aspx</link>
      <pubDate>Wed, 13 Jun 2007 23:37:55 GMT</pubDate>
      <description>&lt;p&gt;
I was helping a friend with a problem recently.&amp;nbsp; He was taking a set of serial
web service calls and doing them in parallel to save time, and was not up-to-speed
on the best approach for that.&amp;nbsp;&amp;nbsp;Once he&amp;nbsp;settled on an approach, he
realized that since his web service calls were being wrapped in an abstraction layer,
he didn't have the Begin/End asynchronous call methods that are provided by the proxy
class.
&lt;/p&gt;
&lt;p&gt;
"No problem, just wrap them in a delegate".&amp;nbsp; The compiler automatically gives
you Begin/EndInvoke methods in addition to the synchronous Invoke method.&amp;nbsp; And,
you're guaranteed not to mess up the implementation because it's all provided by the
CLR!&amp;nbsp; Just one of those things you might forget if you find yourself in the same
situation.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.marklio.com/marklio/aggbug.ashx?id=6e5f5bbf-bad3-44b8-a4ae-208c2f39be6d" /&gt;</description>
      <comments>http://www.marklio.com/marklio/CommentView,guid,6e5f5bbf-bad3-44b8-a4ae-208c2f39be6d.aspx</comments>
      <category>CLR</category>
      <category>Delegates</category>
      <category>Fun</category>
      <category>Software Development</category>
      <category>Technical</category>
    </item>
    <item>
      <trackback:ping>http://www.marklio.com/marklio/Trackback.aspx?guid=9145f690-7279-47b9-95cc-b439660402ad</trackback:ping>
      <pingback:server>http://www.marklio.com/marklio/pingback.aspx</pingback:server>
      <pingback:target>http://www.marklio.com/marklio/PermaLink,guid,9145f690-7279-47b9-95cc-b439660402ad.aspx</pingback:target>
      <dc:creator>Mark</dc:creator>
      <wfw:comment>http://www.marklio.com/marklio/CommentView,guid,9145f690-7279-47b9-95cc-b439660402ad.aspx</wfw:comment>
      <wfw:commentRss>http://www.marklio.com/marklio/SyndicationService.asmx/GetEntryCommentsRss?guid=9145f690-7279-47b9-95cc-b439660402ad</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
After my last few CLR posts, I've had a couple of private inquiries regarding the
usefulness of closed static delegates.  To bring everyone up to speed, a delegate
pointing to an instance method needs a "target" instance to operate on (we'll get
to <em>open instance</em> delegates later).  A static method, needs no such target,
so we can leverage the "space" used for the instance case to carry around another
object of interest.  We call a delegate with a provided value for this space
"closed over the first argument".
</p>
        <p>
For example, let's say we have a static method that does some operation on two numbers. 
For simplicity, let's just say it adds them.  Our silly class and method might
look like this:
</p>
        <blockquote>
          <pre>
            <span style="COLOR: #0000ff">public</span>
            <span style="COLOR: #0000ff">static</span>
            <span style="COLOR: #0000ff">class</span> NumberFunctions
{ <span style="COLOR: #0000ff">public</span><span style="COLOR: #0000ff">static</span><span style="COLOR: #0000ff">double</span> Add(<span style="COLOR: #0000ff">double</span> first, <span style="COLOR: #0000ff">double</span> second)
{ <span style="COLOR: #0000ff">return</span> first + second; } }</pre>
        </blockquote>
        <p>
        </p>
        <p>
Normally, a delegate for this method would look like:
</p>
        <blockquote>
          <pre>
            <span style="COLOR: #0000ff">public</span>
            <span style="COLOR: #0000ff">delegate</span>
            <span style="COLOR: #0000ff">double</span> BinaryOperation(<span style="COLOR: #0000ff">double</span> first, <span style="COLOR: #0000ff">double</span> second);</pre>
        </blockquote>
        <p>
But, we're going to create a closed static delegate, which means we're going to "burn"
the first argument into the delegate itself, so it's not needed in the delegate signature. 
Instead, we'll use the following delegate signature (I didn't spend much time thinking
up these names, I hope they make sense:
</p>
        <blockquote>
          <pre>
            <span style="COLOR: #0000ff">public</span>
            <span style="COLOR: #0000ff">delegate</span>
            <span style="COLOR: #0000ff">double</span> ClosedCall(<span style="COLOR: #0000ff">double</span> other);</pre>
        </blockquote>
        <p>
So, how do we create the delegate?  Normally, since C# (pre-Orcas) doesn't have
syntax for creating closed static delegates, you are forced to use one of the <a href="http://www.marklio.com/marklio/ct.ashx?id=9145f690-7279-47b9-95cc-b439660402ad&amp;url=http%3a%2f%2fmsdn2.microsoft.com%2fen-us%2flibrary%2fsystem.delegate.createdelegate.aspx" target="_blank">Delegate.CreateDelegate</a> overloads:
</p>
        <blockquote>
          <pre>ClosedCall addToOne = (ClosedCall)Delegate.CreateDelegate(
        <span style="COLOR: #0000ff">typeof</span>(ClosedCall),
1.0, <span style="COLOR: #0000ff">typeof</span>(NumberFunctions).GetMethod("<span style="COLOR: #8b0000">Add</span>",
BindingFlags.Public | BindingFlags.Static)); </pre>
        </blockquote>
        <p>
Of course, we just spent <a href="http://www.marklio.com/marklio/ct.ashx?id=9145f690-7279-47b9-95cc-b439660402ad&amp;url=http%3a%2f%2fwww.marklio.com%2fmarklio%2fPermaLink%2cguid%2ccce144d6-e166-45e5-a82a-55594c83e0cb.aspx" target="_blank">2</a><a href="http://www.marklio.com/marklio/ct.ashx?id=9145f690-7279-47b9-95cc-b439660402ad&amp;url=http%3a%2f%2fwww.marklio.com%2fmarklio%2fPermaLink%2cguid%2c3df80b83-9191-4495-9d9f-b6238d06f883.aspx" target="_blank">entries</a> looking
at a helper that can do this for us (I'm not claiming this is better, I just want
you to be able to see what's happening):
</p>
        <blockquote>
          <pre>ClosedCall addToOne = DelegateBinder.Bind&lt;ClosedCall&gt;(1.0,
        <span style="COLOR: #0000ff">typeof</span>(NumberFunctions).GetMethod("<span style="COLOR: #8b0000">Add</span>",
BindingFlags.Public | BindingFlags.Static));</pre>
        </blockquote>
        <p>
Now, a call to addToOne(someNumber) will yield the result of adding the supplied argument
to one.  This is a contrived example, but you could imagine taking a method (perhaps
generated on the fly via LCG), and "attaching" an instance to it via the first argument. 
Then, being able to call it many times with different subsequent arguments, or passing
it to another component that would provide the rest of the arguments.  In this
way, you get the benefits of not having to keep track of an instance, without having
to own the API for the instance.  Additionally, you could "chain" delegates together
so that many arguments are captured in a stack of delegate calls, allowing closure-type
semantics at the cost of some stack space (although since C# has closure support,
you'd never really need to do that).
</p>
        <p>
What's really cool is that with C# 3.0's <a href="http://www.marklio.com/marklio/ct.ashx?id=9145f690-7279-47b9-95cc-b439660402ad&amp;url=http%3a%2f%2fweblogs.asp.net%2fscottgu%2farchive%2f2007%2f03%2f13%2fnew-orcas-language-feature-extension-methods.aspx" target="_blank">Extension
Methods</a> feature, we now have language support for creating early-bound closed-static
delegates.  If you bind a delegate to an extension method (using the regular
syntax for an instance method), you will get the exact IL for creating an early-bound closed
static method <strong>without our fancy helper class</strong>.  Let's see how
that would look.  Let's use a different example to keep us on our toes. 
Here's a helper function that creates email addresses:
</p>
        <blockquote>
          <pre>
            <span style="COLOR: #0000ff">public</span>
            <span style="COLOR: #0000ff">static</span>
            <span style="COLOR: #0000ff">class</span> StringExtensions
{ <span style="COLOR: #0000ff">public</span><span style="COLOR: #0000ff">static</span><span style="COLOR: #0000ff">string</span> MakeEmailAddressWithAlias(<span style="COLOR: #0000ff">this</span><span style="COLOR: #0000ff">string</span> domain, <span style="COLOR: #0000ff">string</span> alias)
{ <span style="COLOR: #0000ff">return</span><span style="COLOR: #0000ff">string</span>.Format("<span style="COLOR: #8b0000">{0}@{1}</span>",
alias, domain); } }</pre>
        </blockquote>
        <p>
Notice the "this" in front of the first parameter, this tells the compiler that the
method should be considered when resolving method calls for string.  We'll use
one of the delegate types provided in Orcas. Now, here's how the bind looks:
</p>
        <blockquote>
          <pre>
            <span style="COLOR: #0000ff">string</span> fooDotCom = "<span style="COLOR: #8b0000">foo.com</span>";
Func&lt;<span style="COLOR: #0000ff">string</span>, <span style="COLOR: #0000ff">string</span>&gt;
makeFooDotComAddress = fooDotCom.MakeEmailAddressWithAlias; <span style="COLOR: #0000ff">string</span> email
= makeFooDotComAddress("<span style="COLOR: #8b0000">bar</span>");</pre>
        </blockquote>
        <p>
So, the result is that email will be <a href="mailto:bar@foo.com">bar@foo.com</a>.
</p>
        <p>
Hopefully, through these contrived examples, you can see the scenarios that closed
static methods provide, as well as learn how you can create one the easy way with
extension methods in Orcas.
</p>
        <img width="0" height="0" src="http://www.marklio.com/marklio/aggbug.ashx?id=9145f690-7279-47b9-95cc-b439660402ad" />
      </body>
      <title>What can you do with a closed static delegate? (and how to create them with Orcas)</title>
      <guid isPermaLink="false">http://www.marklio.com/marklio/PermaLink,guid,9145f690-7279-47b9-95cc-b439660402ad.aspx</guid>
      <link>http://www.marklio.com/marklio/PermaLink,guid,9145f690-7279-47b9-95cc-b439660402ad.aspx</link>
      <pubDate>Thu, 17 May 2007 17:46:22 GMT</pubDate>
      <description>&lt;p&gt;
After my last few CLR posts, I've had a couple of private inquiries regarding the
usefulness of closed static delegates.&amp;nbsp; To bring everyone up to speed, a delegate
pointing to an instance method needs a "target" instance to operate on (we'll get
to &lt;em&gt;open instance&lt;/em&gt; delegates later).&amp;nbsp; A static method, needs no such target,
so we can leverage the "space" used for the instance case to carry around another
object of interest.&amp;nbsp; We call a delegate with a provided value for this space
"closed over the first argument".
&lt;/p&gt;
&lt;p&gt;
For example, let's say we have a static method that does some operation on two numbers.&amp;nbsp;
For simplicity, let's just say it adds them.&amp;nbsp; Our silly class and method might
look like this:
&lt;/p&gt;
&lt;blockquote&gt;&lt;pre&gt;&lt;span style="COLOR: #0000ff"&gt;public&lt;/span&gt; &lt;span style="COLOR: #0000ff"&gt;static&lt;/span&gt; &lt;span style="COLOR: #0000ff"&gt;class&lt;/span&gt; NumberFunctions
{ &lt;span style="COLOR: #0000ff"&gt;public&lt;/span&gt; &lt;span style="COLOR: #0000ff"&gt;static&lt;/span&gt; &lt;span style="COLOR: #0000ff"&gt;double&lt;/span&gt; Add(&lt;span style="COLOR: #0000ff"&gt;double&lt;/span&gt; first, &lt;span style="COLOR: #0000ff"&gt;double&lt;/span&gt; second)
{ &lt;span style="COLOR: #0000ff"&gt;return&lt;/span&gt; first + second; } }&lt;/pre&gt;&lt;/blockquote&gt; 
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
Normally, a delegate for this method would look like:
&lt;/p&gt;
&lt;blockquote&gt;&lt;pre&gt;&lt;span style="COLOR: #0000ff"&gt;public&lt;/span&gt; &lt;span style="COLOR: #0000ff"&gt;delegate&lt;/span&gt; &lt;span style="COLOR: #0000ff"&gt;double&lt;/span&gt; BinaryOperation(&lt;span style="COLOR: #0000ff"&gt;double&lt;/span&gt; first, &lt;span style="COLOR: #0000ff"&gt;double&lt;/span&gt; second);&lt;/pre&gt;&lt;/blockquote&gt; 
&lt;p&gt;
But, we're going to create a closed static delegate, which means we're going to "burn"
the first argument into the delegate itself, so it's not needed in the delegate signature.&amp;nbsp;
Instead, we'll use the following delegate signature (I didn't spend much time thinking
up these names, I hope they make sense:
&lt;/p&gt;
&lt;blockquote&gt;&lt;pre&gt;&lt;span style="COLOR: #0000ff"&gt;public&lt;/span&gt; &lt;span style="COLOR: #0000ff"&gt;delegate&lt;/span&gt; &lt;span style="COLOR: #0000ff"&gt;double&lt;/span&gt; ClosedCall(&lt;span style="COLOR: #0000ff"&gt;double&lt;/span&gt; other);&lt;/pre&gt;&lt;/blockquote&gt; 
&lt;p&gt;
So, how do we create the delegate?&amp;nbsp; Normally, since C# (pre-Orcas) doesn't have
syntax&amp;nbsp;for creating closed static delegates, you are forced to use one of the &lt;a href="http://www.marklio.com/marklio/ct.ashx?id=9145f690-7279-47b9-95cc-b439660402ad&amp;amp;url=http%3a%2f%2fmsdn2.microsoft.com%2fen-us%2flibrary%2fsystem.delegate.createdelegate.aspx" target=_blank&gt;Delegate.CreateDelegate&lt;/a&gt; overloads:
&lt;/p&gt;
&lt;blockquote&gt;&lt;pre&gt;ClosedCall addToOne = (ClosedCall)Delegate.CreateDelegate(
        &lt;span style="COLOR: #0000ff"&gt;typeof&lt;/span&gt;(ClosedCall),
1.0, &lt;span style="COLOR: #0000ff"&gt;typeof&lt;/span&gt;(NumberFunctions).GetMethod("&lt;span style="COLOR: #8b0000"&gt;Add&lt;/span&gt;",
BindingFlags.Public | BindingFlags.Static)); &lt;/pre&gt;&lt;/blockquote&gt; 
&lt;p&gt;
Of course, we just spent &lt;a href="http://www.marklio.com/marklio/ct.ashx?id=9145f690-7279-47b9-95cc-b439660402ad&amp;amp;url=http%3a%2f%2fwww.marklio.com%2fmarklio%2fPermaLink%2cguid%2ccce144d6-e166-45e5-a82a-55594c83e0cb.aspx" target=_blank&gt;2&lt;/a&gt; &lt;a href="http://www.marklio.com/marklio/ct.ashx?id=9145f690-7279-47b9-95cc-b439660402ad&amp;amp;url=http%3a%2f%2fwww.marklio.com%2fmarklio%2fPermaLink%2cguid%2c3df80b83-9191-4495-9d9f-b6238d06f883.aspx" target=_blank&gt;entries&lt;/a&gt; looking
at a helper that can do this for us (I'm not claiming this is better, I just want
you to be able to see what's happening):
&lt;/p&gt;
&lt;blockquote&gt;&lt;pre&gt;ClosedCall addToOne = DelegateBinder.Bind&amp;lt;ClosedCall&amp;gt;(1.0,
        &lt;span style="COLOR: #0000ff"&gt;typeof&lt;/span&gt;(NumberFunctions).GetMethod("&lt;span style="COLOR: #8b0000"&gt;Add&lt;/span&gt;",
BindingFlags.Public | BindingFlags.Static));&lt;/pre&gt;&lt;/blockquote&gt; 
&lt;p&gt;
Now, a call to addToOne(someNumber) will yield the result of adding the supplied argument
to one.&amp;nbsp; This is a contrived example, but you could imagine taking a method (perhaps
generated on the fly via LCG), and "attaching" an instance to it via the first argument.&amp;nbsp;
Then, being able to call it many times with different subsequent arguments, or passing
it to another component that would provide the rest of the arguments.&amp;nbsp; In this
way, you get the benefits of not having to keep track of an instance, without&amp;nbsp;having
to own the API for the instance.&amp;nbsp; Additionally, you could "chain" delegates together
so that many arguments are captured in a stack of delegate calls, allowing closure-type
semantics at the cost of some stack space (although since C# has closure support,
you'd never really need to do that).
&lt;/p&gt;
&lt;p&gt;
What's really cool is that with C# 3.0's &lt;a href="http://www.marklio.com/marklio/ct.ashx?id=9145f690-7279-47b9-95cc-b439660402ad&amp;amp;url=http%3a%2f%2fweblogs.asp.net%2fscottgu%2farchive%2f2007%2f03%2f13%2fnew-orcas-language-feature-extension-methods.aspx" target=_blank&gt;Extension
Methods&lt;/a&gt;&amp;nbsp;feature, we now have language support for creating early-bound closed-static
delegates.&amp;nbsp; If you bind a delegate to an extension method (using the regular
syntax for an instance method), you will get the exact IL for creating an early-bound&amp;nbsp;closed
static method &lt;strong&gt;without our fancy helper class&lt;/strong&gt;.&amp;nbsp; Let's see how
that would look.&amp;nbsp; Let's use a different example to keep us on our toes.&amp;nbsp;
Here's a helper function that creates email addresses:
&lt;/p&gt;
&lt;blockquote&gt;&lt;pre&gt;&lt;span style="COLOR: #0000ff"&gt;public&lt;/span&gt; &lt;span style="COLOR: #0000ff"&gt;static&lt;/span&gt; &lt;span style="COLOR: #0000ff"&gt;class&lt;/span&gt; StringExtensions
{ &lt;span style="COLOR: #0000ff"&gt;public&lt;/span&gt; &lt;span style="COLOR: #0000ff"&gt;static&lt;/span&gt; &lt;span style="COLOR: #0000ff"&gt;string&lt;/span&gt; MakeEmailAddressWithAlias(&lt;span style="COLOR: #0000ff"&gt;this&lt;/span&gt; &lt;span style="COLOR: #0000ff"&gt;string&lt;/span&gt; domain, &lt;span style="COLOR: #0000ff"&gt;string&lt;/span&gt; alias)
{ &lt;span style="COLOR: #0000ff"&gt;return&lt;/span&gt; &lt;span style="COLOR: #0000ff"&gt;string&lt;/span&gt;.Format("&lt;span style="COLOR: #8b0000"&gt;{0}@{1}&lt;/span&gt;",
alias, domain); } }&lt;/pre&gt;&lt;/blockquote&gt; 
&lt;p&gt;
Notice the "this" in front of the first parameter, this tells the compiler that the
method should be considered when resolving method calls for string.&amp;nbsp; We'll use
one of the delegate types provided in Orcas. Now, here's how the bind looks:
&lt;/p&gt;
&lt;blockquote&gt;&lt;pre&gt;&lt;span style="COLOR: #0000ff"&gt;string&lt;/span&gt; fooDotCom = "&lt;span style="COLOR: #8b0000"&gt;foo.com&lt;/span&gt;";
Func&amp;lt;&lt;span style="COLOR: #0000ff"&gt;string&lt;/span&gt;, &lt;span style="COLOR: #0000ff"&gt;string&lt;/span&gt;&amp;gt;
makeFooDotComAddress = fooDotCom.MakeEmailAddressWithAlias; &lt;span style="COLOR: #0000ff"&gt;string&lt;/span&gt; email
= makeFooDotComAddress("&lt;span style="COLOR: #8b0000"&gt;bar&lt;/span&gt;");&lt;/pre&gt;&lt;/blockquote&gt; 
&lt;p&gt;
So, the result is that email will be &lt;a href="mailto:bar@foo.com"&gt;bar@foo.com&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
Hopefully, through these contrived examples, you can see the scenarios that closed
static methods provide, as well as learn how you can create one the easy way with
extension methods in Orcas.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.marklio.com/marklio/aggbug.ashx?id=9145f690-7279-47b9-95cc-b439660402ad" /&gt;</description>
      <comments>http://www.marklio.com/marklio/CommentView,guid,9145f690-7279-47b9-95cc-b439660402ad.aspx</comments>
      <category>CLR</category>
      <category>Delegates</category>
      <category>Fun</category>
      <category>Software Development</category>
      <category>Technical</category>
    </item>
    <item>
      <trackback:ping>http://www.marklio.com/marklio/Trackback.aspx?guid=3df80b83-9191-4495-9d9f-b6238d06f883</trackback:ping>
      <pingback:server>http://www.marklio.com/marklio/pingback.aspx</pingback:server>
      <pingback:target>http://www.marklio.com/marklio/PermaLink,guid,3df80b83-9191-4495-9d9f-b6238d06f883.aspx</pingback:target>
      <dc:creator>Mark</dc:creator>
      <wfw:comment>http://www.marklio.com/marklio/CommentView,guid,3df80b83-9191-4495-9d9f-b6238d06f883.aspx</wfw:comment>
      <wfw:commentRss>http://www.marklio.com/marklio/SyndicationService.asmx/GetEntryCommentsRss?guid=3df80b83-9191-4495-9d9f-b6238d06f883</wfw:commentRss>
      <slash:comments>2</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
In <a href="http://www.marklio.com/marklio/ct.ashx?id=3df80b83-9191-4495-9d9f-b6238d06f883&amp;url=http%3a%2f%2fwww.marklio.com%2fmarklio%2fPermaLink%2cguid%2ccce144d6-e166-45e5-a82a-55594c83e0cb.aspx" target="_blank">my
last post</a>, I showed a nifty way of constructing "early-bound" delegates using
LCG.  Here's the same helper class implemented without LCG:
</p>
        <blockquote>
          <pre>
            <span style="COLOR: #0000ff">public</span>
            <span style="COLOR: #0000ff">static</span>
            <span style="COLOR: #0000ff">class</span> DelegateBinder
{ <span style="COLOR: #0000ff">public</span><span style="COLOR: #0000ff">static</span> TDelegate
Bind&lt;TDelegate&gt;(<span style="COLOR: #0000ff">object</span> firstArg, MethodInfo
method) { <span style="COLOR: #0000ff">return</span> (TDelegate)Activator.CreateInstance( <span style="COLOR: #0000ff">typeof</span>(TDelegate),
firstArg, method.MethodHandle.GetFunctionPointer()); } } </pre>
        </blockquote>
        <p>
This one is quite a bit simpler, and extrapolating from what we learned last time,
it's easy to see what's happening.  Hopefully, you are already familiar
with the <a href="http://www.marklio.com/marklio/ct.ashx?id=3df80b83-9191-4495-9d9f-b6238d06f883&amp;url=http%3a%2f%2fmsdn2.microsoft.com%2fen-us%2flibrary%2fsystem.activator.aspx" target="_blank">Activator</a> class. 
Basically, this just shows the managed call chain that produces a function pointer
to a method given a MethodInfo.
</p>
        <p>
I really like the LCG-based implementation, but only because of my love of DynamicMethod. 
It's pretty complex, and aside from opportunities for caching, doesn't really have
anything over this implementation. This one is just plain simple, and would have a
single-line implementation if I hadn't put some line breaks to avoid formatting problems. 
It does, however, highlight the annoyingness of having to work around the compilers' "helpfulness"
when it comes to delegate construction.  If only I could just call the constructor
directly.
</p>
        <p>
It is worth noting that this doesn't work in the Silverlight 1.1 alpha or the compact
framework (or XNA for that matter), neither of which expose <a href="http://www.marklio.com/marklio/ct.ashx?id=3df80b83-9191-4495-9d9f-b6238d06f883&amp;url=http%3a%2f%2fmsdn2.microsoft.com%2fen-us%2flibrary%2fsystem.runtimemethodhandle.getfunctionpointer.aspx" target="_blank">RuntimeMethodHandle.GetFunctionPointer()</a>.
</p>
        <img width="0" height="0" src="http://www.marklio.com/marklio/aggbug.ashx?id=3df80b83-9191-4495-9d9f-b6238d06f883" />
      </body>
      <title>Early-bind delegates without LCG</title>
      <guid isPermaLink="false">http://www.marklio.com/marklio/PermaLink,guid,3df80b83-9191-4495-9d9f-b6238d06f883.aspx</guid>
      <link>http://www.marklio.com/marklio/PermaLink,guid,3df80b83-9191-4495-9d9f-b6238d06f883.aspx</link>
      <pubDate>Mon, 14 May 2007 23:40:01 GMT</pubDate>
      <description>&lt;p&gt;
In &lt;a href="http://www.marklio.com/marklio/ct.ashx?id=3df80b83-9191-4495-9d9f-b6238d06f883&amp;amp;url=http%3a%2f%2fwww.marklio.com%2fmarklio%2fPermaLink%2cguid%2ccce144d6-e166-45e5-a82a-55594c83e0cb.aspx" target=_blank&gt;my
last post&lt;/a&gt;, I showed a nifty way of constructing "early-bound" delegates using
LCG.&amp;nbsp; Here's the same helper class implemented without LCG:
&lt;/p&gt;
&lt;blockquote&gt;&lt;pre&gt;&lt;span style="COLOR: #0000ff"&gt;public&lt;/span&gt; &lt;span style="COLOR: #0000ff"&gt;static&lt;/span&gt; &lt;span style="COLOR: #0000ff"&gt;class&lt;/span&gt; DelegateBinder
{ &lt;span style="COLOR: #0000ff"&gt;public&lt;/span&gt; &lt;span style="COLOR: #0000ff"&gt;static&lt;/span&gt; TDelegate
Bind&amp;lt;TDelegate&amp;gt;(&lt;span style="COLOR: #0000ff"&gt;object&lt;/span&gt; firstArg, MethodInfo
method) { &lt;span style="COLOR: #0000ff"&gt;return&lt;/span&gt; (TDelegate)Activator.CreateInstance( &lt;span style="COLOR: #0000ff"&gt;typeof&lt;/span&gt;(TDelegate),
firstArg, method.MethodHandle.GetFunctionPointer()); } } &lt;/pre&gt;&lt;/blockquote&gt; 
&lt;p&gt;
This one is quite a bit simpler, and extrapolating from what we learned last time,
it's easy to see what's happening.&amp;nbsp; Hopefully, you are already&amp;nbsp;familiar
with the &lt;a href="http://www.marklio.com/marklio/ct.ashx?id=3df80b83-9191-4495-9d9f-b6238d06f883&amp;amp;url=http%3a%2f%2fmsdn2.microsoft.com%2fen-us%2flibrary%2fsystem.activator.aspx" target=_blank&gt;Activator&lt;/a&gt; class.&amp;nbsp;
Basically, this just shows the managed call chain that produces a function pointer
to a method given a MethodInfo.
&lt;/p&gt;
&lt;p&gt;
I really like the LCG-based implementation, but only because of my love of DynamicMethod.&amp;nbsp;
It's pretty complex, and aside from opportunities for caching, doesn't really have
anything over this implementation. This one is just plain simple, and would have a
single-line implementation if I hadn't put some line breaks to avoid formatting problems.&amp;nbsp;
It does, however, highlight the annoyingness of having to work around the compilers'&amp;nbsp;"helpfulness"
when it comes to delegate construction.&amp;nbsp; If only I could just call the constructor
directly.
&lt;/p&gt;
&lt;p&gt;
It is worth noting that this doesn't work in the Silverlight 1.1 alpha or the compact
framework (or XNA for that matter), neither of which expose &lt;a href="http://www.marklio.com/marklio/ct.ashx?id=3df80b83-9191-4495-9d9f-b6238d06f883&amp;amp;url=http%3a%2f%2fmsdn2.microsoft.com%2fen-us%2flibrary%2fsystem.runtimemethodhandle.getfunctionpointer.aspx" target=_blank&gt;RuntimeMethodHandle.GetFunctionPointer()&lt;/a&gt;.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.marklio.com/marklio/aggbug.ashx?id=3df80b83-9191-4495-9d9f-b6238d06f883" /&gt;</description>
      <comments>http://www.marklio.com/marklio/CommentView,guid,3df80b83-9191-4495-9d9f-b6238d06f883.aspx</comments>
      <category>CLR</category>
      <category>Delegates</category>
      <category>Fun</category>
      <category>Software Development</category>
      <category>Technical</category>
    </item>
    <item>
      <trackback:ping>http://www.marklio.com/marklio/Trackback.aspx?guid=cce144d6-e166-45e5-a82a-55594c83e0cb</trackback:ping>
      <pingback:server>http://www.marklio.com/marklio/pingback.aspx</pingback:server>
      <pingback:target>http://www.marklio.com/marklio/PermaLink,guid,cce144d6-e166-45e5-a82a-55594c83e0cb.aspx</pingback:target>
      <dc:creator>Mark</dc:creator>
      <wfw:comment>http://www.marklio.com/marklio/CommentView,guid,cce144d6-e166-45e5-a82a-55594c83e0cb.aspx</wfw:comment>
      <wfw:commentRss>http://www.marklio.com/marklio/SyndicationService.asmx/GetEntryCommentsRss?guid=cce144d6-e166-45e5-a82a-55594c83e0cb</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
In a <a href="http://www.marklio.com/marklio/ct.ashx?id=cce144d6-e166-45e5-a82a-55594c83e0cb&amp;url=http%3a%2f%2fwww.marklio.com%2fmarklio%2fPermaLink%2cguid%2c4269ecd2-381c-4ab0-ae2e-7e88ed706977.aspx" target="_blank">previous
post about delegates</a>, I discussed the following interesting cases of delegates:
</p>
        <ul>
          <li>
Closed static 
</li>
          <li>
Open instance</li>
        </ul>
        <p>
See the previous post for the full explanation, but these basically open up some interesting
dynamic scenarios.  The problem is that C# and VB do not expose syntax for constructing
these in an "early-bound" fashion, that is using the special constructor on the delegate
type rather than Delegate.CreateDelegate (which more or less binds via reflection).
</p>
        <p>
For most scenarios this is not a huge problem, but there are some performance considerations
and other issues to consider that I don't really want to dig into at the moment. 
One sufficiently important scenario is <em><strong>testing</strong></em> early-bound
invocation.  If your language doesn't support something, how can you test it? 
Well, you can write the whole test in IL, but that is not a terribly maintainable
proposition.
</p>
        <p>
Another option is to only write the part you need in IL.  Unfortunately, C# doesn't
allow you to write inline IL, but you can use Reflection.Emit.  And, since v2.0,
you can use LCG (Lightweight Code Generation) via DynamicMethod.
</p>
        <p>
The trick here is to understand how delegates are instantiated.  Delegates are
just classes like any other.  They inherit from MulticastDelegate (typically). 
The special part is that the runtime provides all the implementation and they have
a special constructor.  Here's (approximately) the constructor signature
for System.Action&lt;T&gt;:
</p>
        <blockquote>
          <pre>
            <span style="COLOR: #0000ff">public</span> Action(<span style="COLOR: #0000ff">object</span> o,
IntPtr method)</pre>
        </blockquote>
        <p>
Object? IntPtr?  What the heck? Well, it's not as bizarre as you might think. 
The object is simply the first argument for the invocation.  This allows binding
to a particular instance ("this" for instance methods, arg 0 for static methods).
The IntPtr is a pointer to the method.  "Pointers?!!?!?! in managed code?!?!"
you say?  That's right, a pointer.  An object is easy enough to come by,
but where do I get the pointer?  Well, the pointer can be easily retrieved via
the <a href="http://www.marklio.com/marklio/ct.ashx?id=cce144d6-e166-45e5-a82a-55594c83e0cb&amp;url=http%3a%2f%2fmsdn2.microsoft.com%2fen-us%2flibrary%2fsystem.reflection.emit.opcodes.ldftn.aspx" target="_blank">ldftn</a> opcode. 
It loads the address of a given method (described via a token in IL, and a MethodInfo
in Reflection.Emit).
</p>
        <p>
Lets cut to the chase.  Here's a little class that can bind a method to a delegate
type and allow you to provide the first argument (you'll need System, System.Reflection,
System.Reflection.Emit using statements):
</p>
        <blockquote>
          <pre>
            <span style="COLOR: #0000ff">public</span>
            <span style="COLOR: #0000ff">static</span>
            <span style="COLOR: #0000ff">class</span> DelegateBinder
{ <span style="COLOR: #0000ff">public</span><span style="COLOR: #0000ff">delegate</span> TDelegate
Binder&lt;TDelegate&gt;(<span style="COLOR: #0000ff">object</span> firstArg); <span style="COLOR: #0000ff">public</span><span style="COLOR: #0000ff">static</span> TDelegate
Bind&lt;TDelegate&gt;(<span style="COLOR: #0000ff">object</span> firstArg, MethodInfo
method) { DynamicMethod dynMethod = <span style="COLOR: #0000ff">new</span> DynamicMethod("<span style="COLOR: #8b0000">PassthroughBinderImplementation</span>", <span style="COLOR: #0000ff">typeof</span>(TDelegate), <span style="COLOR: #0000ff">new</span> Type[]
{ <span style="COLOR: #0000ff">typeof</span>(<span style="COLOR: #0000ff">object</span>)
}, <span style="COLOR: #0000ff">typeof</span>(DelegateBinder)); ILGenerator gen =
dynMethod.GetILGenerator(); <span style="COLOR: #008000">//load the first argument</span> gen.Emit(OpCodes.Ldarg_0); <span style="COLOR: #008000">//load
the address of the method</span> gen.Emit(OpCodes.Ldftn, method); <span style="COLOR: #008000">//create
the delegate</span> gen.Emit(OpCodes.Newobj, <span style="COLOR: #0000ff">typeof</span>(TDelegate).GetConstructor(<span style="COLOR: #0000ff">new</span> Type[]
{ <span style="COLOR: #0000ff">typeof</span>(<span style="COLOR: #0000ff">object</span>), <span style="COLOR: #0000ff">typeof</span>(IntPtr)
})); gen.Emit(OpCodes.Ret); <span style="COLOR: #0000ff">return</span> ((Binder&lt;TDelegate&gt;)dynMethod.CreateDelegate(<span style="COLOR: #0000ff">typeof</span>(Binder&lt;TDelegate&gt;)))(firstArg);
} } </pre>
        </blockquote>
        <p>
With this class, you can dynamically construct all the early-bound variants (ignoring
variants for signature relaxation) like so:
</p>
        <blockquote>
          <pre>
            <span style="COLOR: #0000ff">using</span> System; <span style="COLOR: #0000ff">using</span> System.Reflection; <span style="COLOR: #0000ff">using</span> System.Reflection.Emit; <span style="COLOR: #0000ff">public</span><span style="COLOR: #0000ff">delegate</span><span style="COLOR: #0000ff">string</span> Passthrough(<span style="COLOR: #0000ff">string</span> str); <span style="COLOR: #0000ff">public</span><span style="COLOR: #0000ff">delegate</span><span style="COLOR: #0000ff">string</span> BoundPassthrough(); <span style="COLOR: #0000ff">public</span><span style="COLOR: #0000ff">delegate</span><span style="COLOR: #0000ff">string</span> ProgramPassthrough(Program
p); <span style="COLOR: #0000ff">public</span><span style="COLOR: #0000ff">class</span> Program
{ <span style="COLOR: #0000ff">static</span><span style="COLOR: #0000ff">void</span> Main(<span style="COLOR: #0000ff">string</span>[]
args) { Console.WriteLine("<span style="COLOR: #8b0000">Open Static:</span>"); Passthrough
ospt = DelegateBinder.Bind&lt;Passthrough&gt;(<span style="COLOR: #0000ff">null</span>, <span style="COLOR: #0000ff">typeof</span>(Program).GetMethod("<span style="COLOR: #8b0000">StaticImplementation</span>", <span style="COLOR: #0000ff">new</span> Type[]
{ <span style="COLOR: #0000ff">typeof</span>(<span style="COLOR: #0000ff">string</span>)
})); Console.WriteLine(ospt("<span style="COLOR: #8b0000">Hello World</span>")); Console.WriteLine("<span style="COLOR: #8b0000">Closed
static:</span>"); BoundPassthrough cspt = DelegateBinder.Bind&lt;BoundPassthrough&gt;("<span style="COLOR: #8b0000">Hello
World</span>", <span style="COLOR: #0000ff">typeof</span>(Program).GetMethod("<span style="COLOR: #8b0000">StaticImplementation</span>", <span style="COLOR: #0000ff">new</span> Type[]
{ <span style="COLOR: #0000ff">typeof</span>(<span style="COLOR: #0000ff">string</span>)
})); Console.WriteLine(cspt()); Console.WriteLine("<span style="COLOR: #8b0000">Open
Instance:</span>"); ProgramPassthrough oipt = DelegateBinder.Bind&lt;ProgramPassthrough&gt;(<span style="COLOR: #0000ff">null</span>, <span style="COLOR: #0000ff">typeof</span>(Program).GetMethod("<span style="COLOR: #8b0000">InstanceImplementation</span>",
Type.EmptyTypes)); Console.WriteLine(oipt(<span style="COLOR: #0000ff">new</span> Program("<span style="COLOR: #8b0000">Hello
World</span>"))); Console.WriteLine("<span style="COLOR: #8b0000">Closed Instance:</span>");
BoundPassthrough cipt = DelegateBinder.Bind&lt;BoundPassthrough&gt;(<span style="COLOR: #0000ff">new</span> Program("<span style="COLOR: #8b0000">Hello
World</span>"), <span style="COLOR: #0000ff">typeof</span>(Program).GetMethod("<span style="COLOR: #8b0000">InstanceImplementation</span>",
Type.EmptyTypes)); Console.WriteLine(cipt()); } <span style="COLOR: #0000ff">public</span><span style="COLOR: #0000ff">static</span><span style="COLOR: #0000ff">string</span> StaticImplementation(<span style="COLOR: #0000ff">string</span> str)
{ <span style="COLOR: #0000ff">return</span> str; } <span style="COLOR: #0000ff">public</span> Program(<span style="COLOR: #0000ff">string</span> payload)
{ _Payload = payload; } <span style="COLOR: #0000ff">string</span> _Payload; <span style="COLOR: #0000ff">public</span><span style="COLOR: #0000ff">string</span> InstanceImplementation()
{ <span style="COLOR: #0000ff">return</span> _Payload; } }</pre>
        </blockquote>
        <p>
So, there are certainly cases that will break this, most involving incompatible signature
issues between the method, delegate, and the first argument.  But I didn't want
to make things more complicated for an example. Besides, the point of this is not
really to give you some neat tool (you'll probably never need to do this), but to
give people a better idea what the compiler is doing for you when you create a delegate.
</p>
        <img width="0" height="0" src="http://www.marklio.com/marklio/aggbug.ashx?id=cce144d6-e166-45e5-a82a-55594c83e0cb" />
      </body>
      <title>Early-bound delegates via LCG</title>
      <guid isPermaLink="false">http://www.marklio.com/marklio/PermaLink,guid,cce144d6-e166-45e5-a82a-55594c83e0cb.aspx</guid>
      <link>http://www.marklio.com/marklio/PermaLink,guid,cce144d6-e166-45e5-a82a-55594c83e0cb.aspx</link>
      <pubDate>Fri, 11 May 2007 22:54:23 GMT</pubDate>
      <description>&lt;p&gt;
In a &lt;a href="http://www.marklio.com/marklio/ct.ashx?id=cce144d6-e166-45e5-a82a-55594c83e0cb&amp;amp;url=http%3a%2f%2fwww.marklio.com%2fmarklio%2fPermaLink%2cguid%2c4269ecd2-381c-4ab0-ae2e-7e88ed706977.aspx" target=_blank&gt;previous
post about delegates&lt;/a&gt;, I discussed the following interesting cases of delegates:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
Closed static 
&lt;li&gt;
Open instance&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
See the previous post for the full explanation, but these basically open up some interesting
dynamic scenarios.&amp;nbsp; The problem is that C# and VB do not expose syntax for constructing
these in an "early-bound" fashion, that is using the special constructor on the delegate
type rather than Delegate.CreateDelegate (which more or less binds via reflection).
&lt;/p&gt;
&lt;p&gt;
For most scenarios this is not a huge problem, but there are some performance considerations
and other issues to consider that I don't really want to dig into at the moment.&amp;nbsp;
One sufficiently important scenario is &lt;em&gt;&lt;strong&gt;testing&lt;/strong&gt;&lt;/em&gt; early-bound
invocation.&amp;nbsp; If your language doesn't support something, how can you test it?&amp;nbsp;
Well, you can write the whole test in IL, but that is not a terribly maintainable
proposition.
&lt;/p&gt;
&lt;p&gt;
Another option is to only write the part you need in IL.&amp;nbsp; Unfortunately, C# doesn't
allow you to write inline IL, but you can use Reflection.Emit.&amp;nbsp; And, since v2.0,
you can use LCG (Lightweight Code Generation) via DynamicMethod.
&lt;/p&gt;
&lt;p&gt;
The trick here is to understand how delegates are instantiated.&amp;nbsp; Delegates are
just classes like any other.&amp;nbsp; They inherit from MulticastDelegate (typically).&amp;nbsp;
The special part is that the runtime provides all the implementation and they have
a special constructor.&amp;nbsp;&amp;nbsp;Here's (approximately) the constructor signature
for System.Action&amp;lt;T&amp;gt;:
&lt;/p&gt;
&lt;blockquote&gt;&lt;pre&gt;&lt;span style="COLOR: #0000ff"&gt;public&lt;/span&gt; Action(&lt;span style="COLOR: #0000ff"&gt;object&lt;/span&gt; o,
IntPtr method)&lt;/pre&gt;&lt;/blockquote&gt; 
&lt;p&gt;
Object? IntPtr?&amp;nbsp; What the heck? Well, it's not as bizarre as you might think.&amp;nbsp;
The object is simply the first argument for the invocation.&amp;nbsp; This allows binding
to a particular instance ("this" for instance methods, arg 0 for static methods).
The IntPtr is a pointer to the method.&amp;nbsp; "Pointers?!!?!?! in managed code?!?!"
you say?&amp;nbsp; That's right, a pointer.&amp;nbsp; An object is easy enough to come by,
but where do I get the pointer?&amp;nbsp; Well, the pointer can be easily retrieved via
the &lt;a href="http://www.marklio.com/marklio/ct.ashx?id=cce144d6-e166-45e5-a82a-55594c83e0cb&amp;amp;url=http%3a%2f%2fmsdn2.microsoft.com%2fen-us%2flibrary%2fsystem.reflection.emit.opcodes.ldftn.aspx" target=_blank&gt;ldftn&lt;/a&gt; opcode.&amp;nbsp;
It loads the address of a given method (described via a token in IL, and a MethodInfo
in Reflection.Emit).
&lt;/p&gt;
&lt;p&gt;
Lets cut to the chase.&amp;nbsp; Here's a little class that can bind a method to a delegate
type and allow you to provide the first argument (you'll need System, System.Reflection,
System.Reflection.Emit using statements):
&lt;/p&gt;
&lt;blockquote&gt;&lt;pre&gt;&lt;span style="COLOR: #0000ff"&gt;public&lt;/span&gt; &lt;span style="COLOR: #0000ff"&gt;static&lt;/span&gt; &lt;span style="COLOR: #0000ff"&gt;class&lt;/span&gt; DelegateBinder
{ &lt;span style="COLOR: #0000ff"&gt;public&lt;/span&gt; &lt;span style="COLOR: #0000ff"&gt;delegate&lt;/span&gt; TDelegate
Binder&amp;lt;TDelegate&amp;gt;(&lt;span style="COLOR: #0000ff"&gt;object&lt;/span&gt; firstArg); &lt;span style="COLOR: #0000ff"&gt;public&lt;/span&gt; &lt;span style="COLOR: #0000ff"&gt;static&lt;/span&gt; TDelegate
Bind&amp;lt;TDelegate&amp;gt;(&lt;span style="COLOR: #0000ff"&gt;object&lt;/span&gt; firstArg, MethodInfo
method) { DynamicMethod dynMethod = &lt;span style="COLOR: #0000ff"&gt;new&lt;/span&gt; DynamicMethod("&lt;span style="COLOR: #8b0000"&gt;PassthroughBinderImplementation&lt;/span&gt;", &lt;span style="COLOR: #0000ff"&gt;typeof&lt;/span&gt;(TDelegate), &lt;span style="COLOR: #0000ff"&gt;new&lt;/span&gt; Type[]
{ &lt;span style="COLOR: #0000ff"&gt;typeof&lt;/span&gt;(&lt;span style="COLOR: #0000ff"&gt;object&lt;/span&gt;)
}, &lt;span style="COLOR: #0000ff"&gt;typeof&lt;/span&gt;(DelegateBinder)); ILGenerator gen =
dynMethod.GetILGenerator(); &lt;span style="COLOR: #008000"&gt;//load the first argument&lt;/span&gt; gen.Emit(OpCodes.Ldarg_0); &lt;span style="COLOR: #008000"&gt;//load
the address of the method&lt;/span&gt; gen.Emit(OpCodes.Ldftn, method); &lt;span style="COLOR: #008000"&gt;//create
the delegate&lt;/span&gt; gen.Emit(OpCodes.Newobj, &lt;span style="COLOR: #0000ff"&gt;typeof&lt;/span&gt;(TDelegate).GetConstructor(&lt;span style="COLOR: #0000ff"&gt;new&lt;/span&gt; Type[]
{ &lt;span style="COLOR: #0000ff"&gt;typeof&lt;/span&gt;(&lt;span style="COLOR: #0000ff"&gt;object&lt;/span&gt;), &lt;span style="COLOR: #0000ff"&gt;typeof&lt;/span&gt;(IntPtr)
})); gen.Emit(OpCodes.Ret); &lt;span style="COLOR: #0000ff"&gt;return&lt;/span&gt; ((Binder&amp;lt;TDelegate&amp;gt;)dynMethod.CreateDelegate(&lt;span style="COLOR: #0000ff"&gt;typeof&lt;/span&gt;(Binder&amp;lt;TDelegate&amp;gt;)))(firstArg);
} } &lt;/pre&gt;&lt;/blockquote&gt; 
&lt;p&gt;
With this class, you can dynamically&amp;nbsp;construct all the early-bound variants (ignoring
variants for signature relaxation) like so:
&lt;/p&gt;
&lt;blockquote&gt;&lt;pre&gt;&lt;span style="COLOR: #0000ff"&gt;using&lt;/span&gt; System; &lt;span style="COLOR: #0000ff"&gt;using&lt;/span&gt; System.Reflection; &lt;span style="COLOR: #0000ff"&gt;using&lt;/span&gt; System.Reflection.Emit; &lt;span style="COLOR: #0000ff"&gt;public&lt;/span&gt; &lt;span style="COLOR: #0000ff"&gt;delegate&lt;/span&gt; &lt;span style="COLOR: #0000ff"&gt;string&lt;/span&gt; Passthrough(&lt;span style="COLOR: #0000ff"&gt;string&lt;/span&gt; str); &lt;span style="COLOR: #0000ff"&gt;public&lt;/span&gt; &lt;span style="COLOR: #0000ff"&gt;delegate&lt;/span&gt; &lt;span style="COLOR: #0000ff"&gt;string&lt;/span&gt; BoundPassthrough(); &lt;span style="COLOR: #0000ff"&gt;public&lt;/span&gt; &lt;span style="COLOR: #0000ff"&gt;delegate&lt;/span&gt; &lt;span style="COLOR: #0000ff"&gt;string&lt;/span&gt; ProgramPassthrough(Program
p); &lt;span style="COLOR: #0000ff"&gt;public&lt;/span&gt; &lt;span style="COLOR: #0000ff"&gt;class&lt;/span&gt; Program
{ &lt;span style="COLOR: #0000ff"&gt;static&lt;/span&gt; &lt;span style="COLOR: #0000ff"&gt;void&lt;/span&gt; Main(&lt;span style="COLOR: #0000ff"&gt;string&lt;/span&gt;[]
args) { Console.WriteLine("&lt;span style="COLOR: #8b0000"&gt;Open Static:&lt;/span&gt;"); Passthrough
ospt = DelegateBinder.Bind&amp;lt;Passthrough&amp;gt;(&lt;span style="COLOR: #0000ff"&gt;null&lt;/span&gt;, &lt;span style="COLOR: #0000ff"&gt;typeof&lt;/span&gt;(Program).GetMethod("&lt;span style="COLOR: #8b0000"&gt;StaticImplementation&lt;/span&gt;", &lt;span style="COLOR: #0000ff"&gt;new&lt;/span&gt; Type[]
{ &lt;span style="COLOR: #0000ff"&gt;typeof&lt;/span&gt;(&lt;span style="COLOR: #0000ff"&gt;string&lt;/span&gt;)
})); Console.WriteLine(ospt("&lt;span style="COLOR: #8b0000"&gt;Hello World&lt;/span&gt;")); Console.WriteLine("&lt;span style="COLOR: #8b0000"&gt;Closed
static:&lt;/span&gt;"); BoundPassthrough cspt = DelegateBinder.Bind&amp;lt;BoundPassthrough&amp;gt;("&lt;span style="COLOR: #8b0000"&gt;Hello
World&lt;/span&gt;", &lt;span style="COLOR: #0000ff"&gt;typeof&lt;/span&gt;(Program).GetMethod("&lt;span style="COLOR: #8b0000"&gt;StaticImplementation&lt;/span&gt;", &lt;span style="COLOR: #0000ff"&gt;new&lt;/span&gt; Type[]
{ &lt;span style="COLOR: #0000ff"&gt;typeof&lt;/span&gt;(&lt;span style="COLOR: #0000ff"&gt;string&lt;/span&gt;)
})); Console.WriteLine(cspt()); Console.WriteLine("&lt;span style="COLOR: #8b0000"&gt;Open
Instance:&lt;/span&gt;"); ProgramPassthrough oipt = DelegateBinder.Bind&amp;lt;ProgramPassthrough&amp;gt;(&lt;span style="COLOR: #0000ff"&gt;null&lt;/span&gt;, &lt;span style="COLOR: #0000ff"&gt;typeof&lt;/span&gt;(Program).GetMethod("&lt;span style="COLOR: #8b0000"&gt;InstanceImplementation&lt;/span&gt;",
Type.EmptyTypes)); Console.WriteLine(oipt(&lt;span style="COLOR: #0000ff"&gt;new&lt;/span&gt; Program("&lt;span style="COLOR: #8b0000"&gt;Hello
World&lt;/span&gt;"))); Console.WriteLine("&lt;span style="COLOR: #8b0000"&gt;Closed Instance:&lt;/span&gt;");
BoundPassthrough cipt = DelegateBinder.Bind&amp;lt;BoundPassthrough&amp;gt;(&lt;span style="COLOR: #0000ff"&gt;new&lt;/span&gt; Program("&lt;span style="COLOR: #8b0000"&gt;Hello
World&lt;/span&gt;"), &lt;span style="COLOR: #0000ff"&gt;typeof&lt;/span&gt;(Program).GetMethod("&lt;span style="COLOR: #8b0000"&gt;InstanceImplementation&lt;/span&gt;",
Type.EmptyTypes)); Console.WriteLine(cipt()); } &lt;span style="COLOR: #0000ff"&gt;public&lt;/span&gt; &lt;span style="COLOR: #0000ff"&gt;static&lt;/span&gt; &lt;span style="COLOR: #0000ff"&gt;string&lt;/span&gt; StaticImplementation(&lt;span style="COLOR: #0000ff"&gt;string&lt;/span&gt; str)
{ &lt;span style="COLOR: #0000ff"&gt;return&lt;/span&gt; str; } &lt;span style="COLOR: #0000ff"&gt;public&lt;/span&gt; Program(&lt;span style="COLOR: #0000ff"&gt;string&lt;/span&gt; payload)
{ _Payload = payload; } &lt;span style="COLOR: #0000ff"&gt;string&lt;/span&gt; _Payload; &lt;span style="COLOR: #0000ff"&gt;public&lt;/span&gt; &lt;span style="COLOR: #0000ff"&gt;string&lt;/span&gt; InstanceImplementation()
{ &lt;span style="COLOR: #0000ff"&gt;return&lt;/span&gt; _Payload; } }&lt;/pre&gt;&lt;/blockquote&gt; 
&lt;p&gt;
So, there are certainly cases that will break this, most involving incompatible signature
issues between the method, delegate, and the first argument.&amp;nbsp; But I didn't want
to make things more complicated for an example. Besides, the point of this is not
really to give you some neat tool (you'll probably never need to do this), but to
give people a better idea what the compiler is doing for you when you create a delegate.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.marklio.com/marklio/aggbug.ashx?id=cce144d6-e166-45e5-a82a-55594c83e0cb" /&gt;</description>
      <comments>http://www.marklio.com/marklio/CommentView,guid,cce144d6-e166-45e5-a82a-55594c83e0cb.aspx</comments>
      <category>CLR</category>
      <category>Delegates</category>
      <category>Fun</category>
      <category>Software Development</category>
      <category>Technical</category>
    </item>
    <item>
      <trackback:ping>http://www.marklio.com/marklio/Trackback.aspx?guid=d65c3e37-70d3-4219-b8b7-34a8423f35e4</trackback:ping>
      <pingback:server>http://www.marklio.com/marklio/pingback.aspx</pingback:server>
      <pingback:target>http://www.marklio.com/marklio/PermaLink,guid,d65c3e37-70d3-4219-b8b7-34a8423f35e4.aspx</pingback:target>
      <dc:creator>Mark</dc:creator>
      <wfw:comment>http://www.marklio.com/marklio/CommentView,guid,d65c3e37-70d3-4219-b8b7-34a8423f35e4.aspx</wfw:comment>
      <wfw:commentRss>http://www.marklio.com/marklio/SyndicationService.asmx/GetEntryCommentsRss?guid=d65c3e37-70d3-4219-b8b7-34a8423f35e4</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
So, my <a href="http://www.marklio.com/marklio/ct.ashx?id=d65c3e37-70d3-4219-b8b7-34a8423f35e4&amp;url=http%3a%2f%2fwww.marklio.com%2fmarklio%2fPermaLink%2cguid%2c4269ecd2-381c-4ab0-ae2e-7e88ed706977.aspx">last
post</a> talked about delegates.  In it, I mentioned some compiler trickery involved
in declaring events, but I didn't bother explaining it.  After reading it over
again, and getting some feedback, I felt bad about glossing over what is pretty much
the mainline scenario for delegates. So, what is an event?
</p>
        <p>
An event is kind of like a broadcast. It enables an object to notifiy subscribers
when some "event" occurs, give them relevant information about the event, and allow
them to do something in response.  And, you guessed it, delegates are at the
core of making this work.
</p>
        <p>
Fundamentally, events are a callback mechanism, and could be implemented without delegates
using anything from raw function pointers to interfaces, and the CLR doesn't keep
you from doing either of those, but there's value in a consistent pattern.  In
fact, the designers of the CLR felt so strongly about the value of this particular
pattern, that it is part of the <a href="http://www.marklio.com/marklio/ct.ashx?id=d65c3e37-70d3-4219-b8b7-34a8423f35e4&amp;url=http%3a%2f%2fwww.ecma-international.org%2fpublications%2ffiles%2fECMA-ST%2fEcma-335.pdf">CLI
spec</a> (along with properties, another pattern that is implemented by other more
fundamental constructs).
</p>
        <p>
So, how do you make an event?  Well, in C#, you declare an event like you would
declare a field whose type is some delegate and you add the "event" keyword. 
So, somewhere in a type, you would have something like:
</p>
        <blockquote>
          <p>
            <font face="Courier New">public event EventHandler Click;</font>
          </p>
        </blockquote>
        <p>
Whether or not it's public depends on how you expect the event to be used.  EventHandler
is a delegate with the following signature:
</p>
        <blockquote>
          <p>
            <font face="Courier New">void EventHandler(object sender, EventArgs e);</font>
          </p>
        </blockquote>
        <p>
This signature is another pattern that I'll talk about later.  For now, lets
look at what the compiler does for our event declaration.  The compiler gives
3 things (if you don't count the things it already did for the delegate EventHandler):
</p>
        <ul>
          <li>
A private field whose type is the delegate EventHandler 
</li>
          <li>
A (public in this case) method "accessor" for adding delegate callbacks: add_Click
//Click comes from the event name 
</li>
          <li>
A (public in this case) method "accessor" for removing delegate callbacks: remove_Click</li>
        </ul>
        <p>
When other code wants to hook up to your event, they use the += operator on your event. 
This is really syntax sugar for calling the add_Click method.  And, conversely
the -= operator calls the remove accessor.
</p>
        <p>
Interestingly, you can write your own implementation for the event pattern. 
You might want to do this to save size in a possibly large tree structure with
lots of events at each node.  ASP.net does this with controls.  Rather than
every Control having tons of fields for each event, it has a sparse dictionary of
event delegates, that is only populated for events that have "subscribers". 
With a tree that can easily have thousands of controls per page view, this results
in a sizeable savings.  How do you do this?  Well, in C#, you use the little
known syntax:
</p>
        <blockquote>
          <p>
            <font face="Courier New">public event EventHandler Click {</font>
          </p>
          <p>
            <font face="Courier New">add {/* do something with value in here */}</font>
          </p>
        </blockquote>
        <blockquote>
          <p>
            <font face="Courier New">remove {/* do something with value in here */}</font>
          </p>
          <p>
            <font face="Courier New">}</font>
          </p>
        </blockquote>
        <p>
Looks like a property eh? This causes the compiler not to create the 3 things I mentioned
above. Instead, it calls your add and remove accesors to do the adding and removing
(via the value keyword just like properties).  In it, you can do anything you
want, although it's advisable to keep the same semantics as the default implementations.
</p>
        <p>
So, lets talk a little bit about what happens when an event happens and it is called. 
Let's say that several other classes have registered for your event (via the += syntax
or whatever the compiler supports).  Inside your class, you simply call the delegate
(there's a recommended pattern for this as well).  But wait, there's more than
1 subscriber!  Remember, delegates aren't just function pointers, and they are
more powerful than using interfaces alone.  If you'll recall in the last post,
I said that when you create a delegate, you're really getting a MulticastDelegate,
which tracks an invocation list of delegates to run. (this is why the standard event
pattern returns void, otherwise, you've got the weird situation of multiple return
values from what appears to be a single call).  Under normal circumstances, each
delegate in the invocation list is called and execution resumes.
</p>
        <img width="0" height="0" src="http://www.marklio.com/marklio/aggbug.ashx?id=d65c3e37-70d3-4219-b8b7-34a8423f35e4" />
      </body>
      <title>What is an event?</title>
      <guid isPermaLink="false">http://www.marklio.com/marklio/PermaLink,guid,d65c3e37-70d3-4219-b8b7-34a8423f35e4.aspx</guid>
      <link>http://www.marklio.com/marklio/PermaLink,guid,d65c3e37-70d3-4219-b8b7-34a8423f35e4.aspx</link>
      <pubDate>Fri, 16 Feb 2007 18:36:02 GMT</pubDate>
      <description>&lt;p&gt;
So, my &lt;a href="http://www.marklio.com/marklio/ct.ashx?id=d65c3e37-70d3-4219-b8b7-34a8423f35e4&amp;amp;url=http%3a%2f%2fwww.marklio.com%2fmarklio%2fPermaLink%2cguid%2c4269ecd2-381c-4ab0-ae2e-7e88ed706977.aspx"&gt;last
post&lt;/a&gt; talked about delegates.&amp;nbsp; In it, I mentioned some compiler trickery involved
in declaring events, but I didn't bother explaining it.&amp;nbsp; After reading it over
again, and getting some feedback, I felt bad about glossing over what is pretty much
the mainline scenario for delegates. So, what is an event?
&lt;/p&gt;
&lt;p&gt;
An event is kind of like a broadcast. It enables an object to notifiy subscribers
when some "event" occurs, give them relevant information about the event, and allow
them to do something in response.&amp;nbsp; And, you guessed it, delegates are at the
core of making this work.
&lt;/p&gt;
&lt;p&gt;
Fundamentally, events are a callback mechanism, and could be implemented without delegates
using anything from raw function pointers to interfaces, and the CLR doesn't keep
you from doing either of those, but there's value in a consistent pattern.&amp;nbsp; In
fact, the designers of the CLR felt so strongly about the value of this particular
pattern, that it is part of the &lt;a href="http://www.marklio.com/marklio/ct.ashx?id=d65c3e37-70d3-4219-b8b7-34a8423f35e4&amp;amp;url=http%3a%2f%2fwww.ecma-international.org%2fpublications%2ffiles%2fECMA-ST%2fEcma-335.pdf"&gt;CLI
spec&lt;/a&gt; (along with properties, another pattern that is implemented by other more
fundamental constructs).
&lt;/p&gt;
&lt;p&gt;
So, how do you make an event?&amp;nbsp; Well, in C#, you declare an event like you would
declare a field whose type is some delegate and you add the "event" keyword.&amp;nbsp;
So, somewhere in a type, you would have something like:
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
&lt;font face="Courier New"&gt;public event EventHandler Click;&lt;/font&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
Whether or not it's public depends on how you expect the event to be used.&amp;nbsp; EventHandler
is a delegate with the following signature:
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
&lt;font face="Courier New"&gt;void EventHandler(object sender, EventArgs e);&lt;/font&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
This signature is another pattern that I'll talk about later.&amp;nbsp; For now, lets
look at what the compiler does for our event declaration.&amp;nbsp; The compiler gives
3 things (if you don't count the things it already did for the delegate EventHandler):
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
A private field whose type is the delegate EventHandler 
&lt;li&gt;
A (public in this case) method "accessor"&amp;nbsp;for adding delegate callbacks: add_Click
//Click comes from the event name 
&lt;li&gt;
A (public in this case) method "accessor" for removing delegate callbacks: remove_Click&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
When other code wants to hook up to your event, they use the += operator on your event.&amp;nbsp;
This is really syntax sugar for calling the add_Click method.&amp;nbsp; And, conversely
the -= operator calls the remove accessor.
&lt;/p&gt;
&lt;p&gt;
Interestingly, you can write your own implementation for the event pattern.&amp;nbsp;
You might want to do this to save size in&amp;nbsp;a possibly large tree structure with
lots of events at each node.&amp;nbsp; ASP.net does this with controls.&amp;nbsp; Rather than
every Control having tons of fields for each event, it has a sparse dictionary of
event delegates, that is only populated for events that have "subscribers".&amp;nbsp;
With a tree that can easily have thousands of controls per page view, this results
in a sizeable savings.&amp;nbsp; How do you do this?&amp;nbsp; Well, in C#, you use the little
known syntax:
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
&lt;font face="Courier New"&gt;public event EventHandler Click {&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font face="Courier New"&gt;add {/* do something with value in here */}&lt;/font&gt;
&lt;/p&gt;
&lt;/blockquote&gt; &lt;blockquote&gt; 
&lt;p&gt;
&lt;font face="Courier New"&gt;remove {/* do something with value in here */}&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font face="Courier New"&gt;}&lt;/font&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
Looks like a property eh? This causes the compiler not to create the 3 things I mentioned
above. Instead, it calls your add and remove accesors to do the adding and removing
(via the value keyword just like properties).&amp;nbsp; In it, you can do anything you
want, although it's advisable to keep the same semantics as the default implementations.
&lt;/p&gt;
&lt;p&gt;
So, lets talk a little bit about what happens when an event happens and it is called.&amp;nbsp;
Let's say that several other classes have registered for your event (via the += syntax
or whatever the compiler supports).&amp;nbsp; Inside your class, you simply call the delegate
(there's a recommended pattern for this as well).&amp;nbsp; But wait, there's more than
1 subscriber!&amp;nbsp; Remember, delegates aren't just function pointers, and they are
more powerful than using interfaces alone.&amp;nbsp; If you'll recall in the last post,
I said that when you create a delegate, you're really getting a MulticastDelegate,
which tracks an invocation list of delegates to run. (this is why the standard event
pattern returns void, otherwise, you've got the weird situation of multiple&amp;nbsp;return
values from what appears to be a single call).&amp;nbsp; Under normal circumstances, each
delegate in the invocation list is called and execution resumes.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.marklio.com/marklio/aggbug.ashx?id=d65c3e37-70d3-4219-b8b7-34a8423f35e4" /&gt;</description>
      <comments>http://www.marklio.com/marklio/CommentView,guid,d65c3e37-70d3-4219-b8b7-34a8423f35e4.aspx</comments>
      <category>CLR</category>
      <category>Delegates</category>
      <category>Technical</category>
    </item>
    <item>
      <trackback:ping>http://www.marklio.com/marklio/Trackback.aspx?guid=4269ecd2-381c-4ab0-ae2e-7e88ed706977</trackback:ping>
      <pingback:server>http://www.marklio.com/marklio/pingback.aspx</pingback:server>
      <pingback:target>http://www.marklio.com/marklio/PermaLink,guid,4269ecd2-381c-4ab0-ae2e-7e88ed706977.aspx</pingback:target>
      <dc:creator>Mark</dc:creator>
      <wfw:comment>http://www.marklio.com/marklio/CommentView,guid,4269ecd2-381c-4ab0-ae2e-7e88ed706977.aspx</wfw:comment>
      <wfw:commentRss>http://www.marklio.com/marklio/SyndicationService.asmx/GetEntryCommentsRss?guid=4269ecd2-381c-4ab0-ae2e-7e88ed706977</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Soon, my ownership area will extend to include delegates.  Since I'm fairly excited
about this, I thought I'd celebrate by writing a little something about them. 
So, what are delegates?  A casual observer might be tempted to write off delegates
as a sort of managed function pointer.  While this comparison is certainly
accurate, there's much more to explaining the power of delegates.
</p>
        <p>
In general, delegates are a sort of universal method dispatch mechanism.  Initially,
the scenario they supported was callbacks.  Delegates are one of the things that
distinguish the CLR from other VMs like Java.  Java requires the use of interfaces
to implement callbacks. (I'm only calling that out as a distinction, not saying the
Java way is bad. although personally I like what delegates bring to the table) 
So, delegates let you wrap up a method as an object to pass around, with the expectation
that it will be called from another context.
</p>
        <p>
Its sort of hard to talk about delegates because the discussion is often framed by
the language that's exposing them.  Currently, no managed language exposes them
in the way that they are represented in IL.  In C# and VB, you declare a delegate
by simply defining a method signature.  From an IL perspective, the compilers
generate a class that inherits from MulticastDelegate (another story I'll get to later),
with an Invoke method that matches your signature, and some various constructors to
support different things.  (You also usually get the corresponding asynchronous
calling pattern support methods, but I don't want to get into that)  Some other
delegate-related compiler trickery is involved in declaring events, which I'll cover
later.
</p>
        <p>
Under the covers, a delegate [conceptually] contains 2 things:
</p>
        <ul>
          <li>
A target object 
</li>
          <li>
A target method</li>
        </ul>
        <p>
Now, generally speaking, the target method is the method to be run, and the target
object is the object on which the target method will be run, but there are cases where
this line is blurred a bit.  For instance, when a delegate is pointing to a static
method, the target object is conceptually null (internally it's not, but that's an
implementation detail).  I'll get into the other cases later.
</p>
        <p>
So now you're saying, "Yup, that's a delegate.  Big deal.  What's so cool
about that?"  What's cool about that, my friend is that delegates are the things
that power virtually all of the coolest new language features that came out in v2.0
and will be coming out, including all the dynamic language goodness like IronPython. 
It's the dynamic stuff that is really exciting, so let's talk about how delegates
enable dynamic languages on top of a statically typed system.
</p>
        <p>
(To be fair, Jim Hugunin did his initial Iron Python work before these features were
available, but they now play a big role)  One of the pieces of work done in v2.0
was called delegate relaxation.  Previously, the target method had to match the
delegate signature exactly.  Now, as you might expect intuitively, the signature
can be relaxed such that the target method can have "more general" parameters and
return something "more specific" than the delegate's signature.  This is typically
defined in terms of covariance and contravariance, terms that even people who understand
them get confused.  Here's the way I usually remember it: If I could wrap the
target method with a method having the delegate's signature without casting, it will
work. Anyway, this feature makes delegates quite a bit more flexible.
</p>
        <p>
Before I go into the other features, lets talk a little about implementation. In normal
method calling in the CLR, the first parameter becomes the "this" object.  (Which
is why you see ldarg.0 in IL to put it on the stack.)  So, conceptually, the
target object represents the first argument for the method. (There is an implementation
detail that allows static methods to be called using the same convention, which is
a really elegant solution) So, by extending this idea of the target object simply
being the first argument, we get a couple of interesting variants.
</p>
        <p>
The first is what is called "closed" static delegates.  This allows you
to specify the first argument of a static method at delegate creation rather
than at the callsite.  Notice this maps quite nicely to the dynamic language
concept of adding a method to an existing instance of an object.  The language
runtime just needs to be able to track these extra methods as part of its method dispatch
logic.
</p>
        <p>
The second feature is "open" instance delegates.  This allows you to create a
delegate that points to an instance method, but doesn't define the target object. 
Instead, the delegate signature can have an extra first argument that will specify
the target object at the callsite.  When used with LCG (DynamicMethod), this
can be used to implement things like adding a method to an existing type.  Again,
the language runtime merely needs to add the logic to method dispatch.
</p>
        <p>
These 2 features are intriguing to me because they are not directly exposed from VB
or C#.  I believe VB9 exposes these, but they are not accessible in an early
bound way in C#.  You can, however, create them via <a href="http://www.marklio.com/marklio/ct.ashx?id=4269ecd2-381c-4ab0-ae2e-7e88ed706977&amp;url=http%3a%2f%2fmsdn2.microsoft.com%2fen-us%2flibrary%2fsystem.delegate.createdelegate.aspx" target="_blank">Delegate.CreateDelegate()</a> using
reflection, or use Reflection.Emit to generate the corresponding IL.
</p>
        <p>
Hopefully, I'll have some time in the future to do some samples of these as well as
discuss more about how these improve the dynamic language support in the CLR.
</p>
        <img width="0" height="0" src="http://www.marklio.com/marklio/aggbug.ashx?id=4269ecd2-381c-4ab0-ae2e-7e88ed706977" />
      </body>
      <title>Delegates in the CLR</title>
      <guid isPermaLink="false">http://www.marklio.com/marklio/PermaLink,guid,4269ecd2-381c-4ab0-ae2e-7e88ed706977.aspx</guid>
      <link>http://www.marklio.com/marklio/PermaLink,guid,4269ecd2-381c-4ab0-ae2e-7e88ed706977.aspx</link>
      <pubDate>Thu, 15 Feb 2007 22:32:53 GMT</pubDate>
      <description>&lt;p&gt;
Soon, my ownership area will extend to include delegates.&amp;nbsp; Since I'm fairly excited
about this, I thought I'd celebrate by writing a little something about them.&amp;nbsp;
So, what are delegates?&amp;nbsp; A casual observer might be tempted to write off delegates
as a sort of&amp;nbsp;managed function pointer.&amp;nbsp; While this comparison is certainly
accurate, there's much more to explaining the power of delegates.
&lt;/p&gt;
&lt;p&gt;
In general, delegates are a sort of universal method dispatch mechanism.&amp;nbsp; Initially,
the scenario they supported was callbacks.&amp;nbsp; Delegates are one of the things that
distinguish the CLR from other VMs like Java.&amp;nbsp; Java requires the use of interfaces
to implement callbacks. (I'm only calling that out as a distinction, not saying the
Java way is bad. although personally I like what delegates bring to the table)&amp;nbsp;
So, delegates let you wrap up a method as an object to pass around, with the expectation
that it will be called from another context.
&lt;/p&gt;
&lt;p&gt;
Its sort of hard to talk about delegates because the discussion is often framed by
the language that's exposing them.&amp;nbsp; Currently, no managed language exposes them
in the way that they are represented in IL.&amp;nbsp; In C# and VB, you declare a delegate
by simply defining a method signature.&amp;nbsp; From an IL perspective, the compilers
generate a class that inherits from MulticastDelegate (another story I'll get to later),
with an Invoke method that matches your signature, and some various constructors to
support different things.&amp;nbsp; (You also usually&amp;nbsp;get the corresponding asynchronous
calling pattern support methods, but I don't want to get into that)&amp;nbsp; Some other
delegate-related compiler trickery is involved in declaring events, which I'll cover
later.
&lt;/p&gt;
&lt;p&gt;
Under the covers, a delegate [conceptually] contains 2 things:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
A target object 
&lt;li&gt;
A target method&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
Now, generally speaking, the target method is the method to be run, and the target
object is the object on which the target method will be run, but there are cases where
this line is blurred a bit.&amp;nbsp; For instance, when a delegate is pointing to a static
method, the target object is conceptually null (internally it's not, but that's an
implementation detail).&amp;nbsp; I'll get into the other cases later.
&lt;/p&gt;
&lt;p&gt;
So now you're saying, "Yup, that's a delegate.&amp;nbsp; Big deal.&amp;nbsp; What's so cool
about that?"&amp;nbsp; What's cool about that, my friend is that delegates are the things
that power virtually all of the coolest new language features that came out in v2.0
and will be coming out, including all the dynamic language goodness like IronPython.&amp;nbsp;
It's the dynamic stuff that is really exciting, so let's talk about how delegates
enable dynamic languages on top of a statically typed system.
&lt;/p&gt;
&lt;p&gt;
(To be fair, Jim Hugunin did his initial Iron Python work before these features were
available, but they now play a big role)&amp;nbsp; One of the pieces of work done in v2.0
was called delegate relaxation.&amp;nbsp; Previously, the target method had to match the
delegate signature exactly.&amp;nbsp; Now, as you might expect intuitively, the signature
can be relaxed such that the target method can have "more general" parameters and
return something "more specific" than the delegate's signature.&amp;nbsp; This is typically
defined in terms of covariance and contravariance, terms that even people who understand
them get confused.&amp;nbsp; Here's the way I usually remember it: If I could wrap the
target method with a method having the delegate's signature without casting, it will
work. Anyway, this feature makes delegates quite a bit more flexible.
&lt;/p&gt;
&lt;p&gt;
Before I go into the other features, lets talk a little about implementation. In normal
method calling in the CLR, the first parameter becomes the "this" object.&amp;nbsp; (Which
is why you see ldarg.0 in IL to put it on the stack.)&amp;nbsp; So, conceptually, the
target object represents the first argument for the method. (There is an implementation
detail that allows static methods to be called using the same convention, which is
a really elegant solution) So, by extending this idea of the target object simply
being the first argument, we get a couple of interesting variants.
&lt;/p&gt;
&lt;p&gt;
The first is what is called "closed" static delegates.&amp;nbsp;&amp;nbsp;This allows you
to specify the first argument&amp;nbsp;of a static method at delegate creation rather
than at the callsite.&amp;nbsp; Notice this maps quite nicely to the dynamic language
concept of adding a method to an existing instance of an object.&amp;nbsp; The language
runtime just needs to be able to track these extra methods as part of its method dispatch
logic.
&lt;/p&gt;
&lt;p&gt;
The second feature is "open" instance delegates.&amp;nbsp; This allows you to create a
delegate that points to an instance method, but doesn't define the target object.&amp;nbsp;
Instead, the delegate signature can have an extra first argument that will specify
the target object at the callsite.&amp;nbsp; When used with LCG (DynamicMethod), this
can be used to implement things like adding a method to an existing type.&amp;nbsp; Again,
the language runtime merely needs to add the logic to method dispatch.
&lt;/p&gt;
&lt;p&gt;
These 2 features are intriguing to me because they are not directly exposed from VB
or C#.&amp;nbsp; I believe VB9 exposes these, but they are not accessible in an early
bound way in C#.&amp;nbsp; You can, however,&amp;nbsp;create them via &lt;a href="http://www.marklio.com/marklio/ct.ashx?id=4269ecd2-381c-4ab0-ae2e-7e88ed706977&amp;amp;url=http%3a%2f%2fmsdn2.microsoft.com%2fen-us%2flibrary%2fsystem.delegate.createdelegate.aspx" target=_blank&gt;Delegate.CreateDelegate()&lt;/a&gt; using
reflection, or use Reflection.Emit to generate the corresponding IL.
&lt;/p&gt;
&lt;p&gt;
Hopefully, I'll have some time in the future to do some samples of these as well as
discuss more about how these improve the dynamic language support in the CLR.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.marklio.com/marklio/aggbug.ashx?id=4269ecd2-381c-4ab0-ae2e-7e88ed706977" /&gt;</description>
      <comments>http://www.marklio.com/marklio/CommentView,guid,4269ecd2-381c-4ab0-ae2e-7e88ed706977.aspx</comments>
      <category>CLR</category>
      <category>Delegates</category>
      <category>Technical</category>
    </item>
  </channel>
</rss>