<?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 - CLR</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 - CLR</title>
      <link>http://www.marklio.com/marklio/</link>
    </image>
    <language>en-us</language>
    <copyright>Mark Miller</copyright>
    <lastBuildDate>Tue, 20 Apr 2010 00:09:03 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=302cbf45-8107-4a1b-8839-d0f0ba6b4412</trackback:ping>
      <pingback:server>http://www.marklio.com/marklio/pingback.aspx</pingback:server>
      <pingback:target>http://www.marklio.com/marklio/PermaLink,guid,302cbf45-8107-4a1b-8839-d0f0ba6b4412.aspx</pingback:target>
      <dc:creator>Mark</dc:creator>
      <wfw:comment>http://www.marklio.com/marklio/CommentView,guid,302cbf45-8107-4a1b-8839-d0f0ba6b4412.aspx</wfw:comment>
      <wfw:commentRss>http://www.marklio.com/marklio/SyndicationService.asmx/GetEntryCommentsRss?guid=302cbf45-8107-4a1b-8839-d0f0ba6b4412</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
A number of CLR hosts may want to update to use the new v4 MetaHost APIs for runtime
activation in order to support in-proc SxS, but want to continue working on a machine
with only v2 installed.  This is a common scenario, used by Office and some other
internal partners, and I imagine it would be a common customer scenario as well.
</p>
        <p>
We’ve already covered that getting off of the legacy shim APIs is critical to supporting
in-proc SxS, but once you move off them, you are stuck only working on a machine that
has v4 installed.  Not to worry, it is fairly easy to “fall-back” to a legacy
activation path in the case that the new APIs are not present.
</p>
        <p>
The gateway to the new MetaHost APIs is CLRCreateInstance, if it isn’t present, you
know that the new APIs aren’t available to use.  So, you simply need to do a
“late-bound” call to it via LoadLibrary/GetProcAddress.  If GetProcAddress returns
NULL, then you need to fallback to the legacy activation (CorBindToRuntimeEx or similar). 
However, the function could be present, but still not be available to use.  The
export is on mscoree.dll, but its implementation actually lives with the runtime itself
(mscoreei.dll, which is new in v4).  If no mscoreei.dll is available to handle
the implementation, mscoree will return E_NOTIMPL from CLRCreateInstance.  This
could occur if your mscoree.dll has been updated as the result of an OS servicing
event, or if you’ve installed/uninstalled .NetFx 4.
</p>
        <p>
So, the basic decision tree is this:
</p>
        <ol>
          <li>
LoadLibrary mscoree. If this doesn’t work, then there is likely no CLR installed at
all, move along, nothing more to see here.</li>
          <li>
GetProcAddress for CLRCreateInstance. If this doesn’t work, fall back to CorBindToRuntimeEx
or similar.</li>
          <li>
Call CLRCreateInstance to get ICLRMetaHost/ICLRMetaHostPolicy.  If this doesn’t
work, fall back to CorBindToRuntimeEx or similar.</li>
          <li>
Otherwise, party on the interface you just got!</li>
        </ol>
        <img width="0" height="0" src="http://www.marklio.com/marklio/aggbug.ashx?id=302cbf45-8107-4a1b-8839-d0f0ba6b4412" />
      </body>
      <title>Writing a CLR host that uses v4 MetaHost APIs, but can run without v4 installed</title>
      <guid isPermaLink="false">http://www.marklio.com/marklio/PermaLink,guid,302cbf45-8107-4a1b-8839-d0f0ba6b4412.aspx</guid>
      <link>http://www.marklio.com/marklio/PermaLink,guid,302cbf45-8107-4a1b-8839-d0f0ba6b4412.aspx</link>
      <pubDate>Tue, 20 Apr 2010 00:09:03 GMT</pubDate>
      <description>&lt;p&gt;
A number of CLR hosts may want to update to use the new v4 MetaHost APIs for runtime
activation in order to support in-proc SxS, but want to continue working on a machine
with only v2 installed.&amp;nbsp; This is a common scenario, used by Office and some other
internal partners, and I imagine it would be a common customer scenario as well.
&lt;/p&gt;
&lt;p&gt;
We’ve already covered that getting off of the legacy shim APIs is critical to supporting
in-proc SxS, but once you move off them, you are stuck only working on a machine that
has v4 installed.&amp;nbsp; Not to worry, it is fairly easy to “fall-back” to a legacy
activation path in the case that the new APIs are not present.
&lt;/p&gt;
&lt;p&gt;
The gateway to the new MetaHost APIs is CLRCreateInstance, if it isn’t present, you
know that the new APIs aren’t available to use.&amp;nbsp; So, you simply need to do a
“late-bound” call to it via LoadLibrary/GetProcAddress.&amp;nbsp; If GetProcAddress returns
NULL, then you need to fallback to the legacy activation (CorBindToRuntimeEx or similar).&amp;nbsp;
However, the function could be present, but still not be available to use.&amp;nbsp; The
export is on mscoree.dll, but its implementation actually lives with the runtime itself
(mscoreei.dll, which is new in v4).&amp;nbsp; If no mscoreei.dll is available to handle
the implementation, mscoree will return E_NOTIMPL from CLRCreateInstance.&amp;nbsp; This
could occur if your mscoree.dll has been updated as the result of an OS servicing
event, or if you’ve installed/uninstalled .NetFx 4.
&lt;/p&gt;
&lt;p&gt;
So, the basic decision tree is this:
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
LoadLibrary mscoree. If this doesn’t work, then there is likely no CLR installed at
all, move along, nothing more to see here.&lt;/li&gt;
&lt;li&gt;
GetProcAddress for CLRCreateInstance. If this doesn’t work, fall back to CorBindToRuntimeEx
or similar.&lt;/li&gt;
&lt;li&gt;
Call CLRCreateInstance to get ICLRMetaHost/ICLRMetaHostPolicy.&amp;nbsp; If this doesn’t
work, fall back to CorBindToRuntimeEx or similar.&lt;/li&gt;
&lt;li&gt;
Otherwise, party on the interface you just got!&lt;/li&gt;
&lt;/ol&gt;
&lt;img width="0" height="0" src="http://www.marklio.com/marklio/aggbug.ashx?id=302cbf45-8107-4a1b-8839-d0f0ba6b4412" /&gt;</description>
      <comments>http://www.marklio.com/marklio/CommentView,guid,302cbf45-8107-4a1b-8839-d0f0ba6b4412.aspx</comments>
      <category>CLR</category>
      <category>Software Development</category>
    </item>
    <item>
      <trackback:ping>http://www.marklio.com/marklio/Trackback.aspx?guid=ecc34c3c-be44-4422-86b7-900900e451f9</trackback:ping>
      <pingback:server>http://www.marklio.com/marklio/pingback.aspx</pingback:server>
      <pingback:target>http://www.marklio.com/marklio/PermaLink,guid,ecc34c3c-be44-4422-86b7-900900e451f9.aspx</pingback:target>
      <dc:creator>Mark</dc:creator>
      <wfw:comment>http://www.marklio.com/marklio/CommentView,guid,ecc34c3c-be44-4422-86b7-900900e451f9.aspx</wfw:comment>
      <wfw:commentRss>http://www.marklio.com/marklio/SyndicationService.asmx/GetEntryCommentsRss?guid=ecc34c3c-be44-4422-86b7-900900e451f9</wfw:commentRss>
      <slash:comments>2</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
This post is intended to fill a gap in the current MSDN documentation for this attribute
(<a title="http://msdn.microsoft.com/en-us/library/bbx34a2h(VS.100).aspx" href="http://www.marklio.com/marklio/ct.ashx?id=ecc34c3c-be44-4422-86b7-900900e451f9&amp;url=http%3a%2f%2fmsdn.microsoft.com%2fen-us%2flibrary%2fbbx34a2h(VS.100).aspx" target="_blank">http://msdn.microsoft.com/en-us/library/bbx34a2h(VS.100).aspx</a>). 
This gap should be filled by the time .NET 4 ships.
</p>
        <p>
There is alot of confusion about what the useLegacyV2RuntimeActivationPolicy attribute
does.  Most often, it is used to allow a pre-v4 mixed-mode assembly to load in
v4.  In that context, the name makes very little sense.  Below is an explanation
I’ve provided to people internally that explains the attribute in the context for
which it was named.  This should give people a better idea of what it does, as
well as understand some of the subtlies of in-proc SxS.
</p>
        <p>
Ultimately, this attribute has to do with the behavior of the “legacy shim APIs”. 
You can think of these as encompassing several categories of CLR activation: 
</p>
        <ul>
          <li>
CorBindToRuntimeEx and friends - This includes most of the flat exports of mscoree.dll
defined in mscoree.h (GetCORSystemDirectory, GetCORVersion, LoadLibraryShim, etc).
Note, this also includes the strong name APIs defined in strongname.h) 
</li>
          <li>
Pre-v4 COM activation – This includes CoCreateInstance of a CLSID (or type identifier)
whose latest registration is against a pre-v4 runtime version. Note this includes
both the “new” operator on such a co-class from managed code, or the result of Activator.CreateInstance
against a type created by Type.GetTypeFromCLSID on such a CLSID. 
</li>
          <li>
Pre-v4 IJW (mixed mode) activation – For example, calling into a native export on
such an assembly 
</li>
          <li>
Native activation of a native runtime-provided COM CLSID – Such as CoCreateInstance
on ICLRRuntimeHost’s CLSID 
</li>
          <li>
Native activation of a managed framework CLSID – Such as CoCreateInstance on System.ArrayList’s
CLSID (extremely rare)</li>
        </ul>
        <p>
All these have a “single runtime per process” view of the world, so we try to make
those codepaths believe they still exist in that world by “unifying” the version that
they see.  After a given version has been chosen by one of these codepaths, that’s
the version that all of them see for the remainder of the process lifetime. 
Additionally, all of these activation paths had some kind of roll-forward semantics
associated with them.  We “cap” those semantics at v2, meaning by default none
of these codepaths see v4 at all.  This allows us to claim that installing v4
is “non-impactful”.  It should not change the behavior of existing components
when installed. (Note that this has the interesting side-effect of a v4 only machine
appearing to have no runtimes installed at all via these codepaths.)
</p>
        <p>
This is all well and good until someone WANTS those codepaths to see v4.  Rolling
a v2 managed app forward to v4 using a config (without the attribute) works just fine,
unless that app also expects interaction with these “legacy” codepaths to be associated
with the current runtime (v4).  For instance, a p/Invoke to GetCorSystemDirectory
in order to construct a path to Fusion.dll (please don’t do that, BTW) will give you
v2’s fusion.dll.  COM activation of a managed COM object will prefer the runtime
it was built against rather than load into the current runtime (meaning you may be
dealing with interop rather than a concrete CLR type). That may work, and it may not,
depending on what you’re doing. 
</p>
        <p>
The useLegacyV2RuntimeActivationPolicy attribute basically lets you say, “I have some
dependencies on the legacy shim APIs.  Please make them work the way they used
to with respect to the chosen runtime.”  In that context, hopefully the name
makes more sense to you. It is *<b>mostly</b>* equivalent to calling CorBindToRuntimeEx
using the full version string for v4.  We also have a <a href="http://www.marklio.com/marklio/ct.ashx?id=ecc34c3c-be44-4422-86b7-900900e451f9&amp;url=http%3a%2f%2fmsdn.microsoft.com%2fen-us%2flibrary%2fee426955(VS.100).aspx" target="_blank">method
in our new shim APIs</a> to do this programmatically, the difference being that in
a config file, it can be done declaratively, which is useful for a host that uses
config files to determine which runtime to load plugins into. (the attributes value
(or lack of value) is conveyed back to a host via the pdwConfigFlags parameter of <a href="http://www.marklio.com/marklio/ct.ashx?id=ecc34c3c-be44-4422-86b7-900900e451f9&amp;url=http%3a%2f%2fmsdn.microsoft.com%2fen-us%2flibrary%2fdd380612(VS.100).aspx" target="_blank">ICLRMetaHostPolicy::GetRequestedRuntime</a>) 
</p>
        <p>
One of the big reasons people need to do this is if they have a dependency on a pre-v4
IJW assembly.  By default, we can’t allow those to load into v4*.  Putting
this attribute in your config allows this to happen. 
</p>
        <p>
Why don’t we make this the default behavior? You might argue that this behavior is
more compatible, and makes porting code from previous versions much easier. If you’ll
recall, this can’t be the default behavior because it would make installation of v4
impactful, which can break existing apps installed on your machine. 
</p>
        <p>
Well, why don’t we make this the default behavior for v4 managed apps?  Well,
that is precisely the behavior we had for beta 1.  As we started trying to explain
the behavior to people, we found it was very difficult to explain how these legacy
codepaths worked.  We ultimately decided that making the behavior consistent
was better.  The example that ultimately convinced me we had made the right choice
was that the behavior of a library would change based on whether it was hosted by
a native process or a managed one.  That seemed really bad to me. 
</p>
        <p>
You might say, “Why shouldn’t I just set this for every app I have?” Well, the downside
of this attribute is that it turns off in-proc SxS with pre-v4 runtimes.  It
locks them out of the process.  This may not matter to your scenario.  If
you look at some of the runtime tools, they are using this attribute.  Even Visual
Studio uses this attribute.  Don’t just blindly use it though.  If you'
find yourself needing it in something other than a migration aid, or for loading pre-v4
mixed-mode assemblies (which we hope becomes more rare moving forward as people start
updating the interesting mixed-mode binaries out there), I’d like to know about it. 
Leave me a comment! 
</p>
        <p>
Hopefully, you’ve got a better handle on exactly what this attribute means, and can
make a more informed decision about when it is appropriate to use. 
</p>
        <p>
          <em>*There are many engineering challenges around in-proc SxS and IJW assemblies. 
Currently, pre-v4 IJW assemblies can only load into the runtime that is associated
with the “legacy shim APIs”.  But any given IJW assembly (regardless of version)
may only be loaded into a single runtime per process at this time.</em>
        </p>
        <img width="0" height="0" src="http://www.marklio.com/marklio/aggbug.ashx?id=ecc34c3c-be44-4422-86b7-900900e451f9" />
      </body>
      <title>What is useLegacyV2RuntimeActivationPolicy for?</title>
      <guid isPermaLink="false">http://www.marklio.com/marklio/PermaLink,guid,ecc34c3c-be44-4422-86b7-900900e451f9.aspx</guid>
      <link>http://www.marklio.com/marklio/PermaLink,guid,ecc34c3c-be44-4422-86b7-900900e451f9.aspx</link>
      <pubDate>Sat, 13 Mar 2010 03:14:59 GMT</pubDate>
      <description>&lt;p&gt;
This post is intended to fill a gap in the current MSDN documentation for this attribute
(&lt;a title="http://msdn.microsoft.com/en-us/library/bbx34a2h(VS.100).aspx" href="http://www.marklio.com/marklio/ct.ashx?id=ecc34c3c-be44-4422-86b7-900900e451f9&amp;amp;url=http%3a%2f%2fmsdn.microsoft.com%2fen-us%2flibrary%2fbbx34a2h(VS.100).aspx" target="_blank"&gt;http://msdn.microsoft.com/en-us/library/bbx34a2h(VS.100).aspx&lt;/a&gt;).&amp;nbsp;
This gap should be filled by the time .NET 4 ships.
&lt;/p&gt;
&lt;p&gt;
There is alot of confusion about what the useLegacyV2RuntimeActivationPolicy attribute
does.&amp;nbsp; Most often, it is used to allow a pre-v4 mixed-mode assembly to load in
v4.&amp;nbsp; In that context, the name makes very little sense.&amp;nbsp; Below is an explanation
I’ve provided to people internally that explains the attribute in the context for
which it was named.&amp;nbsp; This should give people a better idea of what it does, as
well as understand some of the subtlies of in-proc SxS.
&lt;/p&gt;
&lt;p&gt;
Ultimately, this attribute has to do with the behavior of the “legacy shim APIs”.&amp;nbsp;
You can think of these as encompassing several categories of CLR activation: 
&lt;ul&gt;
&lt;li&gt;
CorBindToRuntimeEx and friends - This includes most of the flat exports of mscoree.dll
defined in mscoree.h (GetCORSystemDirectory, GetCORVersion, LoadLibraryShim, etc).
Note, this also includes the strong name APIs defined in strongname.h) 
&lt;li&gt;
Pre-v4 COM activation – This includes CoCreateInstance of a CLSID (or type identifier)
whose latest registration is against a pre-v4 runtime version. Note this includes
both the “new” operator on such a co-class from managed code, or the result of Activator.CreateInstance
against a type created by Type.GetTypeFromCLSID on such a CLSID. 
&lt;li&gt;
Pre-v4 IJW (mixed mode) activation – For example, calling into a native export on
such an assembly 
&lt;li&gt;
Native activation of a native runtime-provided COM CLSID – Such as CoCreateInstance
on ICLRRuntimeHost’s CLSID 
&lt;li&gt;
Native activation of a managed framework CLSID – Such as CoCreateInstance on System.ArrayList’s
CLSID (extremely rare)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
All these have a “single runtime per process” view of the world, so we try to make
those codepaths believe they still exist in that world by “unifying” the version that
they see.&amp;nbsp; After a given version has been chosen by one of these codepaths, that’s
the version that all of them see for the remainder of the process lifetime.&amp;nbsp;
Additionally, all of these activation paths had some kind of roll-forward semantics
associated with them.&amp;nbsp; We “cap” those semantics at v2, meaning by default none
of these codepaths see v4 at all.&amp;nbsp; This allows us to claim that installing v4
is “non-impactful”.&amp;nbsp; It should not change the behavior of existing components
when installed. (Note that this has the interesting side-effect of a v4 only machine
appearing to have no runtimes installed at all via these codepaths.)
&lt;/p&gt;
&lt;p&gt;
This is all well and good until someone WANTS those codepaths to see v4.&amp;nbsp; Rolling
a v2 managed app forward to v4 using a config (without the attribute) works just fine,
unless that app also expects interaction with these “legacy” codepaths to be associated
with the current runtime (v4).&amp;nbsp; For instance, a p/Invoke to GetCorSystemDirectory
in order to construct a path to Fusion.dll (please don’t do that, BTW) will give you
v2’s fusion.dll.&amp;nbsp; COM activation of a managed COM object will prefer the runtime
it was built against rather than load into the current runtime (meaning you may be
dealing with interop rather than a concrete CLR type). That may work, and it may not,
depending on what you’re doing. 
&lt;p&gt;
The useLegacyV2RuntimeActivationPolicy attribute basically lets you say, “I have some
dependencies on the legacy shim APIs.&amp;nbsp; Please make them work the way they used
to with respect to the chosen runtime.”&amp;nbsp; In that context, hopefully the name
makes more sense to you. It is *&lt;b&gt;mostly&lt;/b&gt;* equivalent to calling CorBindToRuntimeEx
using the full version string for v4.&amp;nbsp; We also have a &lt;a href="http://www.marklio.com/marklio/ct.ashx?id=ecc34c3c-be44-4422-86b7-900900e451f9&amp;amp;url=http%3a%2f%2fmsdn.microsoft.com%2fen-us%2flibrary%2fee426955(VS.100).aspx" target="_blank"&gt;method
in our new shim APIs&lt;/a&gt; to do this programmatically, the difference being that in
a config file, it can be done declaratively, which is useful for a host that uses
config files to determine which runtime to load plugins into. (the attributes value
(or lack of value) is conveyed back to a host via the pdwConfigFlags parameter of &lt;a href="http://www.marklio.com/marklio/ct.ashx?id=ecc34c3c-be44-4422-86b7-900900e451f9&amp;amp;url=http%3a%2f%2fmsdn.microsoft.com%2fen-us%2flibrary%2fdd380612(VS.100).aspx" target="_blank"&gt;ICLRMetaHostPolicy::GetRequestedRuntime&lt;/a&gt;) 
&lt;p&gt;
One of the big reasons people need to do this is if they have a dependency on a pre-v4
IJW assembly.&amp;nbsp; By default, we can’t allow those to load into v4*.&amp;nbsp; Putting
this attribute in your config allows this to happen. 
&lt;p&gt;
Why don’t we make this the default behavior? You might argue that this behavior is
more compatible, and makes porting code from previous versions much easier. If you’ll
recall, this can’t be the default behavior because it would make installation of v4
impactful, which can break existing apps installed on your machine. 
&lt;p&gt;
Well, why don’t we make this the default behavior for v4 managed apps?&amp;nbsp; Well,
that is precisely the behavior we had for beta 1.&amp;nbsp; As we started trying to explain
the behavior to people, we found it was very difficult to explain how these legacy
codepaths worked.&amp;nbsp; We ultimately decided that making the behavior consistent
was better.&amp;nbsp; The example that ultimately convinced me we had made the right choice
was that the behavior of a library would change based on whether it was hosted by
a native process or a managed one.&amp;nbsp; That seemed really bad to me. 
&lt;p&gt;
You might say, “Why shouldn’t I just set this for every app I have?” Well, the downside
of this attribute is that it turns off in-proc SxS with pre-v4 runtimes.&amp;nbsp; It
locks them out of the process.&amp;nbsp; This may not matter to your scenario.&amp;nbsp; If
you look at some of the runtime tools, they are using this attribute.&amp;nbsp; Even Visual
Studio uses this attribute.&amp;nbsp; Don’t just blindly use it though.&amp;nbsp; If you'
find yourself needing it in something other than a migration aid, or for loading pre-v4
mixed-mode assemblies (which we hope becomes more rare moving forward as people start
updating the interesting mixed-mode binaries out there), I’d like to know about it.&amp;nbsp;
Leave me a comment! 
&lt;p&gt;
Hopefully, you’ve got a better handle on exactly what this attribute means, and can
make a more informed decision about when it is appropriate to use. 
&lt;p&gt;
&lt;em&gt;*There are many engineering challenges around in-proc SxS and IJW assemblies.&amp;nbsp;
Currently, pre-v4 IJW assemblies can only load into the runtime that is associated
with the “legacy shim APIs”.&amp;nbsp; But any given IJW assembly (regardless of version)
may only be loaded into a single runtime per process at this time.&lt;/em&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.marklio.com/marklio/aggbug.ashx?id=ecc34c3c-be44-4422-86b7-900900e451f9" /&gt;</description>
      <comments>http://www.marklio.com/marklio/CommentView,guid,ecc34c3c-be44-4422-86b7-900900e451f9.aspx</comments>
      <category>CLR</category>
      <category>Software Development</category>
      <category>Technical</category>
    </item>
    <item>
      <trackback:ping>http://www.marklio.com/marklio/Trackback.aspx?guid=65e9edd3-21c0-4f6d-bcc0-6e9f11144591</trackback:ping>
      <pingback:server>http://www.marklio.com/marklio/pingback.aspx</pingback:server>
      <pingback:target>http://www.marklio.com/marklio/PermaLink,guid,65e9edd3-21c0-4f6d-bcc0-6e9f11144591.aspx</pingback:target>
      <dc:creator>Mark</dc:creator>
      <wfw:comment>http://www.marklio.com/marklio/CommentView,guid,65e9edd3-21c0-4f6d-bcc0-6e9f11144591.aspx</wfw:comment>
      <wfw:commentRss>http://www.marklio.com/marklio/SyndicationService.asmx/GetEntryCommentsRss?guid=65e9edd3-21c0-4f6d-bcc0-6e9f11144591</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
My brother-in-law posted a note on Facebook that was basically one of those silly
things you do and perpetuate across the internet.  I usually don’t take part
in such things, but this one seemed interesting, and I’ve been thinking about ways
to jumpstart my blogging again now that the embargo on all the cool stuff is lifted. 
So, I thought I would do it.  Here are the rules:
</p>
        <ul>
          <li>
Grab the book nearest you. Right now.</li>
          <li>
Turn to page 56.</li>
          <li>
Find the fifth sentence.</li>
          <li>
Post that sentence along with these instructions in a note to your wall. <em>(this
was on Facebook, so it is referring to that wall)</em></li>
          <li>
Don't dig for your favorite book, the coolest, the most intellectual. Use the CLOSEST.</li>
        </ul>
        <p>
So, it took me a while to determine which book was the “closest”, as my position is
roughly normal to the bookshelf in my office.  I finally decided to be honest
and pick the one that was really closest, but I will share another that was very close,
as it is a good segue into future blogs.
</p>
        <p>
The first (and official) one:
</p>
        <blockquote>
          <p>
Semiconductor materials at 0 K have basically the same structure as insulators – a
filled valence band separated from an empty conduction band by a band gap containing
no allowed energy states (Fig. 3-4).
</p>
          <p align="right">
            <em>Solid State Electronic Devices, Ben G. Streetman</em>
          </p>
        </blockquote>
        <p>
The second, and more relevant/interesting one:
</p>
        <blockquote>
          <p>
The shim’s algorithm for picking a version in the COM interoperability scenario is
much more straightforward – the latest version installed on the machine is always
used.
</p>
          <p align="right">
            <em>Customizing the Microsoft .NET Framework Common Language Runtime, Steven Pratschner</em>
          </p>
        </blockquote>
        <p>
What is amazing about this second one is that this is directly related to one of the
features I’ve been working on for CLR v4 (and yes, that is actually the 5th sentence
on page 56).  Namely, this feature is known as “in-process side by side” (or
in-proc SxS for short), and was announced publicly at PDC last month.  This feature
allows you to have more than version of the CLR loaded and running in a single process.
</p>
        <p>
This feature is primarily a compatibility feature, targeted precisely at the behavior
noted in the quote above.  When we use the latest version, we can break existing
COM objects.  Not only because of breaking changes we make (of which the number
is fairly small), but because of other, more subtle behavior dependencies.
</p>
        <p>
Previously, loading a CLR version into the process locked the process to that CLR
version.  Any other policy than “pick the latest” results in a load order dependency
problem that can result in “for sure” breaks because COM components targeting newer
runtimes cannot run on old runtimes.  So, clearly, that was the best choice of
policies.
</p>
        <p>
Now that we support multiple runtimes in the process (v2 and above), we can make a
smarter, more compatible choice about runtime activation.  The precise policies
are still being worked through, so I’ll avoid stating them explicitly, but you can
imagine us being able to make a much better choice about what runtime to activate
to run a given managed COM component.
</p>
        <p>
I’ll be posting more about this feature and it’s implications soon.  Feel free
to seed my future posts with questions in the comments.  Hopefully, this is the
jumpstart I needed.  As for the “game” above, feel free to do it, or ignore it. 
It won’t result in any difference to your luck, financial situation, or anything else.
</p>
        <img width="0" height="0" src="http://www.marklio.com/marklio/aggbug.ashx?id=65e9edd3-21c0-4f6d-bcc0-6e9f11144591" />
      </body>
      <title>Random fun book thing and CLR In-Proc SxS</title>
      <guid isPermaLink="false">http://www.marklio.com/marklio/PermaLink,guid,65e9edd3-21c0-4f6d-bcc0-6e9f11144591.aspx</guid>
      <link>http://www.marklio.com/marklio/PermaLink,guid,65e9edd3-21c0-4f6d-bcc0-6e9f11144591.aspx</link>
      <pubDate>Fri, 21 Nov 2008 01:25:12 GMT</pubDate>
      <description>&lt;p&gt;
My brother-in-law posted a note on Facebook that was basically one of those silly
things you do and perpetuate across the internet.&amp;nbsp; I usually don’t take part
in such things, but this one seemed interesting, and I’ve been thinking about ways
to jumpstart my blogging again now that the embargo on all the cool stuff is lifted.&amp;nbsp;
So, I thought I would do it.&amp;nbsp; Here are the rules:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
Grab the book nearest you. Right now.&lt;/li&gt;
&lt;li&gt;
Turn to page 56.&lt;/li&gt;
&lt;li&gt;
Find the fifth sentence.&lt;/li&gt;
&lt;li&gt;
Post that sentence along with these instructions in a note to your wall. &lt;em&gt;(this
was on Facebook, so it is referring to that wall)&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;
Don't dig for your favorite book, the coolest, the most intellectual. Use the CLOSEST.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
So, it took me a while to determine which book was the “closest”, as my position is
roughly normal to the bookshelf in my office.&amp;nbsp; I finally decided to be honest
and pick the one that was really closest, but I will share another that was very close,
as it is a good segue into future blogs.
&lt;/p&gt;
&lt;p&gt;
The first (and official) one:
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
Semiconductor materials at 0 K have basically the same structure as insulators – a
filled valence band separated from an empty conduction band by a band gap containing
no allowed energy states (Fig. 3-4).
&lt;/p&gt;
&lt;p align="right"&gt;
&lt;em&gt;Solid State Electronic Devices, Ben G. Streetman&lt;/em&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
The second, and more relevant/interesting one:
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
The shim’s algorithm for picking a version in the COM interoperability scenario is
much more straightforward – the latest version installed on the machine is always
used.
&lt;/p&gt;
&lt;p align="right"&gt;
&lt;em&gt;Customizing the Microsoft .NET Framework Common Language Runtime, Steven Pratschner&lt;/em&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
What is amazing about this second one is that this is directly related to one of the
features I’ve been working on for CLR v4 (and yes, that is actually the 5th sentence
on page 56).&amp;nbsp; Namely, this feature is known as “in-process side by side” (or
in-proc SxS for short), and was announced publicly at PDC last month.&amp;nbsp; This feature
allows you to have more than version of the CLR loaded and running in a single process.
&lt;/p&gt;
&lt;p&gt;
This feature is primarily a compatibility feature, targeted precisely at the behavior
noted in the quote above.&amp;nbsp; When we use the latest version, we can break existing
COM objects.&amp;nbsp; Not only because of breaking changes we make (of which the number
is fairly small), but because of other, more subtle behavior dependencies.
&lt;/p&gt;
&lt;p&gt;
Previously, loading a CLR version into the process locked the process to that CLR
version.&amp;nbsp; Any other policy than “pick the latest” results in a load order dependency
problem that can result in “for sure” breaks because COM components targeting newer
runtimes cannot run on old runtimes.&amp;nbsp; So, clearly, that was the best choice of
policies.
&lt;/p&gt;
&lt;p&gt;
Now that we support multiple runtimes in the process (v2 and above), we can make a
smarter, more compatible choice about runtime activation.&amp;nbsp; The precise policies
are still being worked through, so I’ll avoid stating them explicitly, but you can
imagine us being able to make a much better choice about what runtime to activate
to run a given managed COM component.
&lt;/p&gt;
&lt;p&gt;
I’ll be posting more about this feature and it’s implications soon.&amp;nbsp; Feel free
to seed my future posts with questions in the comments.&amp;nbsp; Hopefully, this is the
jumpstart I needed.&amp;nbsp; As for the “game” above, feel free to do it, or ignore it.&amp;nbsp;
It won’t result in any difference to your luck, financial situation, or anything else.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.marklio.com/marklio/aggbug.ashx?id=65e9edd3-21c0-4f6d-bcc0-6e9f11144591" /&gt;</description>
      <comments>http://www.marklio.com/marklio/CommentView,guid,65e9edd3-21c0-4f6d-bcc0-6e9f11144591.aspx</comments>
      <category>CLR</category>
      <category>Fun</category>
      <category>Software Development</category>
      <category>Technical</category>
    </item>
    <item>
      <trackback:ping>http://www.marklio.com/marklio/Trackback.aspx?guid=95211f13-9e8b-4d4c-b013-b67ccd20a041</trackback:ping>
      <pingback:server>http://www.marklio.com/marklio/pingback.aspx</pingback:server>
      <pingback:target>http://www.marklio.com/marklio/PermaLink,guid,95211f13-9e8b-4d4c-b013-b67ccd20a041.aspx</pingback:target>
      <dc:creator>Mark</dc:creator>
      <wfw:comment>http://www.marklio.com/marklio/CommentView,guid,95211f13-9e8b-4d4c-b013-b67ccd20a041.aspx</wfw:comment>
      <wfw:commentRss>http://www.marklio.com/marklio/SyndicationService.asmx/GetEntryCommentsRss?guid=95211f13-9e8b-4d4c-b013-b67ccd20a041</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Why not come work on the Common Language Runtime team at Microsoft?  My team
is a team of SDETs (Software Development Engineer in Test) that works in the really
low levels of the CLR.  We cover areas like assembly loading/fusion, AppDomains,
the shim (mscoree), and interop.
</p>
        <p>
As an SDET, you are in charge of all aspects of quality for the areas you own. This
means you get to be involved in the design process and be literally the first person
developing code using new features coming out of the team.  I've found my last
year and a half here to be extremely satisfying.
</p>
        <p>
This is a pretty exciting time to join the team as we're working on some long-lead
items for the next version of the CLR and we're also busy shipping Silverlight 2 and
.NET Framework 3.5 SP1.
</p>
        <p>
You'll need strong coding, problem-solving, and communication skills.  Some background
on the CLR and managed code is a plus, but not required.  If you think you've
got what it takes to join the team, <a href="http://www.marklio.com/marklio/ct.ashx?id=95211f13-9e8b-4d4c-b013-b67ccd20a041&amp;url=http%3a%2f%2fmembers.microsoft.com%2fcareers%2fsearch%2fdetails.aspx%3fJobID%3d040443C0-A7C4-407B-A0E4-7D6B5E98BCF4" target="_blank">check
out the job details and submit your resume</a>.  I'm happy to answer questions
about the team and the jobs within reason, so feel free to ping me at marklio at [you-know-where].com.
</p>
        <p>
There are several openings in other areas of the team as well.  Feel free to
search the other CLR jobs and find the one that's right for you.
</p>
        <img width="0" height="0" src="http://www.marklio.com/marklio/aggbug.ashx?id=95211f13-9e8b-4d4c-b013-b67ccd20a041" />
      </body>
      <title>My team is hiring. Come work on the CLR team!</title>
      <guid isPermaLink="false">http://www.marklio.com/marklio/PermaLink,guid,95211f13-9e8b-4d4c-b013-b67ccd20a041.aspx</guid>
      <link>http://www.marklio.com/marklio/PermaLink,guid,95211f13-9e8b-4d4c-b013-b67ccd20a041.aspx</link>
      <pubDate>Fri, 09 May 2008 18:40:09 GMT</pubDate>
      <description>&lt;p&gt;
Why not come work on the Common Language Runtime team at Microsoft?&amp;nbsp; My team
is a team of SDETs (Software Development Engineer in Test) that works in the really
low levels of the CLR.&amp;nbsp; We cover areas like assembly loading/fusion, AppDomains,
the shim (mscoree), and interop.
&lt;/p&gt;
&lt;p&gt;
As an SDET, you are in charge of all aspects of quality for the areas you own. This
means you get to be involved in the design process and be literally the first person
developing code using new features coming out of the team.&amp;nbsp; I've found my last
year and a half here to be extremely satisfying.
&lt;/p&gt;
&lt;p&gt;
This is a pretty exciting time to join the team as we're working on some long-lead
items for the next version of the CLR and we're also busy shipping Silverlight 2 and
.NET Framework 3.5 SP1.
&lt;/p&gt;
&lt;p&gt;
You'll need strong coding, problem-solving, and communication skills.&amp;nbsp; Some background
on the CLR and managed code is a plus, but not required.&amp;nbsp; If you think you've
got what it takes to join the team, &lt;a href="http://www.marklio.com/marklio/ct.ashx?id=95211f13-9e8b-4d4c-b013-b67ccd20a041&amp;amp;url=http%3a%2f%2fmembers.microsoft.com%2fcareers%2fsearch%2fdetails.aspx%3fJobID%3d040443C0-A7C4-407B-A0E4-7D6B5E98BCF4" target="_blank"&gt;check
out the job details and submit your resume&lt;/a&gt;.&amp;nbsp; I'm happy to answer questions
about the team and the jobs within reason, so feel free to ping me at marklio at [you-know-where].com.
&lt;/p&gt;
&lt;p&gt;
There are several openings in other areas of the team as well.&amp;nbsp; Feel free to
search the other CLR jobs and find the one that's right for you.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.marklio.com/marklio/aggbug.ashx?id=95211f13-9e8b-4d4c-b013-b67ccd20a041" /&gt;</description>
      <comments>http://www.marklio.com/marklio/CommentView,guid,95211f13-9e8b-4d4c-b013-b67ccd20a041.aspx</comments>
      <category>Announcements</category>
      <category>CLR</category>
      <category>News</category>
    </item>
    <item>
      <trackback:ping>http://www.marklio.com/marklio/Trackback.aspx?guid=498e2e35-1590-4852-92e1-2212157c68f1</trackback:ping>
      <pingback:server>http://www.marklio.com/marklio/pingback.aspx</pingback:server>
      <pingback:target>http://www.marklio.com/marklio/PermaLink,guid,498e2e35-1590-4852-92e1-2212157c68f1.aspx</pingback:target>
      <dc:creator>Mark</dc:creator>
      <wfw:comment>http://www.marklio.com/marklio/CommentView,guid,498e2e35-1590-4852-92e1-2212157c68f1.aspx</wfw:comment>
      <wfw:commentRss>http://www.marklio.com/marklio/SyndicationService.asmx/GetEntryCommentsRss?guid=498e2e35-1590-4852-92e1-2212157c68f1</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I've been doing some app-building with Silverlight lately and exploring the limitations
of the platform in comparison to the full desktop CLR and what that means for the
Silverlight "ecosystem".  Those limitations can be summed up with 3 items:
</p>
        <ol>
          <li>
Strict sandbox security model</li>
          <li>
Reduced managed framework surface area</li>
          <li>
No binary compatibility with libraries targeting the desktop CLR (eliminates the number
of 3rd party components you can leverage).</li>
        </ol>
        <p>
I think #3 will begin to become a non-issue as more component providers provide builds
for Silverlight. #2 can be broken down into 2 areas:
</p>
        <ul>
          <li>
Full technology areas that are unavailable on Silverlight (LinqToSql, WinForms, etc)</li>
          <li>
Reduced/pruned APIs</li>
        </ul>
        <p>
The second is where my interest lies, and intersects with #1 (the new security model). 
We've done extensive threat modeling against our APIs as well as automated tooling
that has either removed (or made internal) certain APIs, or marked them as SecurityCritical,
meaning they cannot be called directly from "user" code.
</p>
        <p>
In addition, the security model requires safe, verifiable user code.  Normally
when developing on the desktop CLR, unless you are specifically targeting a low trust
environment, you can do whatever you like.
</p>
        <p>
So, for this exercise, I pulled out my trusty STDF parser (blogged <a href="http://www.marklio.com/marklio/ct.ashx?id=498e2e35-1590-4852-92e1-2212157c68f1&amp;url=http%3a%2f%2fwww.marklio.com%2fmarklio%2fPermaLink%2cguid%2ce3601f31-231b-4a82-b5a7-1c31600ce25c.aspx" target="_blank">here</a> and <a href="http://www.marklio.com/marklio/ct.ashx?id=498e2e35-1590-4852-92e1-2212157c68f1&amp;url=http%3a%2f%2fwww.marklio.com%2fmarklio%2fPermaLink%2cguid%2c3a89ecce-89a8-470e-a894-bc5cc86ce41d.aspx" target="_blank">here</a>). 
I've used this project as a test vehicle for both v2.0 and Orcas, and it's served
well as a project that leverages a large cross-section of features in the Framework,
from high-level stuff like Linq down to expression tree inspection and further down
to LCG/RefEmit.
</p>
        <p>
In short, I was able to get the parser working without too much trouble. I felt like
creating a source bed that intended to target both Silverlight and the Desktop should
be an attainable goal.  You just have to switch your mindset from binary compatibility
to source compatibility.
</p>
        <p>
I combated the reduced surface area with extension methods, which worked quite well
to centralize the "overload shims" that needed to be "Silverlight-only", as well as
for a general refactoring tool.  My goal is to have all the desktop vs. Silverlight
differences centralized into files that are either included or excluded from the build
depending on which platform I'm targeting.  I wish you could create extension
properties.  That would let me close all the surface area discrepancies that
aren't caused by missing/irrelevant technology areas.
</p>
        <p>
I was pleased that 99% of my LCG codegen stuff "just worked".  I make heavy use
of Reflection.Emit via DynamicMethod to generate my record parsers based on attributes
on the record classes.  The 2 problems I ran into were:
</p>
        <ul>
          <li>
Visibility restrictions - The new security model won't let my dynamic methods see
internals.  I had used this ability to keep the API clean.  I'm still figuring
out the best approach, but it was simple enough just to expose those methods.</li>
          <li>
Verifiability - I had a few places where I was generating unverifiable code. 
Some of these were my own codegen bugs, but others were just bad assumptions on my
part.</li>
        </ul>
        <p>
This brings me to <a href="http://www.marklio.com/marklio/ct.ashx?id=498e2e35-1590-4852-92e1-2212157c68f1&amp;url=http%3a%2f%2fmsdn2.microsoft.com%2fen-us%2flibrary%2fsystem.reflection.emit.opcodes.constrained.aspx" target="_blank">constrained
callvirt</a>, an interesting little IL tidbit I discovered in the porting process. 
Calling conventions are subtly different between reference types and value types,
and it also depends on whether the given method is actually overridden in the value
type (which can lead to confusing breaks when that state changes).  In the v1.x
days, you always knew the type you were dealing with, so it didn't matter that much
and you could generally always create the right sequence of IL to make a call. 
V2.0 introduced generics, which meant that you couldn't emit unified IL for both the
possibility of reference types or value types.  This meant there needed to be
a way to unify the IL for the 2 cases.  This is where the constrained prefix
came in.  It allows you to write unified IL that works regardless of whether
you're working with a value type or a reference type (think generics constrained by
an interface, or calling a method defined on System.Object like ToString()).
</p>
        <p>
Anyway, I was able to fix my unverifiable code by utilizing the constrained prefix. 
It also simplified my codegen logic significantly in a number of places where I had
different paths based on whether I was working with a value type or not.
</p>
        <p>
All in all, I was pleased with the results.  I'll be posting a sample Silverlight
app using the library when I get some UI stuff figured out.
</p>
        <img width="0" height="0" src="http://www.marklio.com/marklio/aggbug.ashx?id=498e2e35-1590-4852-92e1-2212157c68f1" />
      </body>
      <title>Silverlight limitations and Constrained Callvirt in IL</title>
      <guid isPermaLink="false">http://www.marklio.com/marklio/PermaLink,guid,498e2e35-1590-4852-92e1-2212157c68f1.aspx</guid>
      <link>http://www.marklio.com/marklio/PermaLink,guid,498e2e35-1590-4852-92e1-2212157c68f1.aspx</link>
      <pubDate>Wed, 23 Apr 2008 20:43:46 GMT</pubDate>
      <description>&lt;p&gt;
I've been doing some app-building with Silverlight lately and exploring the limitations
of the platform in comparison to the full desktop CLR and what that means for the
Silverlight "ecosystem".&amp;nbsp; Those limitations can be summed up with 3 items:
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
Strict sandbox security model&lt;/li&gt;
&lt;li&gt;
Reduced managed framework surface area&lt;/li&gt;
&lt;li&gt;
No binary compatibility with libraries targeting the desktop CLR (eliminates the number
of 3rd party components you can leverage).&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
I think #3 will begin to become a non-issue as more component providers provide builds
for Silverlight. #2 can be broken down into 2 areas:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
Full technology areas that are unavailable on Silverlight (LinqToSql, WinForms, etc)&lt;/li&gt;
&lt;li&gt;
Reduced/pruned APIs&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
The second is where my interest lies, and intersects with #1 (the new security model).&amp;nbsp;
We've done extensive threat modeling against our APIs as well as automated tooling
that has either removed (or made internal) certain APIs, or marked them as SecurityCritical,
meaning they cannot be called directly from "user" code.
&lt;/p&gt;
&lt;p&gt;
In addition, the security model requires safe, verifiable user code.&amp;nbsp; Normally
when developing on the desktop CLR, unless you are specifically targeting a low trust
environment, you can do whatever you like.
&lt;/p&gt;
&lt;p&gt;
So, for this exercise, I pulled out my trusty STDF parser (blogged &lt;a href="http://www.marklio.com/marklio/ct.ashx?id=498e2e35-1590-4852-92e1-2212157c68f1&amp;amp;url=http%3a%2f%2fwww.marklio.com%2fmarklio%2fPermaLink%2cguid%2ce3601f31-231b-4a82-b5a7-1c31600ce25c.aspx" target="_blank"&gt;here&lt;/a&gt; and &lt;a href="http://www.marklio.com/marklio/ct.ashx?id=498e2e35-1590-4852-92e1-2212157c68f1&amp;amp;url=http%3a%2f%2fwww.marklio.com%2fmarklio%2fPermaLink%2cguid%2c3a89ecce-89a8-470e-a894-bc5cc86ce41d.aspx" target="_blank"&gt;here&lt;/a&gt;).&amp;nbsp;
I've used this project as a test vehicle for both v2.0 and Orcas, and it's served
well as a project that leverages a large cross-section of features in the Framework,
from high-level stuff like Linq down to expression tree inspection and further down
to LCG/RefEmit.
&lt;/p&gt;
&lt;p&gt;
In short, I was able to get the parser working without too much trouble. I felt like
creating a source bed that intended to target both Silverlight and the Desktop should
be an attainable goal.&amp;nbsp; You just have to switch your mindset from binary compatibility
to source compatibility.
&lt;/p&gt;
&lt;p&gt;
I combated the reduced surface area with extension methods, which worked quite well
to centralize the "overload shims" that needed to be "Silverlight-only", as well as
for a general refactoring tool.&amp;nbsp; My goal is to have all the desktop vs. Silverlight
differences centralized into files that are either included or excluded from the build
depending on which platform I'm targeting.&amp;nbsp; I wish you could create extension
properties.&amp;nbsp; That would let me close all the surface area discrepancies that
aren't caused by missing/irrelevant technology areas.
&lt;/p&gt;
&lt;p&gt;
I was pleased that 99% of my LCG codegen stuff "just worked".&amp;nbsp; I make heavy use
of Reflection.Emit via DynamicMethod to generate my record parsers based on attributes
on the record classes.&amp;nbsp; The 2 problems I ran into were:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
Visibility restrictions - The new security model won't let my dynamic methods see
internals.&amp;nbsp; I had used this ability to keep the API clean.&amp;nbsp; I'm still figuring
out the best approach, but it was simple enough just to expose those methods.&lt;/li&gt;
&lt;li&gt;
Verifiability - I had a few places where I was generating unverifiable code.&amp;nbsp;
Some of these were my own codegen bugs, but others were just bad assumptions on my
part.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
This brings me to &lt;a href="http://www.marklio.com/marklio/ct.ashx?id=498e2e35-1590-4852-92e1-2212157c68f1&amp;amp;url=http%3a%2f%2fmsdn2.microsoft.com%2fen-us%2flibrary%2fsystem.reflection.emit.opcodes.constrained.aspx" target="_blank"&gt;constrained
callvirt&lt;/a&gt;, an interesting little IL tidbit I discovered in the porting process.&amp;nbsp;
Calling conventions are subtly different between reference types and value types,
and it also depends on whether the given method is actually overridden in the value
type (which can lead to confusing breaks when that state changes).&amp;nbsp; In the v1.x
days, you always knew the type you were dealing with, so it didn't matter that much
and you could generally always create the right sequence of IL to make a call.&amp;nbsp;
V2.0 introduced generics, which meant that you couldn't emit unified IL for both the
possibility of reference types or value types.&amp;nbsp; This meant there needed to be
a way to unify the IL for the 2 cases.&amp;nbsp; This is where the constrained prefix
came in.&amp;nbsp; It allows you to write unified IL that works regardless of whether
you're working with a value type or a reference type (think generics constrained by
an interface, or calling a method defined on System.Object like ToString()).
&lt;/p&gt;
&lt;p&gt;
Anyway, I was able to fix my unverifiable code by utilizing the constrained prefix.&amp;nbsp;
It also simplified my codegen logic significantly in a number of places where I had
different paths based on whether I was working with a value type or not.
&lt;/p&gt;
&lt;p&gt;
All in all, I was pleased with the results.&amp;nbsp; I'll be posting a sample Silverlight
app using the library when I get some UI stuff figured out.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.marklio.com/marklio/aggbug.ashx?id=498e2e35-1590-4852-92e1-2212157c68f1" /&gt;</description>
      <comments>http://www.marklio.com/marklio/CommentView,guid,498e2e35-1590-4852-92e1-2212157c68f1.aspx</comments>
      <category>CLR</category>
      <category>Fun</category>
      <category>Software Development</category>
      <category>Technical</category>
    </item>
    <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=1fa8a82b-a6d6-4fbb-8cca-5e352ff3c9e9</trackback:ping>
      <pingback:server>http://www.marklio.com/marklio/pingback.aspx</pingback:server>
      <pingback:target>http://www.marklio.com/marklio/PermaLink,guid,1fa8a82b-a6d6-4fbb-8cca-5e352ff3c9e9.aspx</pingback:target>
      <dc:creator>Mark</dc:creator>
      <wfw:comment>http://www.marklio.com/marklio/CommentView,guid,1fa8a82b-a6d6-4fbb-8cca-5e352ff3c9e9.aspx</wfw:comment>
      <wfw:commentRss>http://www.marklio.com/marklio/SyndicationService.asmx/GetEntryCommentsRss?guid=1fa8a82b-a6d6-4fbb-8cca-5e352ff3c9e9</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
          <em>This is one of those entries that attempts to fill a void in online search for
a particular topic.  I ramble on for a while to give enough context so that a
search engine can match it up in a relevant manner.</em>
        </p>
        <p>
I was debugging something the other day, and thought I had come across a heinous bug
in the CLR.  Turns out, everything was working fine and the bug was in the app.
</p>
        <p>
A program was crashing and it was a managed exception, so I attached to it with VS2008
and dug in.  The first thing I noted was that this was a retail build, so <a href="http://www.marklio.com/marklio/ct.ashx?id=1fa8a82b-a6d6-4fbb-8cca-5e352ff3c9e9&amp;url=http%3a%2f%2fwww.hanselman.com%2fblog%2fReleaseISNOTDebug64bitOptimizationsAndCMethodInliningInReleaseBuildCallStacks.aspx" target="_blank">my
stack was collapsed in a number of places</a>... expected, keep moving.  Then,
I noted an interesting frame on my stack (this is a contrived example, not the actual
thing I saw):
</p>
        <blockquote>
          <p>
&gt;    CanonTest.exe!CanonTest.GenericType&lt;<font color="#ff0000"><strong>System.__Canon</strong></font>,int&gt;.SomeOperation()
+ 0x55 bytes    
</p>
        </blockquote>
        <p>
That's weird, what is System.__Canon and what's it doing here? Surely this must be
a horrible CLR bug where my method tables are being corrupted!  Internet searches
seem to be confirming my thoughts.  A few others with random __Canon's showing
up on the stack do look like bugs.  After a bit more reasoning, I come to the
conclusion that System.__Canon must be special in some way since it follows the pattern
for such things...  marked internal, pre-pended with double-underscore.
</p>
        <p>
A few emails later, I had my answer.  System.__Canon is the special type that
is used to identify "canonical" generic type instantiations. It typically only shows
up in release builds, so you don't normally see it on the stack while debugging. 
So, it's easy to assume something's wrong when you do see it.
</p>
        <p>
If you'll recall, one of the really cool things about the Generics implementation
is that it allows for code sharing.  Jitted methods can be shared between compatible
type instantiations.  For instance, the code for SomeGenericType&lt;string&gt;
can likely be shared with SomeGenericType&lt;Foo&gt; (where Foo is another reference
type. We don't currently share code between value types, so you'll continue to see
those on the stack such as in the example).  That shared code needs to live somewhere,
so we have the concept of a "canonical" generic type instantiation that is identified
by __Canon as the type parameter.
</p>
        <p>
Also, in alot of debugger stack representations you'll get the GenericType`2 form
rather than expanded form.  In that case, you never see __Canon.
</p>
        <p>
For some more info on Generics and code sharing, see <a href="http://www.marklio.com/marklio/ct.ashx?id=1fa8a82b-a6d6-4fbb-8cca-5e352ff3c9e9&amp;url=http%3a%2f%2fblogs.msdn.com%2fjoelpob%2farchive%2f2004%2f11%2f17%2f259224.aspx" target="_blank">Joel
Pobar's excellent blog entry on the subject</a>.
</p>
        <img width="0" height="0" src="http://www.marklio.com/marklio/aggbug.ashx?id=1fa8a82b-a6d6-4fbb-8cca-5e352ff3c9e9" />
      </body>
      <title>What is System.__Canon and why is it on my stack?</title>
      <guid isPermaLink="false">http://www.marklio.com/marklio/PermaLink,guid,1fa8a82b-a6d6-4fbb-8cca-5e352ff3c9e9.aspx</guid>
      <link>http://www.marklio.com/marklio/PermaLink,guid,1fa8a82b-a6d6-4fbb-8cca-5e352ff3c9e9.aspx</link>
      <pubDate>Tue, 12 Feb 2008 19:56:35 GMT</pubDate>
      <description>&lt;p&gt;
&lt;em&gt;This is one of those entries that attempts to fill a void in online search for
a particular topic.&amp;nbsp; I ramble on for a while to give enough context so that a
search engine can match it up in a relevant manner.&lt;/em&gt;
&lt;/p&gt;
&lt;p&gt;
I was debugging something the other day, and thought I had come across a heinous bug
in the CLR.&amp;nbsp; Turns out, everything was working fine and the bug was in the app.
&lt;/p&gt;
&lt;p&gt;
A program was crashing and it was a managed exception, so I attached to it with VS2008
and dug in.&amp;nbsp; The first thing I noted was that this was a retail build, so &lt;a href="http://www.marklio.com/marklio/ct.ashx?id=1fa8a82b-a6d6-4fbb-8cca-5e352ff3c9e9&amp;amp;url=http%3a%2f%2fwww.hanselman.com%2fblog%2fReleaseISNOTDebug64bitOptimizationsAndCMethodInliningInReleaseBuildCallStacks.aspx" target="_blank"&gt;my
stack was collapsed in a number of places&lt;/a&gt;... expected, keep moving.&amp;nbsp; Then,
I noted an interesting frame on my stack (this is a contrived example, not the actual
thing I saw):
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
&amp;gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; CanonTest.exe!CanonTest.GenericType&amp;lt;&lt;font color="#ff0000"&gt;&lt;strong&gt;System.__Canon&lt;/strong&gt;&lt;/font&gt;,int&amp;gt;.SomeOperation()
+ 0x55 bytes&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
That's weird, what is System.__Canon and what's it doing here? Surely this must be
a horrible CLR bug where my method tables are being corrupted!&amp;nbsp; Internet searches
seem to be confirming my thoughts.&amp;nbsp; A few others with random __Canon's showing
up on the stack do look like bugs.&amp;nbsp; After a bit more reasoning, I come to the
conclusion that System.__Canon must be special in some way since it follows the pattern
for such things...&amp;nbsp; marked internal, pre-pended with double-underscore.
&lt;/p&gt;
&lt;p&gt;
A few emails later, I had my answer.&amp;nbsp; System.__Canon is the special type that
is used to identify "canonical" generic type instantiations. It typically only shows
up in release builds, so you don't normally see it on the stack while debugging.&amp;nbsp;
So, it's easy to assume something's wrong when you do see it.
&lt;/p&gt;
&lt;p&gt;
If you'll recall, one of the really cool things about the Generics implementation
is that it allows for code sharing.&amp;nbsp; Jitted methods can be shared between compatible
type instantiations.&amp;nbsp; For instance, the code for SomeGenericType&amp;lt;string&amp;gt;
can likely be shared with SomeGenericType&amp;lt;Foo&amp;gt; (where Foo is another reference
type. We don't currently share code between value types, so you'll continue to see
those on the stack such as in the example).&amp;nbsp; That shared code needs to live somewhere,
so we have the concept of a "canonical" generic type instantiation that is identified
by __Canon as the type parameter.
&lt;/p&gt;
&lt;p&gt;
Also, in alot of debugger stack representations you'll get the GenericType`2 form
rather than expanded form.&amp;nbsp; In that case, you never see __Canon.
&lt;/p&gt;
&lt;p&gt;
For some more info on Generics and code sharing, see &lt;a href="http://www.marklio.com/marklio/ct.ashx?id=1fa8a82b-a6d6-4fbb-8cca-5e352ff3c9e9&amp;amp;url=http%3a%2f%2fblogs.msdn.com%2fjoelpob%2farchive%2f2004%2f11%2f17%2f259224.aspx" target="_blank"&gt;Joel
Pobar's excellent blog entry on the subject&lt;/a&gt;.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.marklio.com/marklio/aggbug.ashx?id=1fa8a82b-a6d6-4fbb-8cca-5e352ff3c9e9" /&gt;</description>
      <comments>http://www.marklio.com/marklio/CommentView,guid,1fa8a82b-a6d6-4fbb-8cca-5e352ff3c9e9.aspx</comments>
      <category>CLR</category>
      <category>Software Development</category>
      <category>Technical</category>
    </item>
    <item>
      <trackback:ping>http://www.marklio.com/marklio/Trackback.aspx?guid=15185490-9b48-4e78-abd3-197862bf2554</trackback:ping>
      <pingback:server>http://www.marklio.com/marklio/pingback.aspx</pingback:server>
      <pingback:target>http://www.marklio.com/marklio/PermaLink,guid,15185490-9b48-4e78-abd3-197862bf2554.aspx</pingback:target>
      <dc:creator>Mark</dc:creator>
      <wfw:comment>http://www.marklio.com/marklio/CommentView,guid,15185490-9b48-4e78-abd3-197862bf2554.aspx</wfw:comment>
      <wfw:commentRss>http://www.marklio.com/marklio/SyndicationService.asmx/GetEntryCommentsRss?guid=15185490-9b48-4e78-abd3-197862bf2554</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
          <em>I wish I had time to come up with more concrete information (examples/code) in
this post, but I don't have the time to work that stuff up.  I did think it would
be useful for people searching for solutions to this problem, so here it is in all
its ambiguity.</em>
        </p>
        <p>
I was playing with expression tree inspection and dynamic interpretation the other
day, when I hit something that I was sure was a bug.  I was inspecting an expression
tree, identifying "branches" of interest, and generating lamdba expressions from them
on the fly.
</p>
        <p>
You might do this to break up an expression into separate units of execution to spread
across multiple processors (a la PLINQ), or to replace parts of a tree requiring local
execution before passing off to another layer to be transformed into another domain
like SQL, or whatever.  In any case, I was doing it.
</p>
        <p>
I found that if the type of the expression was a value type, I could not create a
lambda expression returning object from it, even though there is an inheritance relationship. 
You get a fairly straightforward, but perhaps surprising exception.
</p>
        <p>
After some back and forth with the Linq team, I discovered that this was by design. 
In the case of value types, the boxing operation required to make an object must be
represented by a unary convert expression. <strong>The solution is to wrap such expression
trees with a call to Expression.Convert(expression, typeof(object)).</strong></p>
        <img width="0" height="0" src="http://www.marklio.com/marklio/aggbug.ashx?id=15185490-9b48-4e78-abd3-197862bf2554" />
      </body>
      <title>Expression Tree Pruning, Value Types, and the one true Object</title>
      <guid isPermaLink="false">http://www.marklio.com/marklio/PermaLink,guid,15185490-9b48-4e78-abd3-197862bf2554.aspx</guid>
      <link>http://www.marklio.com/marklio/PermaLink,guid,15185490-9b48-4e78-abd3-197862bf2554.aspx</link>
      <pubDate>Thu, 30 Aug 2007 00:17:11 GMT</pubDate>
      <description>&lt;p&gt;
&lt;em&gt;I wish I had time to come up with more concrete information (examples/code) in
this post, but I don't have the time to work that stuff up.&amp;nbsp; I did think it would
be useful for people searching for solutions to this problem, so here it is in all
its ambiguity.&lt;/em&gt;
&lt;/p&gt;
&lt;p&gt;
I was playing with expression tree inspection and dynamic interpretation the other
day, when I hit something that I was sure was a bug.&amp;nbsp; I was inspecting an expression
tree, identifying "branches" of interest, and generating lamdba expressions from them
on the fly.
&lt;/p&gt;
&lt;p&gt;
You might do this to break up an expression into separate units of execution to spread
across multiple processors (a la PLINQ), or to replace parts of a tree requiring local
execution before passing off to another layer to be transformed into another domain
like SQL, or whatever.&amp;nbsp; In any case, I was doing it.
&lt;/p&gt;
&lt;p&gt;
I found that if the type of the expression was a value type, I could not create a
lambda expression returning object from it, even though there is an inheritance relationship.&amp;nbsp;
You get a fairly straightforward, but perhaps surprising exception.
&lt;/p&gt;
&lt;p&gt;
After some back and forth with the Linq team, I discovered that this was by design.&amp;nbsp;
In the case of value types, the boxing operation required to make an object must be
represented by a unary convert expression. &lt;strong&gt;The solution is to wrap such expression
trees with a call to Expression.Convert(expression, typeof(object)).&lt;/strong&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.marklio.com/marklio/aggbug.ashx?id=15185490-9b48-4e78-abd3-197862bf2554" /&gt;</description>
      <comments>http://www.marklio.com/marklio/CommentView,guid,15185490-9b48-4e78-abd3-197862bf2554.aspx</comments>
      <category>CLR</category>
      <category>Fun</category>
      <category>Technical</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=f246bcec-5a5c-4869-b9c1-671d8f0daa4c</trackback:ping>
      <pingback:server>http://www.marklio.com/marklio/pingback.aspx</pingback:server>
      <pingback:target>http://www.marklio.com/marklio/PermaLink,guid,f246bcec-5a5c-4869-b9c1-671d8f0daa4c.aspx</pingback:target>
      <dc:creator>Mark</dc:creator>
      <wfw:comment>http://www.marklio.com/marklio/CommentView,guid,f246bcec-5a5c-4869-b9c1-671d8f0daa4c.aspx</wfw:comment>
      <wfw:commentRss>http://www.marklio.com/marklio/SyndicationService.asmx/GetEntryCommentsRss?guid=f246bcec-5a5c-4869-b9c1-671d8f0daa4c</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I've been toying with <a href="http://www.marklio.com/marklio/ct.ashx?id=f246bcec-5a5c-4869-b9c1-671d8f0daa4c&amp;url=http%3a%2f%2fweblogs.asp.net%2fscottgu%2farchive%2f2007%2f03%2f13%2fnew-orcas-language-feature-extension-methods.aspx" target="_blank">Orcas
extension methods</a> recently, and I came across a situation that could be problematic. 
The problem involves value types.  Generally, the advice to people is to always
make value types immutable.  That is, once a type is constructed, it's state
cannot be changed. There are a number of reasons behind this, and now there's one
more.
</p>
        <p>
Extension methods allow you to make a static method look like an instance method on
another class via the "this" keyword on the first parameter.  The compiler will
then use that method if it is in scope when resolving methods in code. So, the first
parameter to the method behaves roughly like the "this" pointer.  However,
there is a subtle difference when extending value types in this way.
</p>
        <p>
In a regular instance method, the "this" parameter is passed by reference, even for
a value type. (In IL, you load an address onto the stack rather than the instance) 
This allows you to change the state of the object within the method.  However,
for extension methods, the first parameter is passed by value.
</p>
        <p>
          <strong>So, if you attempt to make changes to the state of a value type in an extension
method on that type, the changes won't be reflected after the method completes.</strong>
        </p>
        <p>
Like most value type problems, this is because you've made changes to a copy of your
instance rather than the "original".  So, keep you value types immutable, or
be aware of all the various gotchas of mutable value types.
</p>
        <img width="0" height="0" src="http://www.marklio.com/marklio/aggbug.ashx?id=f246bcec-5a5c-4869-b9c1-671d8f0daa4c" />
      </body>
      <title>Extension Methods Gotcha</title>
      <guid isPermaLink="false">http://www.marklio.com/marklio/PermaLink,guid,f246bcec-5a5c-4869-b9c1-671d8f0daa4c.aspx</guid>
      <link>http://www.marklio.com/marklio/PermaLink,guid,f246bcec-5a5c-4869-b9c1-671d8f0daa4c.aspx</link>
      <pubDate>Wed, 21 Mar 2007 00:22:25 GMT</pubDate>
      <description>&lt;p&gt;
I've been toying with &lt;a href="http://www.marklio.com/marklio/ct.ashx?id=f246bcec-5a5c-4869-b9c1-671d8f0daa4c&amp;amp;url=http%3a%2f%2fweblogs.asp.net%2fscottgu%2farchive%2f2007%2f03%2f13%2fnew-orcas-language-feature-extension-methods.aspx" target=_blank&gt;Orcas
extension methods&lt;/a&gt; recently, and I came across a situation that could be problematic.&amp;nbsp;
The problem involves value types.&amp;nbsp; Generally, the advice to people is to always
make value types immutable.&amp;nbsp; That is, once a type is constructed, it's state
cannot be changed. There are a number of reasons behind this, and now there's one
more.
&lt;/p&gt;
&lt;p&gt;
Extension methods allow you to make a static method look like an instance method on
another class via the "this" keyword on the first parameter.&amp;nbsp; The compiler will
then use that method if it is in scope when resolving methods in code. So, the first
parameter to the method behaves roughly&amp;nbsp;like the "this" pointer.&amp;nbsp; However,
there is a subtle difference when extending value types in this way.
&lt;/p&gt;
&lt;p&gt;
In a regular instance method, the "this" parameter is passed by reference, even for
a value type. (In IL, you load an address onto the stack rather than the instance)&amp;nbsp;
This allows you to change the state of the object within the method.&amp;nbsp; However,
for extension methods, the first parameter is passed by value.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;So, if you attempt to make changes to the state of a value type in an extension
method on that type, the changes won't be reflected after the method completes.&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
Like most value type problems, this is because you've made changes to a copy of your
instance rather than the "original".&amp;nbsp; So, keep you value types immutable, or
be aware of all the various gotchas of mutable value types.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.marklio.com/marklio/aggbug.ashx?id=f246bcec-5a5c-4869-b9c1-671d8f0daa4c" /&gt;</description>
      <comments>http://www.marklio.com/marklio/CommentView,guid,f246bcec-5a5c-4869-b9c1-671d8f0daa4c.aspx</comments>
      <category>CLR</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>
    <item>
      <trackback:ping>http://www.marklio.com/marklio/Trackback.aspx?guid=077f85a7-d422-403d-8e12-3752fc037dd3</trackback:ping>
      <pingback:server>http://www.marklio.com/marklio/pingback.aspx</pingback:server>
      <pingback:target>http://www.marklio.com/marklio/PermaLink,guid,077f85a7-d422-403d-8e12-3752fc037dd3.aspx</pingback:target>
      <dc:creator>Mark</dc:creator>
      <wfw:comment>http://www.marklio.com/marklio/CommentView,guid,077f85a7-d422-403d-8e12-3752fc037dd3.aspx</wfw:comment>
      <wfw:commentRss>http://www.marklio.com/marklio/SyndicationService.asmx/GetEntryCommentsRss?guid=077f85a7-d422-403d-8e12-3752fc037dd3</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I dealt with several situations in the past months where the crux of the problem was
confusion over assemblyname and filename.  Let's define what we're talking about:
</p>
        <ul>
          <li>
Filename - The name of a file in the filesystem, such as System.dll</li>
          <li>
Assemblyname - The name given to an assembly to establish its identity.  In this
case, we'll only concern ourselves with the "simple" name. such as System</li>
        </ul>
        <p>
Usually, any confusion that arises between the two can be resolved by reminding people
that a filesystem is just one of the places you can get an assembly from.  For
instance there are APIs for getting assemblies from byte arrays.
</p>
        <p>
For those that still do see it... In the managed world, the assemblyname gives identity
to the code that resides in the assembly.  If you have 2 assemblies with the
same assemblyname, you expect them to represent the same identity (perhaps different
versions, build flavors, bitness, etc.).  If we relied on the filesystem name,
the identity of the code could change just by changing the filename.  That's
not the semantics we expect.
</p>
        <p>
So, why does the filename matter?  Why do we recommend keeping them the same? 
Some of the reasons are simple convenience.  It's nice to look at a file and
know what it is without cracking it open.  If the names are different, it's like
me going to a party and wearing a nametag that says, "Peter".  While there is
nothing keeping me from doing it, it causes confusion.  However, another more
important reason to keep them the same is that assemblies are rarely loaded by filename. 
References and most dynamic loads are done by assemblyname.  You don't take a
reference to System.dll, you take a reference to System.  At some point, the
loader has to find an appropriate file to load to satisfy that reference.  If
System's filename is Peter.dll, then it's going to have a difficult time finding it
to load.  This is actually the very reason that gacutil will not let you install
an assembly into the global assembly cache if the filename doesn't match the assemblyname. 
However, I think it's silly that it doesn't just fix the name for you.
</p>
        <p>
What about multi-module assemblies?  Well, it's the module with the assembly
manifest that matters.  It's the one that should match.  Then the rest of
the files need to match the assembly manifest :). But, if you're using multi-module
assemblies, let me know.  I'd like to know why.
</p>
        <img width="0" height="0" src="http://www.marklio.com/marklio/aggbug.ashx?id=077f85a7-d422-403d-8e12-3752fc037dd3" />
      </body>
      <title>Assemblyname vs. Filename</title>
      <guid isPermaLink="false">http://www.marklio.com/marklio/PermaLink,guid,077f85a7-d422-403d-8e12-3752fc037dd3.aspx</guid>
      <link>http://www.marklio.com/marklio/PermaLink,guid,077f85a7-d422-403d-8e12-3752fc037dd3.aspx</link>
      <pubDate>Mon, 29 Jan 2007 20:46:56 GMT</pubDate>
      <description>&lt;p&gt;
I dealt with several situations in the past months where the crux of the problem was
confusion over assemblyname and filename.&amp;nbsp; Let's define what we're talking about:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
Filename - The name of a file in the filesystem, such as System.dll&lt;/li&gt;
&lt;li&gt;
Assemblyname - The name given to an assembly to establish its identity.&amp;nbsp; In this
case, we'll only concern ourselves with the "simple" name.&amp;nbsp;such as System&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
Usually, any confusion that arises between the two can be resolved by reminding people
that a filesystem is just one of the places you can get an assembly from.&amp;nbsp; For
instance there are APIs for getting assemblies from byte arrays.
&lt;/p&gt;
&lt;p&gt;
For those that still do see it... In the managed world, the assemblyname gives identity
to the code that resides in the assembly.&amp;nbsp; If you have 2 assemblies with the
same assemblyname, you expect them to represent the same identity (perhaps different
versions, build flavors, bitness, etc.).&amp;nbsp; If we relied on the filesystem name,
the identity of the code could change just by changing the filename.&amp;nbsp; That's
not the semantics we expect.
&lt;/p&gt;
&lt;p&gt;
So, why does the filename matter?&amp;nbsp; Why do we recommend keeping them the same?&amp;nbsp;
Some of the reasons are simple convenience.&amp;nbsp; It's nice to look at a file and
know what it is without cracking it open.&amp;nbsp; If the names are different, it's like
me going to a party and wearing a nametag that says, "Peter".&amp;nbsp; While there is
nothing keeping me from doing it, it causes confusion.&amp;nbsp; However, another more
important reason to keep them the same is that assemblies are rarely loaded by filename.&amp;nbsp;
References and most dynamic loads are done by assemblyname.&amp;nbsp; You don't take a
reference to System.dll, you take a reference to System.&amp;nbsp; At some point, the
loader has to find an appropriate file to load to satisfy that reference.&amp;nbsp; If
System's filename is Peter.dll, then it's going to have a difficult time finding it
to load.&amp;nbsp; This is actually the very reason that gacutil will not let you install
an assembly into the global assembly cache if the filename doesn't match the assemblyname.&amp;nbsp;
However, I think it's silly that it doesn't just fix the name for you.
&lt;/p&gt;
&lt;p&gt;
What about multi-module assemblies?&amp;nbsp; Well, it's the module with the assembly
manifest that matters.&amp;nbsp; It's the one that should match.&amp;nbsp; Then the rest of
the files need to match the assembly manifest :).&amp;nbsp;But, if you're using multi-module
assemblies, let me know.&amp;nbsp; I'd like to know why.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.marklio.com/marklio/aggbug.ashx?id=077f85a7-d422-403d-8e12-3752fc037dd3" /&gt;</description>
      <comments>http://www.marklio.com/marklio/CommentView,guid,077f85a7-d422-403d-8e12-3752fc037dd3.aspx</comments>
      <category>CLR</category>
      <category>Technical</category>
    </item>
    <item>
      <trackback:ping>http://www.marklio.com/marklio/Trackback.aspx?guid=6ffb977d-6496-47c5-abad-4e0705bf3b0b</trackback:ping>
      <pingback:server>http://www.marklio.com/marklio/pingback.aspx</pingback:server>
      <pingback:target>http://www.marklio.com/marklio/PermaLink,guid,6ffb977d-6496-47c5-abad-4e0705bf3b0b.aspx</pingback:target>
      <dc:creator>Mark</dc:creator>
      <wfw:comment>http://www.marklio.com/marklio/CommentView,guid,6ffb977d-6496-47c5-abad-4e0705bf3b0b.aspx</wfw:comment>
      <wfw:commentRss>http://www.marklio.com/marklio/SyndicationService.asmx/GetEntryCommentsRss?guid=6ffb977d-6496-47c5-abad-4e0705bf3b0b</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I've mentioned before that one of my ownership areas at MS is the CLR "shim". 
Most people I've said that to ask me, "What's that?".  I usually reply that generally,
it's mscoree.dll, to which they typically respond, "Oh yeah.  What does that
do?"  In general terms, the shim is in charge of firing up the runtime in a process. 
In addition, it exposes all the hosting APIs and other stuff you need to do stuff
with the CLR from unmanaged code. If you look at a managed app, you will see that
it has a dependency on mscoree.dll, and nothing else CLR-related.  Ater the runtime
is spun up, most of the things that mscoree exposes are simply forwarded calls into
the mscorwks.dll of the runtime you have installed.
</p>
        <p>
What's interesting about mscoree.dll, is that it is the only piece of the runtime
that doesn't run side by side.  You can have v1.1 and v2.0 installed on the machine,
but you will only have one mscoree.dll.  You always have the version of mscoree.dll
that corresponds to the latest version of the runtime installed on your machine (unless
you have installed a patch or something that services mscoree, in which case you may
have a v2.0 shim even though only v1.1 is installed on your machine).
</p>
        <p>
So, naturally, backwards compatibility is extremely important in the Shim.  When
you start up managed code, the Shim decides which version of the CLR to fire up based
on lots of different things.  These things are all fairly well documented and
all have a specific scenario they enable, but by their nature they are very confusing.
</p>
        <blockquote>
          <p>
Aside: When I was job hunting, I interviewed at several companies other than Microsoft. 
During some of those other interviews, I was asked questions about what runtime would
be started under certain conditions. The rules are so confusing, that some of the
interviewers, although all extremely smart people, had formed incorrect models
of what the rules were.  Some told me I had the wrong answer to their question,
when if fact it was correct. (that's not to say that I knew the correct answers to
all of them.)
</p>
        </blockquote>
        <p>
I'm not certain that I can clear up the confusion, but I do hope to have a series
of posts in the coming months on why the shim does what it does under certain circumstances. 
Then, at least you might understand what's going on when it happens.
</p>
        <img width="0" height="0" src="http://www.marklio.com/marklio/aggbug.ashx?id=6ffb977d-6496-47c5-abad-4e0705bf3b0b" />
      </body>
      <title>What is the shim?</title>
      <guid isPermaLink="false">http://www.marklio.com/marklio/PermaLink,guid,6ffb977d-6496-47c5-abad-4e0705bf3b0b.aspx</guid>
      <link>http://www.marklio.com/marklio/PermaLink,guid,6ffb977d-6496-47c5-abad-4e0705bf3b0b.aspx</link>
      <pubDate>Mon, 22 Jan 2007 18:50:36 GMT</pubDate>
      <description>&lt;p&gt;
I've mentioned before that one of my ownership areas at MS is the CLR "shim".&amp;nbsp;
Most people I've said that to ask me, "What's that?".&amp;nbsp; I usually reply that generally,
it's mscoree.dll, to which they typically respond, "Oh yeah.&amp;nbsp; What does that
do?"&amp;nbsp; In general terms, the shim is in charge of firing up the runtime in a process.&amp;nbsp;
In addition, it exposes all the hosting APIs and other stuff you need to do stuff
with the CLR from unmanaged code. If you look at a managed app, you will see that
it has a dependency on mscoree.dll, and nothing else CLR-related.&amp;nbsp; Ater the runtime
is spun up, most of the things that mscoree exposes are simply forwarded calls into
the mscorwks.dll of the runtime you have installed.
&lt;/p&gt;
&lt;p&gt;
What's interesting about mscoree.dll, is that it is the only piece of the runtime
that doesn't run side by side.&amp;nbsp; You can have v1.1 and v2.0 installed on the machine,
but you will only have one mscoree.dll.&amp;nbsp; You always have the version of mscoree.dll
that corresponds to the latest version of the runtime installed on your machine (unless
you have installed a patch or something that services mscoree, in which case you may
have a v2.0 shim even though only v1.1 is installed on your machine).
&lt;/p&gt;
&lt;p&gt;
So, naturally, backwards compatibility is extremely important in the Shim.&amp;nbsp; When
you start up managed code, the Shim decides which version of the CLR to fire up based
on lots of different things.&amp;nbsp; These things are all fairly well documented and
all have a specific scenario they enable, but by their nature they are very confusing.
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
Aside: When I was job hunting, I interviewed at several companies other than Microsoft.&amp;nbsp;
During some of those other interviews, I was asked questions about what runtime would
be started under certain conditions. The rules are so confusing, that some of the
interviewers, although all extremely smart people,&amp;nbsp;had formed incorrect models
of what the rules were.&amp;nbsp; Some told me I had the wrong answer to their question,
when if fact it was correct. (that's not to say that I knew the correct answers to
all of them.)
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
I'm not certain that I can clear up the confusion, but I do hope to have a series
of posts in the coming months on why the shim does what it does under certain circumstances.&amp;nbsp;
Then, at least you might understand what's going on when it happens.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.marklio.com/marklio/aggbug.ashx?id=6ffb977d-6496-47c5-abad-4e0705bf3b0b" /&gt;</description>
      <comments>http://www.marklio.com/marklio/CommentView,guid,6ffb977d-6496-47c5-abad-4e0705bf3b0b.aspx</comments>
      <category>CLR</category>
      <category>Technical</category>
    </item>
    <item>
      <trackback:ping>http://www.marklio.com/marklio/Trackback.aspx?guid=fcda68c3-f9de-4038-8231-917a394990a6</trackback:ping>
      <pingback:server>http://www.marklio.com/marklio/pingback.aspx</pingback:server>
      <pingback:target>http://www.marklio.com/marklio/PermaLink,guid,fcda68c3-f9de-4038-8231-917a394990a6.aspx</pingback:target>
      <dc:creator>Mark</dc:creator>
      <wfw:comment>http://www.marklio.com/marklio/CommentView,guid,fcda68c3-f9de-4038-8231-917a394990a6.aspx</wfw:comment>
      <wfw:commentRss>http://www.marklio.com/marklio/SyndicationService.asmx/GetEntryCommentsRss?guid=fcda68c3-f9de-4038-8231-917a394990a6</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Someone asked me the other day if you could reflect on other people's assemblies using
the CLR.  My answer was, "ABSOLUTELY!!!!!"
</p>
        <p>
As it turns out, they were having some problems achieving this, and they were wondering
if there was some kind of security mechanism in place that was preventing reflection
on 3rd party assemblies.  Here's the basic scenario.  They had a 3rd party
library with a type that defined several "constants" using fields.  They needed
to be able to specify a named constant via a string and return the value for the constant. 
Security and performance arguments aside (they had already been considered), they
simply wanted to lookup the field by name via reflection and get its value. 
This can be accomplished via just a few fairly reasonable lines of code using one
of the GetField family of methods on System.Type and then getting the value. 
I'll leave this as an exercise for the reader so they can have the fun of wresting
with the <a href="http://www.marklio.com/marklio/ct.ashx?id=fcda68c3-f9de-4038-8231-917a394990a6&amp;url=http%3a%2f%2fblogs.msdn.com%2fbrada%2farchive%2f2005%2f11%2f09%2f475090.aspx" target="_blank">silly
BindingFlags enum</a>.
</p>
        <p>
After some discussion, I learned that the trouble actually revolved around getting
the Type object in the first place.  They were using Type.GetType to load the
type with a namespace-qualified name as the string argument.  It was returning
null (Nothing in VB).  They were validating that the string was correct using
Intellisense, and concluding that since Intellisense could see the type, that Type.GetType
should also "see" it (which seems like a perfectly reasonable assumption).
</p>
        <p>
Type.GetType() takes a string argument specifying the type to retrieve.  When
specifying types in strings, you can usually use a namespace qualified name ("[namespace.]type"),
or an assembly-qualified name ("[namespace.]type, assemblyName").  If you don't
provide the assembly name, the API looks through the assemblies already loaded in
the AppDomain for a type that matches the name.  If an assembly name <strong>is</strong> specified,
a bind occurs to the assembly and it is loaded if necessary.
</p>
        <p>
In this case, there was a reference to the assembly containing the type, so Intellisense
was picking it up and providing completion. The trouble was that, at runtime, the
assembly had not yet been loaded into the AppDomain, so the type was unavailable.
</p>
        <p>
So, the options were (in order of appropriateness in my opinion):
</p>
        <ul>
          <li>
Use an early-bound type - Since the type was known at compile-time, use typeof() (GetType()
in VB).  This will create a compile-time, assembly-qualified type reference in
the IL rather than a runtime parsing/bind/load of the type string.</li>
          <li>
Use an assembly-qualified type string - Adding the assembly name to the type string
will let the CLR know what assembly to look in (and load if necessary).  There
are some subtle versioning issues with this approach, especially for strongly-named
assemblies.</li>
          <li>
Make sure the assembly is loaded prior to calling Type.GetType() - Making an early-bound call
to something else in the assembly first will get it loaded into the AppDomain. 
This seems like a fragile solution and I would not recommend it, although it will
technically work.</li>
        </ul>
        <p>
The real issue here is the number of samples (especially in VB) provided by MS that
use Type.GetType() with a non-qualified name (ex. "System.String").
</p>
        <img width="0" height="0" src="http://www.marklio.com/marklio/aggbug.ashx?id=fcda68c3-f9de-4038-8231-917a394990a6" />
      </body>
      <title>When attempting to reflect on assemblies, make sure you have the assembly loaded</title>
      <guid isPermaLink="false">http://www.marklio.com/marklio/PermaLink,guid,fcda68c3-f9de-4038-8231-917a394990a6.aspx</guid>
      <link>http://www.marklio.com/marklio/PermaLink,guid,fcda68c3-f9de-4038-8231-917a394990a6.aspx</link>
      <pubDate>Wed, 03 Jan 2007 17:09:25 GMT</pubDate>
      <description>&lt;p&gt;
Someone asked me the other day if you could reflect on other people's assemblies using
the CLR.&amp;nbsp; My answer was, "ABSOLUTELY!!!!!"
&lt;/p&gt;
&lt;p&gt;
As it turns out, they were having some problems achieving this, and they were wondering
if there was some kind of security mechanism in place that was preventing reflection
on 3rd party assemblies.&amp;nbsp; Here's the basic scenario.&amp;nbsp; They had a 3rd party
library with a type that defined several "constants" using fields.&amp;nbsp; They needed
to be able to specify a named constant via a string and return the value for the constant.&amp;nbsp;
Security and performance arguments aside (they had already been considered), they
simply wanted to lookup the field by name via reflection and get its value.&amp;nbsp;
This can be accomplished via just a few fairly reasonable lines of code using one
of the GetField family of methods on System.Type and then getting the value.&amp;nbsp;
I'll leave this as an exercise for the reader so they can have the fun of wresting
with the &lt;a href="http://www.marklio.com/marklio/ct.ashx?id=fcda68c3-f9de-4038-8231-917a394990a6&amp;amp;url=http%3a%2f%2fblogs.msdn.com%2fbrada%2farchive%2f2005%2f11%2f09%2f475090.aspx" target="_blank"&gt;silly
BindingFlags enum&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
After some discussion, I learned that the trouble actually revolved around getting
the Type object in the first place.&amp;nbsp; They were using Type.GetType to load the
type with a namespace-qualified name as the string argument.&amp;nbsp; It was returning
null (Nothing in VB).&amp;nbsp; They were validating that the string was correct using
Intellisense, and concluding that since Intellisense could see the type, that Type.GetType
should also "see" it (which seems like a perfectly reasonable assumption).
&lt;/p&gt;
&lt;p&gt;
Type.GetType() takes a string argument specifying the type to retrieve.&amp;nbsp; When
specifying types in strings, you can usually use a namespace qualified name ("[namespace.]type"),
or an assembly-qualified name ("[namespace.]type, assemblyName").&amp;nbsp; If you don't
provide the assembly name, the API looks through the assemblies already loaded in
the AppDomain for a type that matches the name.&amp;nbsp; If an assembly name &lt;strong&gt;is&lt;/strong&gt; specified,
a bind occurs to the assembly and it is loaded if necessary.
&lt;/p&gt;
&lt;p&gt;
In this case, there was a reference to the assembly containing the type, so Intellisense
was picking it up and providing completion. The trouble was that, at runtime, the
assembly had not yet been loaded into the AppDomain, so the type was unavailable.
&lt;/p&gt;
&lt;p&gt;
So, the options were (in order of appropriateness in my opinion):
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
Use an early-bound type - Since the type was known at compile-time, use typeof() (GetType()
in VB).&amp;nbsp; This will create a compile-time, assembly-qualified type reference in
the IL rather than a runtime parsing/bind/load of the type string.&lt;/li&gt;
&lt;li&gt;
Use an assembly-qualified type string - Adding the assembly name to the type string
will let the CLR know what assembly to look in (and load if necessary).&amp;nbsp; There
are some subtle versioning issues with this approach, especially for strongly-named
assemblies.&lt;/li&gt;
&lt;li&gt;
Make sure the assembly is loaded prior to calling Type.GetType() - Making an early-bound&amp;nbsp;call
to something else in the assembly first will get it loaded into the AppDomain.&amp;nbsp;
This seems like a fragile solution and I would not recommend it, although it will
technically work.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
The real issue here is the number of samples (especially in VB) provided by MS that
use Type.GetType() with a non-qualified name (ex. "System.String").
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.marklio.com/marklio/aggbug.ashx?id=fcda68c3-f9de-4038-8231-917a394990a6" /&gt;</description>
      <comments>http://www.marklio.com/marklio/CommentView,guid,fcda68c3-f9de-4038-8231-917a394990a6.aspx</comments>
      <category>CLR</category>
      <category>Technical</category>
    </item>
    <item>
      <trackback:ping>http://www.marklio.com/marklio/Trackback.aspx?guid=4c4d6194-5bd1-4c4d-9415-acbf92b9bc1d</trackback:ping>
      <pingback:server>http://www.marklio.com/marklio/pingback.aspx</pingback:server>
      <pingback:target>http://www.marklio.com/marklio/PermaLink,guid,4c4d6194-5bd1-4c4d-9415-acbf92b9bc1d.aspx</pingback:target>
      <dc:creator>Mark</dc:creator>
      <wfw:comment>http://www.marklio.com/marklio/CommentView,guid,4c4d6194-5bd1-4c4d-9415-acbf92b9bc1d.aspx</wfw:comment>
      <wfw:commentRss>http://www.marklio.com/marklio/SyndicationService.asmx/GetEntryCommentsRss?guid=4c4d6194-5bd1-4c4d-9415-acbf92b9bc1d</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Something that always bothered me at my previous job was having to install the
framework SDK to get a copy of gacutil.exe.  I know the guys still there hate
having to install the SDK on a server so they can manipulate the GAC.  Richard
Lander <a href="http://www.marklio.com/marklio/ct.ashx?id=4c4d6194-5bd1-4c4d-9415-acbf92b9bc1d&amp;url=http%3a%2f%2fhoser.lander.ca%2fGACUtil%2bV20%2b%2bWhere%2bTo%2bGet%2bIt.aspx" target="_blank">gives
some interesting information</a> on the topic, but he doesn't go into <strong>why</strong> it's
not included in the redist.
</p>
        <p>
Since I've been at MS, I've learned a great deal more about the Global Assembly Cache
(GAC) and the fusion APIs. Last night, I was taking a shower after cutting my hair,
and the reason came to me.  That reason is... installers, or more importantly...
uninstallers.
</p>
        <p>
During a discussion recently, I heard an amazingly profound saying:
</p>
        <blockquote>
          <p>
"It is better to fail to do, than to fail to undo"
</p>
        </blockquote>
        <p>
I don't recall who said it, and they probably got it from someone else, but it is
right on the money.
</p>
        <p>
When programs uninstall, they have to correctly remove things they've placed in the
GAC.  Let's make up an example.  Let's say I have some software company. 
We've developed a magical managed library that makes it wicked easy to develop
our software, so we use it in all our products.  Let's say that in our deployment
model, it makes sense to deploy that library to the GAC.  So, uninstalling our
software should remove it from the GAC, right?  We'll what if one of our other
products is on the machine? We don't want to uninstall one and break the remaining
one.
</p>
        <p>
When you install an assembly into the GAC via the Fusion APIs, you do so with a traced
reference.  That reference tells Fusion "who" installed it.  If 2 installers
install the same assembly, it's smart enough to know not to remove the assembly until <strong>both</strong> uninstall.
</p>
        <p>
GACUtil, as a management tool, enables you to  use traced references as
well, but it also allows you to install without a traced references.  It also
allows you to force uninstalls and do lots of other screwy things.  In other
words, the tool is too powerful.  Devs need to be able to do screwy things. 
Administrators need to be able to do screwy things.  Regular users don't. 
Give them an install package that handles everything.  Otherwise, you're bound
to have a support nightmare.
</p>
        <p>
If I have time, or enough requests, we'll go into how to use the fusion APIs directly
to manage the GAC.
</p>
        <img width="0" height="0" src="http://www.marklio.com/marklio/aggbug.ashx?id=4c4d6194-5bd1-4c4d-9415-acbf92b9bc1d" />
      </body>
      <title>Why is GACUtil.exe not part of the .NET Framework?</title>
      <guid isPermaLink="false">http://www.marklio.com/marklio/PermaLink,guid,4c4d6194-5bd1-4c4d-9415-acbf92b9bc1d.aspx</guid>
      <link>http://www.marklio.com/marklio/PermaLink,guid,4c4d6194-5bd1-4c4d-9415-acbf92b9bc1d.aspx</link>
      <pubDate>Tue, 14 Nov 2006 17:10:30 GMT</pubDate>
      <description>&lt;p&gt;
Something that always bothered me at my previous job&amp;nbsp;was having to install the
framework SDK to get a copy of gacutil.exe.&amp;nbsp; I know the guys still there hate
having to install the SDK on a server so they can manipulate the GAC.&amp;nbsp; Richard
Lander &lt;a href="http://www.marklio.com/marklio/ct.ashx?id=4c4d6194-5bd1-4c4d-9415-acbf92b9bc1d&amp;amp;url=http%3a%2f%2fhoser.lander.ca%2fGACUtil%2bV20%2b%2bWhere%2bTo%2bGet%2bIt.aspx" target="_blank"&gt;gives
some&amp;nbsp;interesting information&lt;/a&gt;&amp;nbsp;on the topic, but he doesn't go into &lt;strong&gt;why&lt;/strong&gt; it's
not included in the redist.
&lt;/p&gt;
&lt;p&gt;
Since I've been at MS, I've learned a great deal more about the Global Assembly Cache
(GAC) and the fusion APIs. Last night, I was taking a shower after cutting my hair,
and the reason came to me.&amp;nbsp; That reason is... installers, or more importantly...
uninstallers.
&lt;/p&gt;
&lt;p&gt;
During a discussion recently, I heard an amazingly profound saying:
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
"It is better to fail to do, than to fail to undo"
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
I don't recall who said it, and they probably got it from someone else, but it is
right on the money.
&lt;/p&gt;
&lt;p&gt;
When programs uninstall, they have to correctly remove things they've placed in the
GAC.&amp;nbsp; Let's make up an example.&amp;nbsp; Let's say I have some software company.&amp;nbsp;
We've developed a magical&amp;nbsp;managed library that makes it wicked easy to develop
our software, so we use it in all our products.&amp;nbsp; Let's say that in our deployment
model, it makes sense to deploy that library to the GAC.&amp;nbsp; So, uninstalling our
software should remove it from the GAC, right?&amp;nbsp; We'll what if one of our other
products is on the machine? We don't want to uninstall one and break the remaining
one.
&lt;/p&gt;
&lt;p&gt;
When you install an assembly into the GAC via the Fusion APIs, you do so with a traced
reference.&amp;nbsp; That&amp;nbsp;reference tells Fusion "who" installed it.&amp;nbsp; If 2 installers
install the same assembly, it's smart enough to know not to remove the assembly until &lt;strong&gt;both&lt;/strong&gt; uninstall.
&lt;/p&gt;
&lt;p&gt;
GACUtil, as a management tool, enables you to&amp;nbsp;&amp;nbsp;use traced references as
well, but it also allows you to install without a traced references.&amp;nbsp; It also
allows you to force uninstalls and do lots of other screwy things.&amp;nbsp; In other
words, the tool is too powerful.&amp;nbsp; Devs need to be able to do screwy things.&amp;nbsp;
Administrators need to be able to do screwy things.&amp;nbsp; Regular users don't.&amp;nbsp;
Give them an install package that handles everything.&amp;nbsp; Otherwise, you're bound
to have a support nightmare.
&lt;/p&gt;
&lt;p&gt;
If I have time, or enough requests, we'll go into how to use the fusion APIs directly
to manage the GAC.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.marklio.com/marklio/aggbug.ashx?id=4c4d6194-5bd1-4c4d-9415-acbf92b9bc1d" /&gt;</description>
      <comments>http://www.marklio.com/marklio/CommentView,guid,4c4d6194-5bd1-4c4d-9415-acbf92b9bc1d.aspx</comments>
      <category>CLR</category>
      <category>Technical</category>
    </item>
    <item>
      <trackback:ping>http://www.marklio.com/marklio/Trackback.aspx?guid=f721f518-fdc6-40e5-bac0-624e9b869164</trackback:ping>
      <pingback:server>http://www.marklio.com/marklio/pingback.aspx</pingback:server>
      <pingback:target>http://www.marklio.com/marklio/PermaLink,guid,f721f518-fdc6-40e5-bac0-624e9b869164.aspx</pingback:target>
      <dc:creator>Mark</dc:creator>
      <wfw:comment>http://www.marklio.com/marklio/CommentView,guid,f721f518-fdc6-40e5-bac0-624e9b869164.aspx</wfw:comment>
      <wfw:commentRss>http://www.marklio.com/marklio/SyndicationService.asmx/GetEntryCommentsRss?guid=f721f518-fdc6-40e5-bac0-624e9b869164</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Several people have complained that I haven't made any entries in a while.  There
are several reasons for this.  Aside from being generally busy, I'm signed up
to do some posts about the CLR in general, and some from my ownership areas as part
of my job.  This has resulted in several "half-baked" entries ranging from hosting
the runtime to the Global Assembly Cache, to some other more general CLR-related entries.
</p>
        <p>
In addition, MS has had several new products in the queue that were on the verge of
releasing, and I've had entries waiting for them to release.  Of course, being
on the safe side, I've waited for someone else to blog first, at which point an extra
post from me doesn't make much difference.
</p>
        <p>
And thirdly, Jenna is growing like a weed and it's hard to find time to sit down and
formulate posts when you're chasing her around the house.
</p>
        <p>
So, regarding technical content.  I'd like to get an idea of what people are
interested in hearing about. So shoot me an email or leave a comment with suggestions
for posts regarding the CLR in general or specifically within my areas of ownership:
</p>
        <ul>
          <li>
The unmanaged hosting API's (CorBindToRuntime, etc.). These are what you'd use
to host the CLR in your own app in order to more tightly control things, or provide
additional isolation or escalation policies. 
</li>
          <li>
The global assembly cache 
</li>
          <li>
"automatic" CLR activation - what I mean by this is what happens when the runtime
is spun up my a managed app, or via COM interop.  Things like how to decide which
runtime to use, etc.</li>
        </ul>
        <p>
In addition, I'll try to keep the personal updates coming for those who are not looking
for just technical content.
</p>
        <img width="0" height="0" src="http://www.marklio.com/marklio/aggbug.ashx?id=f721f518-fdc6-40e5-bac0-624e9b869164" />
      </body>
      <title>RFP (Requests for posts)</title>
      <guid isPermaLink="false">http://www.marklio.com/marklio/PermaLink,guid,f721f518-fdc6-40e5-bac0-624e9b869164.aspx</guid>
      <link>http://www.marklio.com/marklio/PermaLink,guid,f721f518-fdc6-40e5-bac0-624e9b869164.aspx</link>
      <pubDate>Mon, 13 Nov 2006 20:49:21 GMT</pubDate>
      <description>&lt;p&gt;
Several people have complained that I haven't made any entries in a while.&amp;nbsp; There
are several reasons for this.&amp;nbsp; Aside from being generally busy, I'm signed up
to do some posts about the CLR in general, and some from my ownership areas as part
of my job.&amp;nbsp; This has resulted in several "half-baked" entries ranging from hosting
the runtime to the Global Assembly Cache, to some other more general CLR-related entries.
&lt;/p&gt;
&lt;p&gt;
In addition, MS has had several new products in the queue that were on the verge of
releasing, and I've had entries waiting for them to release.&amp;nbsp; Of course, being
on the safe side, I've waited for someone else to blog first, at which point an extra
post from me doesn't make much difference.
&lt;/p&gt;
&lt;p&gt;
And thirdly, Jenna is growing like a weed and it's hard to find time to sit down and
formulate posts when you're chasing her around the house.
&lt;/p&gt;
&lt;p&gt;
So, regarding technical content.&amp;nbsp; I'd like to get an idea of what people are
interested in hearing about.&amp;nbsp;So shoot me an email or leave a comment with suggestions
for posts regarding the CLR in general or&amp;nbsp;specifically within my areas of ownership:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
The unmanaged hosting API's (CorBindToRuntime, etc.).&amp;nbsp;These are what you'd use
to host the CLR in your own app in order to more tightly control things, or provide
additional isolation or escalation policies. 
&lt;li&gt;
The global assembly cache 
&lt;li&gt;
"automatic" CLR activation - what I mean by this is what happens when the runtime
is spun up my a managed app, or via COM interop.&amp;nbsp; Things like how to decide which
runtime to use, etc.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
In addition, I'll try to keep the personal updates coming for those who are not looking
for just technical content.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.marklio.com/marklio/aggbug.ashx?id=f721f518-fdc6-40e5-bac0-624e9b869164" /&gt;</description>
      <comments>http://www.marklio.com/marklio/CommentView,guid,f721f518-fdc6-40e5-bac0-624e9b869164.aspx</comments>
      <category>Technical</category>
      <category>CLR</category>
    </item>
  </channel>
</rss>