<?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>CLR Hoser - Code Samples</title>
    <link>http://hoser.lander.ca/</link>
    <description>(none)</description>
    <language>en-us</language>
    <copyright>Rich Lander</copyright>
    <lastBuildDate>Tue, 08 Dec 2009 19:19:57 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.3.9074.18820</generator>
    <managingEditor>rich@lander.ca</managingEditor>
    <webMaster>rich@lander.ca</webMaster>
    <item>
      <trackback:ping>http://hoser.lander.ca/Trackback.aspx?guid=7dfac062-eec3-4fc4-8562-146eb57ff715</trackback:ping>
      <pingback:server>http://hoser.lander.ca/pingback.aspx</pingback:server>
      <pingback:target>http://hoser.lander.ca/PermaLink,guid,7dfac062-eec3-4fc4-8562-146eb57ff715.aspx</pingback:target>
      <dc:creator>Rich Lander</dc:creator>
      <wfw:comment>http://hoser.lander.ca/CommentView,guid,7dfac062-eec3-4fc4-8562-146eb57ff715.aspx</wfw:comment>
      <wfw:commentRss>http://hoser.lander.ca/SyndicationService.asmx/GetEntryCommentsRss?guid=7dfac062-eec3-4fc4-8562-146eb57ff715</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
          <a href="http://www.flickr.com/photos/39641695@N04/4039697112/in/set-72157622395654822/">
            <img style="border-right-width: 0px; margin: 0px 0px 0px 5px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" align="right" src="http://farm4.static.flickr.com/3550/4039697112_6523410603_m.jpg" />
          </a>I'm
doing a little project right now that requires me to know (among other things) the
number of hardlinks for a given file. I have a tool that already will tell me the
number of hard links for a given file, but I need to collect a bunch more information,
and I really hate calling out to an exe and collecting and parsing its output, as
part of a larger app. So, I set out to write that slightly larger app. 
</p>
        <p>
Having never written an app that cares about hardlinks, I didn't know whether or not
the BCL had APIs that exposed this info. It doesn't. Whether it should expose this
info is a whole other question. So, I resorted to p/invoke, which is fine. I
did a few searches and found that there isn't too much info out there on doing this
from .NET, hence my motivation for writing this post.
</p>
        <p>
I started out with <font size="2" face="Consolas"><font size="2" face="Consolas"><a href="http://pinvoke.net/default.aspx/ntdll/NtQueryInformationFile.html">NtQueryInformationFile</a>. </font></font>I'm
pretty sure that the p/invoke signature on p/invoke.net is wrong. I wasn't able to
get this function working to my satisfaction, but can share where I ended up, in the
hope that it might help someone:
</p>
        <font size="2" face="Consolas">
          <font size="2" face="Consolas">
            <pre>
              <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"> [DllImport(<span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px">"ntdll.dll"</span>,
SetLastError=<span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">true</span>)] <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">static</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">extern</span> Int32
NtQueryInformationFile(IntPtr fileHandle, <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">ref</span> IO_STATUS_BLOCK
IoStatusBlock, [MarshalAs(UnmanagedType.LPArray)] Byte[] arrayBuffer, <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">uint</span> length,
FILE_INFORMATION_CLASS fileInformation); </span>
            </pre>
          </font>
        </font>
        <p>
FWIW, I allocated a byte[2048], and passed in <font size="2" face="Consolas"><font size="2" face="Consolas">(</font></font><font color="#0000ff" size="2" face="Consolas"><font color="#0000ff" size="2" face="Consolas"><font color="#0000ff" size="2" face="Consolas">uint</font></font></font><font size="2" face="Consolas"><font size="2" face="Consolas">)array.Length </font></font>as
the length paramter. 2k was overkill for what I needed, but at least got me a
bunch of bytes. I wasn't too sure what to do next, so decided to look for something
easier. I was guessing that I needed to write a struct who fields mapped over that
byte[], but I wasn't sure what the meaning of those fields was going to be. I couldn't
find any docs that would have that easier, and didn't see any code samples either.
</p>
        <p>
The good news is that I found another function -- <a href="http://pinvoke.net/default.aspx/kernel32/GetFileInformationByHandle.html">GetFileInformationByHandle</a> --
that is much easier to use, and that did work for me.
</p>
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"> [DllImport(<span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px">"kernel32.dll"</span>,
SetLastError <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">true</span>)] <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">static</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">extern</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">bool</span> GetFileInformationByHandle(IntPtr
hFile, <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">out</span> BY_HANDLE_FILE_INFORMATION
lpFileInformation);</span>
        </pre>
        <p>
You can see my app below. For now, it just contains calculates the hard-links, which
is the part that I tackled first. I'll now move to writing the rest of my experiment.
</p>
        <pre>
          <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px">
            <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">using</span> System; <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">using</span> System.Runtime.InteropServices; <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">using</span> System.IO; <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">namespace</span> banff
{ <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">class</span> Program
{ <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">static</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">void</span> Main(<span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">string</span>[]
args) { <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">if</span> (args
== <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">null</span> ||
args.Length == 0) { Console.WriteLine(<span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px">"Please
specify a file to check"</span>); <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">return</span>;
} <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">else</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">if</span> (!File.Exists(args[0]))
{ Console.WriteLine(<span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px">"File
doesn't exist"</span>); } FileStream fs <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">null</span>;
fs <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">new</span> FileStream(args[0],FileMode.Open,FileAccess.Read);
BY_HANDLE_FILE_INFORMATION fileInfo; Boolean result <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span> GetFileInformationByHandle(fs.Handle, <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">out</span> fileInfo);
Console.WriteLine(fileInfo.NumberOfLinks); fs.Close(); } [DllImport(<span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px">"kernel32.dll"</span>,
SetLastError <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px">=</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">true</span>)] <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">static</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">extern</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">bool</span> GetFileInformationByHandle(IntPtr
hFile, <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">out</span> BY_HANDLE_FILE_INFORMATION
lpFileInformation); <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">struct</span> BY_HANDLE_FILE_INFORMATION
{ <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">uint</span> FileAttributes; <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span> FILETIME
CreationTime; <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span> FILETIME
LastAccessTime; <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span> FILETIME
LastWriteTime; <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">uint</span> VolumeSerialNumber; <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">uint</span> FileSizeHigh; <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">uint</span> FileSizeLow; <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">uint</span> NumberOfLinks; <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">uint</span> FileIndexHigh; <span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">public</span><span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px">uint</span> FileIndexLow;
} } } </span>
        </pre>
        <p>
 
</p>
        <p>
 
</p>
        <img width="0" height="0" src="http://hoser.lander.ca/aggbug.ashx?id=7dfac062-eec3-4fc4-8562-146eb57ff715" />
      </body>
      <title>Determining Number of Hardlinks for a File</title>
      <guid isPermaLink="false">http://hoser.lander.ca/PermaLink,guid,7dfac062-eec3-4fc4-8562-146eb57ff715.aspx</guid>
      <link>http://hoser.lander.ca/2009/12/08/DeterminingNumberOfHardlinksForAFile.aspx</link>
      <pubDate>Tue, 08 Dec 2009 19:19:57 GMT</pubDate>
      <description>&lt;p&gt;
&lt;a href="http://www.flickr.com/photos/39641695@N04/4039697112/in/set-72157622395654822/"&gt;&lt;img style="border-right-width: 0px; margin: 0px 0px 0px 5px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" align=right src="http://farm4.static.flickr.com/3550/4039697112_6523410603_m.jpg"&gt;&lt;/a&gt;I'm
doing a little project right now that requires me to know (among other things) the
number of hardlinks for a given file. I have a tool that already will tell me the
number of hard links for a given file, but I need to collect a bunch more information,
and I really hate calling out to an exe and collecting and parsing its output, as
part of a larger app. So, I set out to write that slightly larger app. 
&lt;/p&gt;
&lt;p&gt;
Having never written an app that cares about hardlinks, I didn't know whether or not
the BCL had APIs that exposed this info. It doesn't. Whether it should expose this
info is a whole other question.&amp;nbsp;So, I resorted to p/invoke, which is fine. I
did a few searches and found that there isn't too much info out there on doing this
from .NET, hence my motivation for writing this post.
&lt;/p&gt;
&lt;p&gt;
I started out with&amp;nbsp;&lt;font size=2 face=Consolas&gt;&lt;font size=2 face=Consolas&gt;&lt;a href="http://pinvoke.net/default.aspx/ntdll/NtQueryInformationFile.html"&gt;NtQueryInformationFile&lt;/a&gt;. &lt;/font&gt;&lt;/font&gt;I'm
pretty sure that the p/invoke signature on p/invoke.net is wrong. I wasn't able to
get this function working to my satisfaction, but can share where I ended up, in the
hope that it might help someone:
&lt;/p&gt;
&lt;font size=2 face=Consolas&gt;&lt;font size=2 face=Consolas&gt;&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt; [DllImport(&lt;span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px"&gt;"ntdll.dll"&lt;/span&gt;,
SetLastError=&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;true&lt;/span&gt;)] &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;static&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;extern&lt;/span&gt; Int32
NtQueryInformationFile(IntPtr fileHandle, &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;ref&lt;/span&gt; IO_STATUS_BLOCK
IoStatusBlock, [MarshalAs(UnmanagedType.LPArray)] Byte[] arrayBuffer, &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;uint&lt;/span&gt; length,
FILE_INFORMATION_CLASS fileInformation); &lt;/span&gt;&lt;/pre&gt;&lt;/font&gt;&lt;/font&gt; 
&lt;p&gt;
FWIW, I allocated a byte[2048], and passed in&amp;nbsp;&lt;font size=2 face=Consolas&gt;&lt;font size=2 face=Consolas&gt;(&lt;/font&gt;&lt;/font&gt;&lt;font color=#0000ff size=2 face=Consolas&gt;&lt;font color=#0000ff size=2 face=Consolas&gt;&lt;font color=#0000ff size=2 face=Consolas&gt;uint&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font size=2 face=Consolas&gt;&lt;font size=2 face=Consolas&gt;)array.Length &lt;/font&gt;&lt;/font&gt;as
the&amp;nbsp;length paramter. 2k was overkill for what I needed, but at least got me a
bunch of bytes. I wasn't too sure what to do next, so decided to look for something
easier. I was guessing that I needed to write a struct who fields mapped over that
byte[], but I wasn't sure what the meaning of those fields was going to be. I couldn't
find any docs that would have that easier, and didn't see any code samples either.
&lt;/p&gt;
&lt;p&gt;
The good news is that I found another function -- &lt;a href="http://pinvoke.net/default.aspx/kernel32/GetFileInformationByHandle.html"&gt;GetFileInformationByHandle&lt;/a&gt; --
that is much easier to use, and that did work for me.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt; [DllImport(&lt;span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px"&gt;"kernel32.dll"&lt;/span&gt;,
SetLastError &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;true&lt;/span&gt;)] &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;static&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;extern&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;bool&lt;/span&gt; GetFileInformationByHandle(IntPtr
hFile, &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;out&lt;/span&gt; BY_HANDLE_FILE_INFORMATION
lpFileInformation);&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
You can see my app below. For now, it just contains calculates the hard-links, which
is the part that I tackled first. I'll now move to writing the rest of my experiment.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: black; FONT-SIZE: 11px"&gt;&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;using&lt;/span&gt; System; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;using&lt;/span&gt; System.Runtime.InteropServices; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;using&lt;/span&gt; System.IO; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;namespace&lt;/span&gt; banff
{ &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;class&lt;/span&gt; Program
{ &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;static&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;void&lt;/span&gt; Main(&lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;string&lt;/span&gt;[]
args) { &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;if&lt;/span&gt; (args
== &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;null&lt;/span&gt; ||
args.Length == 0) { Console.WriteLine(&lt;span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px"&gt;"Please
specify a file to check"&lt;/span&gt;); &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;return&lt;/span&gt;;
} &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;else&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;if&lt;/span&gt; (!File.Exists(args[0]))
{ Console.WriteLine(&lt;span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px"&gt;"File
doesn't exist"&lt;/span&gt;); } FileStream fs &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;null&lt;/span&gt;;
fs &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;new&lt;/span&gt; FileStream(args[0],FileMode.Open,FileAccess.Read);
BY_HANDLE_FILE_INFORMATION fileInfo; Boolean result &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; GetFileInformationByHandle(fs.Handle, &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;out&lt;/span&gt; fileInfo);
Console.WriteLine(fileInfo.NumberOfLinks); fs.Close(); } [DllImport(&lt;span style="BACKGROUND-COLOR: #e4e4e4; FONT-FAMILY: Courier New; COLOR: #666666; FONT-SIZE: 11px"&gt;"kernel32.dll"&lt;/span&gt;,
SetLastError &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: red; FONT-SIZE: 11px"&gt;=&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;true&lt;/span&gt;)] &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;static&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;extern&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;bool&lt;/span&gt; GetFileInformationByHandle(IntPtr
hFile, &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;out&lt;/span&gt; BY_HANDLE_FILE_INFORMATION
lpFileInformation); &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;struct&lt;/span&gt; BY_HANDLE_FILE_INFORMATION
{ &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;uint&lt;/span&gt; FileAttributes; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; FILETIME
CreationTime; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; FILETIME
LastAccessTime; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; FILETIME
LastWriteTime; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;uint&lt;/span&gt; VolumeSerialNumber; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;uint&lt;/span&gt; FileSizeHigh; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;uint&lt;/span&gt; FileSizeLow; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;uint&lt;/span&gt; NumberOfLinks; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;uint&lt;/span&gt; FileIndexHigh; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;public&lt;/span&gt; &lt;span style="BACKGROUND-COLOR: transparent; FONT-FAMILY: Courier New; COLOR: blue; FONT-SIZE: 11px"&gt;uint&lt;/span&gt; FileIndexLow;
} } } &lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://hoser.lander.ca/aggbug.ashx?id=7dfac062-eec3-4fc4-8562-146eb57ff715" /&gt;</description>
      <comments>http://hoser.lander.ca/CommentView,guid,7dfac062-eec3-4fc4-8562-146eb57ff715.aspx</comments>
      <category>Code Samples</category>
    </item>
    <item>
      <trackback:ping>http://hoser.lander.ca/Trackback.aspx?guid=ef6034d4-1492-4780-9cfd-0fc43341ace0</trackback:ping>
      <pingback:server>http://hoser.lander.ca/pingback.aspx</pingback:server>
      <pingback:target>http://hoser.lander.ca/PermaLink,guid,ef6034d4-1492-4780-9cfd-0fc43341ace0.aspx</pingback:target>
      <dc:creator>Rich Lander</dc:creator>
      <wfw:comment>http://hoser.lander.ca/CommentView,guid,ef6034d4-1492-4780-9cfd-0fc43341ace0.aspx</wfw:comment>
      <wfw:commentRss>http://hoser.lander.ca/SyndicationService.asmx/GetEntryCommentsRss?guid=ef6034d4-1492-4780-9cfd-0fc43341ace0</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I decided to re-build <a href="http://www.dasblog.info/">dasblog</a> (the blog software
that I use) on Whidbey (VS 2005) over the holidays. It is something that I had always
wanted to do and it seems like the dasblog folks have not yet publicly taken that
on. Before anyone gets any strange ideas, I have no desire for a <a href="http://www.process64.com/thinkjot/download.htm">thinkjot</a>-like
schism, but wanted to move the codebase to Whidbey and work on getting the software
to run under partial-trust. If this all works out, and the dasblog team wants to adopt
my changes, cool, otherwise, I'm going to just use it myself.
</p>
        <p>
Anyway, I decided to tackle this task over the holidays. Believe it or not, I almost
exclusively use the free <a href="http://msdn.microsoft.com/vstudio/express/">VS
Express SKUs</a> for my coding work. The "free" part isn't a big deal for me as I
have a full copy of VS team suite a couple meters away from me, and I can download
and install any program (including "Microsoft Bob") from the MS network that I want.
Still, the Express SKUs are super convenient since I can download and install them
in about 10mins and they satisfy most of my needs. Anyway, I downloaded the dasblog
source and started playing with it. It became clear that I needed to use both <a href="http://msdn.microsoft.com/vstudio/express/visualcsharp/">VS
express C#</a> and <a href="http://msdn.microsoft.com/vstudio/express/vwd/">VS express
Web</a> to get this done since the Web product didn't appear to support multiple projects
in a single solution; in fact, it didn't appear to support the solution concept at
all. And I then realized that since VS C# doesn't support JIT attach debugging (due
to licensing issues), that the whole thing wasn't going to work at all. That's when
I reached over for the quite large (requires two hands) VS team suite box to do some
"real developlment" ;)
</p>
        <p>
After a fairly lengthy install (and I didn't even install MSDN since I use the web
mostly), I started back at it. I was able to get the web and class library projects
into one solution in VS. Cool! I then hit F5 and it was immediately clear that something
was terribly broken. The web project launched as expected, but was immediately detached
from the debugger. Huh? I tried a couple more times, and I had the same experience.
I was able to attach VS to WebDev.WebServer.Exe and then refresh the page, and then
my breakpoints were hit. This approach though is anything but a good experience.
</p>
        <p>
I had heard that there were some incompatibilities with VS on Vista, but I was under
the impression that it was more niche issues, of which this is not. It is also very
strange that I didn't have these same problems with the Express products, which I've
been using on Vista for months. I wonder why the full product has some additional
problems. I'm sure someone in building 41 knows.
</p>
        <p>
Time to install VS 2005 SP1. I went to the following <a href="http://msdn.microsoft.com/vstudio/support/vs2005sp1/default.aspx">page</a>.
I downloaded the Vista-specific update. That didn't work, claiming that I was missing
a file or two. I then downloaded and installed the non-Vista-specific SP1 package,
which is just shy of 1/2 GB. Ouch! That worked. Upon launching VS, it claimed that
I needed the Vista-specific update. Oh, I see, you need to install the generic <a href="http://www.microsoft.com/downloads/details.aspx?familyid=BB4A75AB-E2D4-4C96-B39D-37BAF6B5B1DC&amp;displaylang=en">VS
2005 service pack</a>, and then the <a href="http://www.microsoft.com/downloads/details.aspx?familyid=fb6bb56a-10b7-4c05-b81c-5863284503cf&amp;displaylang=en">Vista-specific
update</a>. That was not at all clear to me from the VS 2005 SP1 page. Grrrr. Anyhow,
now you can avoid the trouble that I had.
</p>
        <p>
I then launch VS, but it claims that I need to launch the app elevated. I was actually
expecting that, but thought that they would have manifested the application to force
the elevation dialog. I guess not, or maybe that's still coming. Developers are going
to go nuts if they have to remember to right click on the VS 2005 icon and hit "Run
as administrator" every time, or just turn off UAC on their dev-boxes, which is a
bad idea. 
</p>
        <p>
OK, launch VS again, but elevated, and voila, everything is working correctly again.
Peace and harmony have now returned to my development experience.
</p>
        <p>
I'm very thanksful that the VS team has pulled off this pretty significant service
pack ... *before* Vista is generally available. I'm glad to be back and productive
again. The directions on MSDN could use some improvement.
</p>
        <img width="0" height="0" src="http://hoser.lander.ca/aggbug.ashx?id=ef6034d4-1492-4780-9cfd-0fc43341ace0" />
      </body>
      <title>Visual Studio SP1 on Windows Vista Experience</title>
      <guid isPermaLink="false">http://hoser.lander.ca/PermaLink,guid,ef6034d4-1492-4780-9cfd-0fc43341ace0.aspx</guid>
      <link>http://hoser.lander.ca/2006/12/27/VisualStudioSP1OnWindowsVistaExperience.aspx</link>
      <pubDate>Wed, 27 Dec 2006 22:29:08 GMT</pubDate>
      <description>&lt;p&gt;
I decided to re-build &lt;a href="http://www.dasblog.info/"&gt;dasblog&lt;/a&gt; (the blog software
that I use) on Whidbey (VS 2005) over the holidays. It is something that I had always
wanted to do and it seems like the dasblog folks have not yet publicly taken that
on. Before anyone gets any strange ideas, I have no desire for a &lt;a href="http://www.process64.com/thinkjot/download.htm"&gt;thinkjot&lt;/a&gt;-like
schism, but wanted to move the codebase to Whidbey and work on getting the software
to run under partial-trust. If this all works out, and the dasblog team wants to adopt
my changes, cool, otherwise, I'm going to just use it myself.
&lt;/p&gt;
&lt;p&gt;
Anyway, I decided to tackle this task over the holidays. Believe it or not, I almost
exclusively use the free&amp;nbsp;&lt;a href="http://msdn.microsoft.com/vstudio/express/"&gt;VS
Express SKUs&lt;/a&gt; for my coding work. The "free" part isn't a big deal for me as I
have a full copy of VS team suite a couple meters away from me, and I can download
and install any program (including "Microsoft Bob") from the MS network that I want.
Still, the Express SKUs are super convenient since I can download and install them
in about 10mins and they satisfy most of my needs. Anyway, I downloaded the dasblog
source and started playing with it. It became clear that I needed to use both &lt;a href="http://msdn.microsoft.com/vstudio/express/visualcsharp/"&gt;VS
express C#&lt;/a&gt; and &lt;a href="http://msdn.microsoft.com/vstudio/express/vwd/"&gt;VS express
Web&lt;/a&gt; to get this done since the Web product didn't appear to support multiple projects
in a single solution; in fact, it didn't appear to support the solution concept at
all. And I then realized that since VS C# doesn't support JIT attach debugging (due
to licensing issues), that the whole thing wasn't going to work at all. That's when
I reached over for the quite large (requires two hands) VS team suite box to do some
"real developlment" ;)
&lt;/p&gt;
&lt;p&gt;
After a fairly lengthy install (and I didn't even install MSDN since I use the web
mostly), I started back at it. I was able to get the web and class library projects
into one solution in VS. Cool! I then hit F5 and it was immediately clear that something
was terribly broken. The web project launched as expected, but was immediately detached
from the debugger. Huh? I tried a couple more times, and I had the same experience.
I was able to attach VS to WebDev.WebServer.Exe and then refresh the page, and then
my breakpoints were hit. This approach though is anything but a good experience.
&lt;/p&gt;
&lt;p&gt;
I had heard that there were some incompatibilities with VS on Vista, but I was under
the impression that it was more niche issues, of which this is not. It is also very
strange that I didn't have these same problems with the Express products, which I've
been using on Vista for months. I wonder why the full product has some additional
problems. I'm sure someone in building 41 knows.
&lt;/p&gt;
&lt;p&gt;
Time to install VS 2005 SP1. I went to the following &lt;a href="http://msdn.microsoft.com/vstudio/support/vs2005sp1/default.aspx"&gt;page&lt;/a&gt;.
I downloaded the Vista-specific update. That didn't work, claiming that I was missing
a file or two. I then downloaded and installed the non-Vista-specific SP1 package,
which is just shy of 1/2 GB. Ouch! That worked. Upon launching VS, it claimed that
I needed the Vista-specific update. Oh, I see, you need to install the generic &lt;a href="http://www.microsoft.com/downloads/details.aspx?familyid=BB4A75AB-E2D4-4C96-B39D-37BAF6B5B1DC&amp;amp;displaylang=en"&gt;VS
2005 service pack&lt;/a&gt;, and then the &lt;a href="http://www.microsoft.com/downloads/details.aspx?familyid=fb6bb56a-10b7-4c05-b81c-5863284503cf&amp;amp;displaylang=en"&gt;Vista-specific
update&lt;/a&gt;. That was not at all clear to me from the VS 2005 SP1 page. Grrrr. Anyhow,
now you can avoid the trouble that I had.
&lt;/p&gt;
&lt;p&gt;
I then launch VS, but it claims that I need to launch the app elevated. I was actually
expecting that, but thought that they would have manifested the application to force
the elevation dialog. I guess not, or maybe that's still coming. Developers are going
to go nuts if they have to remember to right click on the VS 2005 icon and hit "Run
as administrator" every time, or just turn off UAC on their dev-boxes, which is a
bad idea. 
&lt;/p&gt;
&lt;p&gt;
OK, launch VS again, but elevated, and voila, everything is working correctly again.
Peace and harmony have now returned to my development experience.
&lt;/p&gt;
&lt;p&gt;
I'm very thanksful that the VS team has pulled off this pretty significant service
pack ... *before* Vista is generally available. I'm glad to be back and productive
again. The directions on MSDN could use some improvement.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://hoser.lander.ca/aggbug.ashx?id=ef6034d4-1492-4780-9cfd-0fc43341ace0" /&gt;</description>
      <comments>http://hoser.lander.ca/CommentView,guid,ef6034d4-1492-4780-9cfd-0fc43341ace0.aspx</comments>
      <category>Code Samples</category>
      <category>Community</category>
    </item>
    <item>
      <trackback:ping>http://hoser.lander.ca/Trackback.aspx?guid=d96d6c72-c981-49a0-a50f-654dda2da2d1</trackback:ping>
      <pingback:server>http://hoser.lander.ca/pingback.aspx</pingback:server>
      <pingback:target>http://hoser.lander.ca/PermaLink,guid,d96d6c72-c981-49a0-a50f-654dda2da2d1.aspx</pingback:target>
      <dc:creator>Rich Lander</dc:creator>
      <wfw:comment>http://hoser.lander.ca/CommentView,guid,d96d6c72-c981-49a0-a50f-654dda2da2d1.aspx</wfw:comment>
      <wfw:commentRss>http://hoser.lander.ca/SyndicationService.asmx/GetEntryCommentsRss?guid=d96d6c72-c981-49a0-a50f-654dda2da2d1</wfw:commentRss>
      <title>Getting the list of loaded assemblies from another App domain</title>
      <guid isPermaLink="false">http://hoser.lander.ca/PermaLink,guid,d96d6c72-c981-49a0-a50f-654dda2da2d1.aspx</guid>
      <link>http://hoser.lander.ca/2006/12/20/GettingTheListOfLoadedAssembliesFromAnotherAppDomain.aspx</link>
      <pubDate>Wed, 20 Dec 2006 20:40:10 GMT</pubDate>
      <description>&lt;p class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;
&lt;font face=Calibri color=#000000 size=3&gt;I&amp;nbsp;wrote a recent post about &lt;a href="http://hoser.lander.ca/Getting+The+List+Of+Loaded+Assemblies.aspx"&gt;getting
the list of loaded assemblies&lt;/a&gt;. That’s a pretty straight-forward operation as shown
by the code listed in that post. I’d like to step up the problem one notch by looking
at getting the list of assemblies from another domain. Seems like a very reasonable
thing to do ... Or we’ll see …&lt;/font&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;
&lt;font face=Calibri color=#000000 size=3&gt;To properly setup this example, I need to
create another domain and keep a reference to that domain. That’s easy:&lt;/font&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/span&gt;&lt;span style="COLOR: green"&gt;&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;//Create
new domain&lt;o:p&gt;&lt;/o:p&gt;
&lt;/span&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/span&gt;&lt;span style="COLOR: teal"&gt;AppDomain&lt;/span&gt;&lt;font color=#000000&gt; domain
= &lt;/font&gt;&lt;span style="COLOR: teal"&gt;AppDomain&lt;/span&gt;&lt;font color=#000000&gt;.CreateDomain(&lt;/font&gt;&lt;span style="COLOR: maroon"&gt;"domain2"&lt;/span&gt;&lt;font color=#000000&gt;);&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;
&lt;o:p&gt;
&lt;font face=Calibri color=#000000 size=3&gt;&amp;nbsp;&lt;/font&gt;
&lt;/o:p&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;
&lt;font face=Calibri color=#000000 size=3&gt;Next, we need to load some code in that domain,
or else there won’t be any assemblies loaded there to actually query. Note that &lt;/font&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: teal; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;Host&lt;/span&gt;&lt;font face=Calibri color=#000000 size=3&gt; is
one of my types, as you’ll see later. The method below loads the DomainHost assembly
in domain2, creates an instance of the &lt;/font&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: teal; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;DomainHost.Host&lt;/span&gt;&lt;font face=Calibri color=#000000 size=3&gt; type
in domain2 and also returns a reference to that instance in the current domain. That’s
why you see the cast to &lt;/font&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: teal; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;Host&lt;/span&gt;&lt;font face=Calibri color=#000000 size=3&gt;.&lt;/font&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/span&gt;&lt;span style="COLOR: teal"&gt;Host&lt;/span&gt;&lt;font color=#000000&gt; host
= (&lt;/font&gt;&lt;span style="COLOR: teal"&gt;Host&lt;/span&gt;&lt;font color=#000000&gt;)domain.CreateInstanceAndUnwrap(&lt;/font&gt;&lt;span style="COLOR: maroon"&gt;"DomainHost"&lt;/span&gt;&lt;font color=#000000&gt;, &lt;/font&gt;&lt;span style="COLOR: maroon"&gt;"DomainHost.Host"&lt;/span&gt;&lt;font color=#000000&gt;);&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;
&lt;o:p&gt;
&lt;font face=Calibri color=#000000 size=3&gt;&amp;nbsp;&lt;/font&gt;
&lt;/o:p&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;
&lt;font face=Calibri color=#000000 size=3&gt;OK … this is the part that where it gets interesting.
It is actually a little non-obvious on how you are intended to get the list of loaded
assemblies from the other domains. Let’s first look at the most intuitive approach
for doing this. There is the same GetAssemblies() method on &lt;/font&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: teal; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;AppDomain&lt;/span&gt;&lt;font color=#000000&gt;&lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt; &lt;/span&gt;&lt;font face=Calibri&gt;&lt;font size=3&gt;that
I used in the last post.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;&lt;/font&gt;&lt;font size=3&gt;So,
given that I have a reference to the domain2 &lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: teal; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;AppDomain&lt;/span&gt;&lt;font color=#000000&gt;&lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt; &lt;/span&gt;&lt;font face=Calibri size=3&gt;instance,
why not just call GetAssemblies() on it? This is what the code would look like:&lt;/font&gt;&lt;/font&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;domain.GetAssemblies();&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;
&lt;o:p&gt;
&lt;font face=Calibri color=#000000 size=3&gt;&amp;nbsp;&lt;/font&gt;
&lt;/o:p&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;
&lt;font face=Calibri color=#000000 size=3&gt;For a variety of reasons, assemblies are not
marked as serializable objects, which means that they cannot be remoted across an
appdomain (or any other) boundary. You can get around this limitation by remoting
and then loading an assembly as a Byte[], but that’s a different blog post. This call
gets around the limitation by remoting an array of representative &lt;/font&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: teal; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;AssemblyName&lt;/span&gt;&lt;font face=Calibri color=#000000 size=3&gt; objects,
as opposed to the actual assemblies. The loader then does the equivalent of the following,
assuming the &lt;/font&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: teal; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;AssemblyName[]&lt;/span&gt;&lt;font face=Calibri color=#000000 size=3&gt;is
called asmNames:&lt;/font&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/span&gt;&lt;span style="COLOR: blue"&gt;foreach&lt;/span&gt;&lt;font color=#000000&gt; (&lt;/font&gt;&lt;span style="COLOR: teal"&gt;AssemblyName&lt;/span&gt;&lt;font color=#000000&gt; asmName &lt;/font&gt;&lt;span style="COLOR: blue"&gt;in&lt;/span&gt;&lt;font color=#000000&gt; asmNames)&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/span&gt;&lt;span style="COLOR: teal"&gt;Assembly&lt;/span&gt;&lt;font color=#000000&gt;.Load(asmName);&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;
&lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;
&lt;font face=Calibri color=#000000 size=3&gt;This behavior is not really expected or desirable.
First, you really want the &lt;/font&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: teal; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;AssemblyName&lt;/span&gt;&lt;font face=Calibri color=#000000 size=3&gt; objects,
not the &lt;/font&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: teal; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;Assembly&lt;/span&gt;&lt;font face=Calibri color=#000000 size=3&gt; objects,
and you definitely do not want to cause the assemblies to be loaded in this domain.
In addition, if the assemblies are not located in the GAC, and the other domain has
a different AppBase, then it is very likely that the assembly loads will fail, resulting
in the loader throwing an exception that you might not be prepared to catch. Ouch.
That’s really bad. It is also important that not all assemblies play nicely with this
behavior. For example, Reflection.Emit assemblies cannot be re-loaded cross domain,
for fairly obvious reasons. Mixed-mode assemblies have another set of problems. So,
let’s skip that option.&lt;/font&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;
&lt;font face=Calibri color=#000000 size=3&gt;Another option is to call &lt;/font&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: teal; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;AppDomain&lt;/span&gt;&lt;font color=#000000&gt;&lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;.GetAssemblies()&lt;/span&gt;&lt;font face=Calibri size=3&gt; in
the other domain, and then to remote the &lt;/font&gt;&lt;/font&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: teal; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;Assembly[]&lt;/span&gt;&lt;font face=Calibri color=#000000 size=3&gt; back
through to the current domain.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;That will
have the same effect as what I just discussed above. &lt;/font&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;
&lt;font size=3&gt;&lt;font color=#000000&gt;&lt;font face=Calibri&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/span&gt;I’ve
skipped over some important details that I need to explain. My &lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: teal; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;DomainHost.Host&lt;/span&gt;&lt;font face=Calibri color=#000000 size=3&gt; type
is a regular old that type that happens to inherit from &lt;/font&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: teal; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;System.MarshalByRefObject&lt;/span&gt;&lt;font face=Calibri color=#000000 size=3&gt;.
The fact that it inherits from this type is more of a marking than anything else,
as I don’t override any of MBRO’s methods, nor do I have to know what they are. This
is very conceptually similar to marking a type with the [&lt;/font&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: teal; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;Serializable&lt;/span&gt;&lt;font face=Calibri color=#000000 size=3&gt;]
attribute. The difference is that MBRO provides copy-by-ref semantics as opposed to
copy-by-value across a boundary, which is what the [Serializable] attribute provides.
This means that MBRO-inherited types can be remoted across a domain, process or even
machine boundary as a reference that you can easily call across. The method calls
only affect the domain in which the type actually resides (not where the reference
resides), with the exception that return types (which must be either MBRO or [Serializable])
are returned to where the call was made from (the current domain). The reference is
called a transparent proxy, and you can see that in the debugger when you try to peer
into what turns out to be the largely opaque &lt;/font&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: teal; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;System.Runtime.Remoting.Proxies.__TransparentProxy&lt;/span&gt;&lt;font face=Calibri color=#000000 size=3&gt; type.
Woah! The fact that&lt;/font&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: teal; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt; DomainHost.Host&lt;/span&gt;&lt;font face=Calibri color=#000000 size=3&gt; is
MBRO is going to turn out quite useful, as we’ll see. &lt;/font&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;
&lt;font face=Calibri color=#000000 size=3&gt;OK, now to the real solution. I need to add
a method to &lt;/font&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: teal; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;DomainHost.Host&lt;/span&gt;&lt;font face=Calibri color=#000000 size=3&gt; that
I can call that returns &lt;/font&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: teal; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;AssemblyName[]&lt;/span&gt;&lt;font face=Calibri color=#000000 size=3&gt; and
that does not interact with the loader in any way on this side of the domain boundary,
as I want to avoid any assembly loads on these &lt;/font&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: teal; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;AssemblyName&lt;/span&gt;&lt;font face=Calibri color=#000000 size=3&gt; objects.
Let’s see what the method would look like:&lt;/font&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/span&gt;&lt;span style="COLOR: blue"&gt;public&lt;/span&gt;&lt;font color=#000000&gt; &lt;/font&gt;&lt;span style="COLOR: teal"&gt;AssemblyName&lt;/span&gt;&lt;font color=#000000&gt;[]
GetAssemblyNames()&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/span&gt;&lt;span style="COLOR: teal"&gt;AssemblyName&lt;/span&gt;&lt;font color=#000000&gt;[]
names;&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/span&gt;&lt;span style="COLOR: teal"&gt;Assembly&lt;/span&gt;&lt;font color=#000000&gt;[]
asms;&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;
&lt;o:p&gt;
&lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
&lt;/o:p&gt;
&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;asms
= &lt;/font&gt;&lt;span style="COLOR: teal"&gt;AppDomain&lt;/span&gt;&lt;font color=#000000&gt;.CurrentDomain.GetAssemblies();&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;names
= &lt;/font&gt;&lt;span style="COLOR: blue"&gt;new&lt;/span&gt;&lt;font color=#000000&gt; &lt;/font&gt;&lt;span style="COLOR: teal"&gt;AssemblyName&lt;/span&gt;&lt;font color=#000000&gt;[asms.Length];&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;
&lt;o:p&gt;
&lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
&lt;/o:p&gt;
&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/span&gt;&lt;span style="COLOR: blue"&gt;for&lt;/span&gt;&lt;font color=#000000&gt; (&lt;/font&gt;&lt;span style="COLOR: teal"&gt;Int32&lt;/span&gt;&lt;font color=#000000&gt; i
= 0; i &amp;lt; asms.Length; i++ )&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;names[i]
= asms[i].GetName();&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;
&lt;o:p&gt;
&lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
&lt;/o:p&gt;
&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/span&gt;&lt;span style="COLOR: blue"&gt;return&lt;/span&gt;&lt;font color=#000000&gt; names;&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;
&lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;
&lt;font face=Calibri color=#000000 size=3&gt;This method uses the AppDomain.GetAssemblies()
method to get the list of loaded assemblies, but doesn’t return that. It creates an
equal length &lt;/font&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: teal; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;AssemblyName[]&lt;/span&gt;&lt;font face=Calibri color=#000000 size=3&gt; array
to the &lt;/font&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: teal; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;Assembly[]&lt;/span&gt;&lt;font face=Calibri color=#000000 size=3&gt; that
it already has.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;It then populates that
array with the assembly name of each of the assemblies. After the assembly name array
is populated, the method returns that array of serializable objects back to the caller,
which happens to be on the other side of the appdomain boundary. And how does one
call such a method on the other side of the appdomain boundary? Easy:&lt;/font&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/span&gt;&lt;span style="COLOR: teal"&gt;AssemblyName&lt;/span&gt;&lt;font color=#000000&gt;[]
asmNames = host.GetAssemblyNames();&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;
&lt;o:p&gt;
&lt;font face=Calibri color=#000000 size=3&gt;&amp;nbsp;&lt;/font&gt;
&lt;/o:p&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;
&lt;font face=Calibri color=#000000 size=3&gt;This ease of cross-boundary calls is the beauty
of .NET Remoting. You just call methods as if the instances were located directly
beside you. Cool.&lt;/font&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;
&lt;font face=Calibri color=#000000 size=3&gt;Well, that’s now about it. Let’s take a look
at the whole program:&lt;/font&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;
&lt;font face=Calibri color=#000000 size=3&gt;Main program:&lt;/font&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;using&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt; System;&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;using&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt; System.Reflection;&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;using&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt; DomainHost;&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;
&lt;o:p&gt;
&lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
&lt;/o:p&gt;
&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;namespace&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt; GetAssemblyNamesFromDomain&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt;{&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/span&gt;&lt;span style="COLOR: blue"&gt;class&lt;/span&gt;&lt;font color=#000000&gt; &lt;/font&gt;&lt;span style="COLOR: teal"&gt;Program&lt;o:p&gt;&lt;/o:p&gt;
&lt;/span&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/span&gt;&lt;span style="COLOR: blue"&gt;static&lt;/span&gt;&lt;font color=#000000&gt; &lt;/font&gt;&lt;span style="COLOR: blue"&gt;void&lt;/span&gt;&lt;font color=#000000&gt; Main(&lt;/font&gt;&lt;span style="COLOR: blue"&gt;string&lt;/span&gt;&lt;font color=#000000&gt;[]
args)&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/span&gt;&lt;span style="COLOR: teal"&gt;AppDomain&lt;/span&gt;&lt;font color=#000000&gt; domain;&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/span&gt;&lt;span style="COLOR: teal"&gt;Host&lt;/span&gt;&lt;font color=#000000&gt; host;&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/span&gt;&lt;span style="COLOR: teal"&gt;AssemblyName&lt;/span&gt;&lt;font color=#000000&gt;[]
asmNames;&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;
&lt;o:p&gt;
&lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
&lt;/o:p&gt;
&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/span&gt;&lt;span style="COLOR: green"&gt;//Create
new domain&lt;o:p&gt;&lt;/o:p&gt;
&lt;/span&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;domain
= &lt;/font&gt;&lt;span style="COLOR: teal"&gt;AppDomain&lt;/span&gt;&lt;font color=#000000&gt;.CreateDomain(&lt;/font&gt;&lt;span style="COLOR: maroon"&gt;"domain2"&lt;/span&gt;&lt;font color=#000000&gt;);&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;//Load
assembly in domain2 and create instance of DomainHost.Host&lt;o:p&gt;&lt;/o:p&gt;
&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: 0.5in; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;//A
reference to the instance of DomainHost.Host is returned&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;
&lt;o:p&gt;&lt;/o:p&gt;
&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;host
= (&lt;/font&gt;&lt;span style="COLOR: teal"&gt;Host&lt;/span&gt;&lt;font color=#000000&gt;)domain.CreateInstanceAndUnwrap(&lt;/font&gt;&lt;span style="COLOR: maroon"&gt;"DomainHost"&lt;/span&gt;&lt;font color=#000000&gt;, &lt;/font&gt;&lt;span style="COLOR: maroon"&gt;"DomainHost.Host"&lt;/span&gt;&lt;font color=#000000&gt;);&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;
&lt;o:p&gt;
&lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
&lt;/o:p&gt;
&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/span&gt;&lt;span style="COLOR: green"&gt;//Most
obvious method for getting the list of loaded assemblies.&lt;o:p&gt;&lt;/o:p&gt;
&lt;/span&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/span&gt;&lt;span style="COLOR: green"&gt;//This
method will cause the assemblies in the remote domain to&lt;o:p&gt;&lt;/o:p&gt;
&lt;/span&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/span&gt;&lt;span style="COLOR: green"&gt;//be
loaded in this domain, which frequently won't work. Ouch!&lt;o:p&gt;&lt;/o:p&gt;
&lt;/span&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/span&gt;&lt;span style="COLOR: green"&gt;//domain.GetAssemblies();&lt;o:p&gt;&lt;/o:p&gt;
&lt;/span&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;
&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;
&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;asmNames
= host.GetAssemblyNames();&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/span&gt;&lt;span style="COLOR: teal"&gt;Console&lt;/span&gt;&lt;font color=#000000&gt;.WriteLine();&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/span&gt;&lt;span style="COLOR: teal"&gt;Console&lt;/span&gt;&lt;font color=#000000&gt;.WriteLine(&lt;/font&gt;&lt;span style="COLOR: maroon"&gt;"Printing
assemblies:"&lt;/span&gt;&lt;font color=#000000&gt;);&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/span&gt;&lt;span style="COLOR: blue"&gt;foreach&lt;/span&gt;&lt;font color=#000000&gt; (&lt;/font&gt;&lt;span style="COLOR: teal"&gt;AssemblyName&lt;/span&gt;&lt;font color=#000000&gt; asmName &lt;/font&gt;&lt;span style="COLOR: blue"&gt;in&lt;/span&gt;&lt;font color=#000000&gt; asmNames)&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/span&gt;&lt;span style="COLOR: teal"&gt;Console&lt;/span&gt;&lt;font color=#000000&gt;.WriteLine(asmName.FullName);&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;
&lt;o:p&gt;
&lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
&lt;/o:p&gt;
&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt;}&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;
&lt;o:p&gt;
&lt;font face=Calibri color=#000000 size=3&gt;&amp;nbsp;&lt;/font&gt;
&lt;/o:p&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;
&lt;font face=Calibri color=#000000 size=3&gt;DomainHost.Host (in DomainHost.dll)&lt;/font&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;using&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt; System;&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;using&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt; System.Reflection;&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;
&lt;o:p&gt;
&lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
&lt;/o:p&gt;
&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;namespace&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt; DomainHost&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt;{&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/span&gt;&lt;span style="COLOR: blue"&gt;public&lt;/span&gt;&lt;font color=#000000&gt; &lt;/font&gt;&lt;span style="COLOR: blue"&gt;class&lt;/span&gt;&lt;font color=#000000&gt; &lt;/font&gt;&lt;span style="COLOR: teal"&gt;Host&lt;/span&gt;&lt;font color=#000000&gt; : &lt;/font&gt;&lt;span style="COLOR: teal"&gt;MarshalByRefObject&lt;o:p&gt;&lt;/o:p&gt;
&lt;/span&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/span&gt;&lt;span style="COLOR: blue"&gt;public&lt;/span&gt;&lt;font color=#000000&gt; Host()&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/span&gt;&lt;span style="COLOR: teal"&gt;Console&lt;/span&gt;&lt;font color=#000000&gt;.WriteLine(&lt;/font&gt;&lt;span style="COLOR: maroon"&gt;"Loading
host in domain {0}"&lt;/span&gt;&lt;font color=#000000&gt;,&lt;/font&gt;&lt;span style="COLOR: teal"&gt;AppDomain&lt;/span&gt;&lt;font color=#000000&gt;.CurrentDomain.FriendlyName);&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;
&lt;o:p&gt;
&lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
&lt;/o:p&gt;
&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/span&gt;&lt;span style="COLOR: blue"&gt;public&lt;/span&gt;&lt;font color=#000000&gt; &lt;/font&gt;&lt;span style="COLOR: teal"&gt;AssemblyName&lt;/span&gt;&lt;font color=#000000&gt;[]
GetAssemblyNames()&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/span&gt;&lt;span style="COLOR: teal"&gt;AssemblyName&lt;/span&gt;&lt;font color=#000000&gt;[]
names;&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/span&gt;&lt;span style="COLOR: teal"&gt;Assembly&lt;/span&gt;&lt;font color=#000000&gt;[]
asms;&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;
&lt;o:p&gt;
&lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
&lt;/o:p&gt;
&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;asms
= &lt;/font&gt;&lt;span style="COLOR: teal"&gt;AppDomain&lt;/span&gt;&lt;font color=#000000&gt;.CurrentDomain.GetAssemblies();&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;names
= &lt;/font&gt;&lt;span style="COLOR: blue"&gt;new&lt;/span&gt;&lt;font color=#000000&gt; &lt;/font&gt;&lt;span style="COLOR: teal"&gt;AssemblyName&lt;/span&gt;&lt;font color=#000000&gt;[asms.Length];&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;
&lt;o:p&gt;
&lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
&lt;/o:p&gt;
&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/span&gt;&lt;span style="COLOR: blue"&gt;for&lt;/span&gt;&lt;font color=#000000&gt; (&lt;/font&gt;&lt;span style="COLOR: teal"&gt;Int32&lt;/span&gt;&lt;font color=#000000&gt; i
= 0; i &amp;lt; asms.Length; i++ )&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;names[i]
= asms[i].GetName();&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;
&lt;o:p&gt;
&lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
&lt;/o:p&gt;
&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/span&gt;&lt;span style="COLOR: blue"&gt;return&lt;/span&gt;&lt;font color=#000000&gt; names;&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;
&lt;o:p&gt;
&lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
&lt;/o:p&gt;
&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt;}&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;
&lt;o:p&gt;
&lt;font face=Calibri color=#000000 size=3&gt;&amp;nbsp;&lt;/font&gt;
&lt;/o:p&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;
&lt;font face=Calibri color=#000000 size=3&gt;The output of the program looks like:&lt;/font&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal"&gt;
&lt;font face=Calibri color=#000000 size=3&gt;Loading host in domain domain2&lt;/font&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal"&gt;
&lt;font face=Calibri color=#000000 size=3&gt;Printing assemblies:&lt;/font&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal"&gt;
&lt;font face=Calibri color=#000000 size=3&gt;mscorlib, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089&lt;/font&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal"&gt;
&lt;font face=Calibri color=#000000 size=3&gt;Microsoft.VisualStudio.HostingProcess.Utilities,
Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a&lt;/font&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal"&gt;
&lt;font face=Calibri color=#000000 size=3&gt;DomainHost, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null&lt;/font&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;
&lt;o:p&gt;
&lt;font face=Calibri color=#000000 size=3&gt;&amp;nbsp;&lt;/font&gt;
&lt;/o:p&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;
&lt;font face=Calibri color=#000000 size=3&gt;The fact that I see “Microsoft.VisualStudio.HostingProcess.Utilities”
loaded is a VS weirdness. VS is loading an assembly in domain2 to somehow “help me”,
but I don’t know why. This only occurs when I’m running/debugging my app through VS.
If I run the program outside of VS, I don’t see that assembly loaded. You have noticed
executables ending in vshost.exe. That’s a similar issue, but for another day.&lt;/font&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://hoser.lander.ca/aggbug.ashx?id=d96d6c72-c981-49a0-a50f-654dda2da2d1" /&gt;</description>
      <comments>http://hoser.lander.ca/CommentView,guid,d96d6c72-c981-49a0-a50f-654dda2da2d1.aspx</comments>
      <category>AppDomains</category>
      <category>Code Samples</category>
      <category>Loader</category>
    </item>
    <item>
      <trackback:ping>http://hoser.lander.ca/Trackback.aspx?guid=9595f71a-5137-4d06-b9c0-8bd1b0726439</trackback:ping>
      <pingback:server>http://hoser.lander.ca/pingback.aspx</pingback:server>
      <pingback:target>http://hoser.lander.ca/PermaLink,guid,9595f71a-5137-4d06-b9c0-8bd1b0726439.aspx</pingback:target>
      <dc:creator>Rich Lander</dc:creator>
      <wfw:comment>http://hoser.lander.ca/CommentView,guid,9595f71a-5137-4d06-b9c0-8bd1b0726439.aspx</wfw:comment>
      <wfw:commentRss>http://hoser.lander.ca/SyndicationService.asmx/GetEntryCommentsRss?guid=9595f71a-5137-4d06-b9c0-8bd1b0726439</wfw:commentRss>
      <slash:comments>2</slash:comments>
      <title>How to avoid assembly loads</title>
      <guid isPermaLink="false">http://hoser.lander.ca/PermaLink,guid,9595f71a-5137-4d06-b9c0-8bd1b0726439.aspx</guid>
      <link>http://hoser.lander.ca/2006/12/07/HowToAvoidAssemblyLoads.aspx</link>
      <pubDate>Thu, 07 Dec 2006 20:25:44 GMT</pubDate>
      <description>&lt;p class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;
&lt;font face=Calibri color=#000000 size=3&gt;A guy from a well-known company mailed me
asking how to avoid deploying statically referenced assemblies that he knows will
not be loaded at runtime. The issue is that the assembly is being loaded, but he doesn’t
believe that it is being actually used. His current workaround is to deploy the assembly
to avoid FileNotFound exceptions, which nobody likes.&lt;/font&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;
&lt;font face=Calibri color=#000000 size=3&gt;I have two questions for the guy:&lt;/font&gt;
&lt;/p&gt;
&lt;p class=MsoListParagraph style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l1 level1 lfo1"&gt;
&lt;font face="Times New Roman"&gt;&lt;span style="mso-fareast-font-family: 'Times New Roman'"&gt;&lt;span style="mso-list: Ignore"&gt;&lt;font color=#000000&gt;&lt;font size=3&gt;1.&lt;/font&gt;&lt;span style="FONT: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/font&gt;&lt;/span&gt;&lt;/span&gt;&lt;font color=#000000 size=3&gt;Why
are you so certain that the assembly will not be needed?&lt;/font&gt;&lt;/font&gt;
&lt;/p&gt;
&lt;p class=MsoListParagraph style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l1 level1 lfo1"&gt;
&lt;font face="Times New Roman"&gt;&lt;span style="mso-fareast-font-family: 'Times New Roman'"&gt;&lt;span style="mso-list: Ignore"&gt;&lt;font color=#000000&gt;&lt;font size=3&gt;2.&lt;/font&gt;&lt;span style="FONT: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/font&gt;&lt;/span&gt;&lt;/span&gt;&lt;font color=#000000 size=3&gt;Is
the assembly used in other scenarios, just not this one? There must be a reason that
it is statically referenced by your app in the first place.&lt;/font&gt;&lt;/font&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;
&lt;o:p&gt;
&lt;font face=Calibri color=#000000 size=3&gt;&amp;nbsp;&lt;/font&gt;
&lt;/o:p&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;
&lt;font face=Calibri color=#000000 size=3&gt;As you can guess, this whole exercise is a
bit of a dangerous situation. You can spend a lot of time ensuring that certain code
paths will never be called, and then your users do something unexpected and low-and-behold,
that darned FileNotFound exception is thrown. Ouch.&lt;/font&gt;
&lt;/p&gt;
&lt;h2 style="MARGIN: 10pt 0in 0pt"&gt;&lt;font face=Cambria color=#4f81bd size=4&gt;Why is this
a problem?&lt;/font&gt;
&lt;/h2&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;
&lt;font face=Calibri color=#000000 size=3&gt;Anyway, the core of the problem is at the
level of the JIT (just-in-time compiler). The JIT jits code (MSIL) at a method-level
basis.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;The following is how things work
at a high-level:&lt;/font&gt;
&lt;/p&gt;
&lt;p class=MsoListParagraph style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l0 level1 lfo2"&gt;
&lt;font face="Times New Roman"&gt;&lt;span style="mso-fareast-font-family: 'Times New Roman'"&gt;&lt;span style="mso-list: Ignore"&gt;&lt;font color=#000000&gt;&lt;font size=3&gt;1.&lt;/font&gt;&lt;span style="FONT: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/font&gt;&lt;/span&gt;&lt;/span&gt;&lt;font color=#000000 size=3&gt;The
app calls a method&lt;/font&gt;&lt;/font&gt;
&lt;/p&gt;
&lt;p class=MsoListParagraph style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l0 level1 lfo2"&gt;
&lt;font face="Times New Roman"&gt;&lt;span style="mso-fareast-font-family: 'Times New Roman'"&gt;&lt;span style="mso-list: Ignore"&gt;&lt;font color=#000000&gt;&lt;font size=3&gt;2.&lt;/font&gt;&lt;span style="FONT: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/font&gt;&lt;/span&gt;&lt;/span&gt;&lt;font color=#000000 size=3&gt;The
method cannot be executed because it hasn’t yet been jitted to machine code&lt;/font&gt;&lt;/font&gt;
&lt;/p&gt;
&lt;p class=MsoListParagraph style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l0 level1 lfo2"&gt;
&lt;font face="Times New Roman"&gt;&lt;span style="mso-fareast-font-family: 'Times New Roman'"&gt;&lt;span style="mso-list: Ignore"&gt;&lt;font color=#000000&gt;&lt;font size=3&gt;3.&lt;/font&gt;&lt;span style="FONT: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/font&gt;&lt;/span&gt;&lt;/span&gt;&lt;font color=#000000 size=3&gt;The
JIT compiles the method (MSIL) into x86 (or X64 or IA64) code&lt;/font&gt;&lt;/font&gt;
&lt;/p&gt;
&lt;p class=MsoListParagraph style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l0 level1 lfo2"&gt;
&lt;font face="Times New Roman"&gt;&lt;span style="mso-fareast-font-family: 'Times New Roman'"&gt;&lt;span style="mso-list: Ignore"&gt;&lt;font color=#000000&gt;&lt;font size=3&gt;4.&lt;/font&gt;&lt;span style="FONT: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/font&gt;&lt;/span&gt;&lt;/span&gt;&lt;font color=#000000 size=3&gt;For
every method call that the JIT sees, it must fully understand the signature of that
method, specifically the return type and the arguments/parameters. This requirement
may cause an assembly load.&lt;/font&gt;&lt;/font&gt;
&lt;/p&gt;
&lt;p class=MsoListParagraph style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l0 level1 lfo2"&gt;
&lt;font face="Times New Roman"&gt;&lt;span style="mso-fareast-font-family: 'Times New Roman'"&gt;&lt;span style="mso-list: Ignore"&gt;&lt;font color=#000000&gt;&lt;font size=3&gt;5.&lt;/font&gt;&lt;span style="FONT: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/font&gt;&lt;/span&gt;&lt;/span&gt;&lt;font color=#000000 size=3&gt;The
method can now be called. The method will not need to be jitted again (at least in
this app domain).&lt;/font&gt;&lt;/font&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;
&lt;o:p&gt;
&lt;font face=Calibri color=#000000 size=3&gt;&amp;nbsp;&lt;/font&gt;
&lt;/o:p&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;
&lt;font face=Calibri color=#000000 size=3&gt;For example, the return type might be a value
type, requiring the JIT to know how large that type is. To get that information, the
JIT must load the type, and must request that the assembly (in which the type is contained)
be loaded if it is not already loaded. This is still the case even if the method call
is within an if statement, and might not actually be called.&lt;/font&gt;
&lt;/p&gt;
&lt;h2 style="MARGIN: 10pt 0in 0pt"&gt;&lt;font face=Cambria color=#4f81bd size=4&gt;Code that
exposes the problem&lt;/font&gt;
&lt;/h2&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;
&lt;font face=Calibri color=#000000 size=3&gt;As already suggested, you are going to run
into this problem anytime you have a direct cross-assembly method call anywhere in
a method you know that you will call, and hence JIT. This is even true under an if
statement where the condition will not be true in this scenario.&lt;/font&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/span&gt;&lt;span style="COLOR: blue"&gt;static&lt;/span&gt;&lt;font color=#000000&gt; &lt;/font&gt;&lt;span style="COLOR: blue"&gt;void&lt;/span&gt;&lt;font color=#000000&gt; Method1()&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/span&gt;&lt;span style="COLOR: blue"&gt;if&lt;/span&gt;&lt;font color=#000000&gt; (condition)&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/span&gt;&lt;span style="COLOR: green"&gt;//directly
calling method from ClassLibrary2&lt;o:p&gt;&lt;/o:p&gt;
&lt;/span&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/span&gt;&lt;span style="COLOR: teal"&gt;Class2&lt;/span&gt;&lt;font color=#000000&gt;.Multiply(100,
200);&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;
&lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;
&lt;o:p&gt;
&lt;font face=Calibri color=#000000 size=3&gt;&amp;nbsp;&lt;/font&gt;
&lt;/o:p&gt;
&lt;/p&gt;
&lt;h2 style="MARGIN: 10pt 0in 0pt"&gt;&lt;font face=Cambria color=#4f81bd size=4&gt;Code that
avoids this problem&lt;/font&gt;
&lt;/h2&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;
&lt;font face=Calibri color=#000000 size=3&gt;It is pretty easy to avoid this problem. Write
the following code instead.&lt;/font&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/span&gt;&lt;span style="COLOR: blue"&gt;static&lt;/span&gt;&lt;font color=#000000&gt; &lt;/font&gt;&lt;span style="COLOR: blue"&gt;void&lt;/span&gt;&lt;font color=#000000&gt; Method1()&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/span&gt;&lt;span style="COLOR: blue"&gt;if&lt;/span&gt;&lt;font color=#000000&gt; (condition)&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/span&gt;&lt;span style="COLOR: green"&gt;//indirectly
calling method from ClassLibrary2&lt;o:p&gt;&lt;/o:p&gt;
&lt;/span&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/span&gt;&lt;span style="COLOR: teal"&gt;Method2()&lt;/span&gt;&lt;font color=#000000&gt;;&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;
&lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/span&gt;&lt;span style="COLOR: blue"&gt;static&lt;/span&gt;&lt;font color=#000000&gt; &lt;/font&gt;&lt;span style="COLOR: blue"&gt;void&lt;/span&gt;&lt;font color=#000000&gt; Method2()&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/span&gt;&lt;span style="COLOR: green"&gt;//directly
calling method from ClassLibrary2&lt;o:p&gt;&lt;/o:p&gt;
&lt;/span&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/span&gt;&lt;span style="COLOR: teal"&gt;Class2&lt;/span&gt;&lt;font color=#000000&gt;.Multiply(100,
200);&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;
&lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;
&lt;font face=Calibri color=#000000 size=3&gt;As you can see, the call to &lt;/font&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: teal; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;Class2&lt;/span&gt;&lt;font color=#000000&gt;&lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;.Multiply()&lt;/span&gt;&lt;font face=Calibri size=3&gt; is
now an indirect call and does not require the JIT to know anything about the Class2
or require ClassLibrary2 to be loaded (the symptom to be avoided), unless of course
the condition is met and Method2() is called. This trick is a little awkward sometimes,
but it is a great option to at least defer and even completely avoid assembly loads.&lt;/font&gt;&lt;/font&gt;
&lt;/p&gt;
&lt;h2 style="MARGIN: 10pt 0in 0pt"&gt;&lt;font face=Cambria color=#4f81bd size=4&gt;Gotcha&lt;/font&gt;
&lt;/h2&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;
&lt;font face=Calibri color=#000000 size=3&gt;Unfortunately, there is a gotcha. The JIT
does optimize code by inlining methods. When this happens, you lose your indirect
call, and you are now in the same bad boat you were in before. I don’t know much about
the JIT inlining policy, so cannot provide a list of where this happens. This is merely
a caveat that my workaround doesn’t work in all cases. &lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/span&gt;If
folks are interested, I can talk to the folks on the JIT and perf teams to learn more
about this gotcha.&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://hoser.lander.ca/aggbug.ashx?id=9595f71a-5137-4d06-b9c0-8bd1b0726439" /&gt;</description>
      <comments>http://hoser.lander.ca/CommentView,guid,9595f71a-5137-4d06-b9c0-8bd1b0726439.aspx</comments>
      <category>Code Samples</category>
      <category>Loader</category>
    </item>
    <item>
      <trackback:ping>http://hoser.lander.ca/Trackback.aspx?guid=9807acb4-9197-44fc-bb37-35fce1974cbd</trackback:ping>
      <pingback:server>http://hoser.lander.ca/pingback.aspx</pingback:server>
      <pingback:target>http://hoser.lander.ca/PermaLink,guid,9807acb4-9197-44fc-bb37-35fce1974cbd.aspx</pingback:target>
      <dc:creator>Rich Lander</dc:creator>
      <wfw:comment>http://hoser.lander.ca/CommentView,guid,9807acb4-9197-44fc-bb37-35fce1974cbd.aspx</wfw:comment>
      <wfw:commentRss>http://hoser.lander.ca/SyndicationService.asmx/GetEntryCommentsRss?guid=9807acb4-9197-44fc-bb37-35fce1974cbd</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
We often talk about the loaded assemblies list over here. This is the list of assemblies
currently loaded in the app domain. Big surprise. There isn't anything incredible
special about this list. It is pretty easy to access and use. At the minimum, you
can print out the list of assemblies. Beyond that, you can load and instantiate any
of the types within those assemblies. That's where reflection comes in.
</p>
        <p>
Here's some code that prints out the list of loaded assemblies:
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">using</span> System;<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">using</span> System.Reflection;<br /><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">namespace</span> GetLoadedAssemblies<br />
{<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">class</span> Program<br />
{<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">static</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span> Main(<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">string</span>[]
args)<br />
{<br />
Assembly[] asms;<br /><br />
asms <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> AppDomain.CurrentDomain.GetAssemblies();<br /><br />
Console.WriteLine(<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"There
are {0} assemblies loaded:"</span>, asms.Length);<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">foreach</span> (Assembly
asm <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">in</span> asms)<br />
{<br />
Console.WriteLine(asm.FullName);<br />
}<br /><br />
Console.WriteLine();<br />
Console.WriteLine(<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"The
currently executing assembly is:"</span>);<br />
Console.WriteLine(Assembly.GetExecutingAssembly().FullName);<br /><br />
}<br />
}<br />
}<br /></span>
        </pre>
        <img width="0" height="0" src="http://hoser.lander.ca/aggbug.ashx?id=9807acb4-9197-44fc-bb37-35fce1974cbd" />
      </body>
      <title>Getting the list of loaded assemblies</title>
      <guid isPermaLink="false">http://hoser.lander.ca/PermaLink,guid,9807acb4-9197-44fc-bb37-35fce1974cbd.aspx</guid>
      <link>http://hoser.lander.ca/2006/12/05/GettingTheListOfLoadedAssemblies.aspx</link>
      <pubDate>Tue, 05 Dec 2006 23:51:00 GMT</pubDate>
      <description>&lt;p&gt;
We often talk about the loaded assemblies list over here. This is the list of assemblies
currently loaded in the app domain. Big surprise. There isn't anything incredible
special about this list. It is pretty easy to access and use. At the minimum, you
can print out the list of assemblies. Beyond that, you can load and instantiate any
of the types within those assemblies. That's where reflection comes in.
&lt;/p&gt;
&lt;p&gt;
Here's some code that prints out the list of loaded assemblies:
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; System;&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; System.Reflection;&lt;br&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;namespace&lt;/span&gt; GetLoadedAssemblies&lt;br&gt;
{&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;class&lt;/span&gt; Program&lt;br&gt;
{&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;static&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;void&lt;/span&gt; Main(&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;string&lt;/span&gt;[]
args)&lt;br&gt;
{&lt;br&gt;
Assembly[] asms;&lt;br&gt;
&lt;br&gt;
asms &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; AppDomain.CurrentDomain.GetAssemblies();&lt;br&gt;
&lt;br&gt;
Console.WriteLine(&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"There
are {0} assemblies loaded:"&lt;/span&gt;, asms.Length);&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;foreach&lt;/span&gt; (Assembly
asm &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;in&lt;/span&gt; asms)&lt;br&gt;
{&lt;br&gt;
Console.WriteLine(asm.FullName);&lt;br&gt;
}&lt;br&gt;
&lt;br&gt;
Console.WriteLine();&lt;br&gt;
Console.WriteLine(&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"The
currently executing assembly is:"&lt;/span&gt;);&lt;br&gt;
Console.WriteLine(Assembly.GetExecutingAssembly().FullName);&lt;br&gt;
&lt;br&gt;
}&lt;br&gt;
}&lt;br&gt;
}&lt;br&gt;
&lt;/span&gt;&lt;/pre&gt;&lt;img width="0" height="0" src="http://hoser.lander.ca/aggbug.ashx?id=9807acb4-9197-44fc-bb37-35fce1974cbd" /&gt;</description>
      <comments>http://hoser.lander.ca/CommentView,guid,9807acb4-9197-44fc-bb37-35fce1974cbd.aspx</comments>
      <category>AppDomains</category>
      <category>Code Samples</category>
      <category>Loader</category>
    </item>
    <item>
      <trackback:ping>http://hoser.lander.ca/Trackback.aspx?guid=0947990c-3ca6-4e15-9a39-cfc609b5141b</trackback:ping>
      <pingback:server>http://hoser.lander.ca/pingback.aspx</pingback:server>
      <pingback:target>http://hoser.lander.ca/PermaLink,guid,0947990c-3ca6-4e15-9a39-cfc609b5141b.aspx</pingback:target>
      <dc:creator>Rich Lander</dc:creator>
      <wfw:comment>http://hoser.lander.ca/CommentView,guid,0947990c-3ca6-4e15-9a39-cfc609b5141b.aspx</wfw:comment>
      <wfw:commentRss>http://hoser.lander.ca/SyndicationService.asmx/GetEntryCommentsRss?guid=0947990c-3ca6-4e15-9a39-cfc609b5141b</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I took another look at the FileVersion sample. I wish the API was actually a little
different. The API actually makes sense as a general use API, but it isn't as user-friendly
as I would like. I wish that there were an instance method on the Assembly class called
"GetFileVersion" or something like that it took nothing and returned a Version class.
</p>
        <p>
Here is more of less what it would look like, except that the GetFileVersion wouldn't
be static, it wouldn't take anything and would be on the assembly class.
</p>
        <p>
If you look @ the FileVersion class, there is a lot of stuff on there, and it is a
super wonky API anyway. I don't understand why it has a single static method that
more or less acts that the instance constructor. Why not just have a constructor that
takes a string or a FileInfo or even a FileStream. Bad API design. I prefer the Version
class a lot more since it is super simple. I
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">using</span> System;<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">using</span> System.Reflection;<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">using</span> System.Diagnostics;<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">namespace</span> FileVersion<br />
{<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">class</span> Program<br />
{<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">static</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span> Main(<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">string</span>[]
args)<br />
{<br />
Assembly asm;<br />
Version ver;<br /><br />
asm <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> Assembly.Load(<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"mscorlib,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"</span>);<br />
ver <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> GetFileVersion(asm);<br />
Console.WriteLine(ver.ToString());<br />
}<br /><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">static</span> Version
GetFileVersion(Assembly asm)<br />
{<br />
FileVersionInfo versionInfo;<br />
Version ver;<br /><br />
versionInfo <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> FileVersionInfo.GetVersionInfo(asm.Location);<br />
ver <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span> Version(versionInfo.FileMajorPart,
versionInfo.FileMinorPart, versionInfo.FileBuildPart, versionInfo.FilePrivatePart);<br /><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">return</span> ver;<br />
}<br />
}<br />
}</span>
        </pre>
        <p>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <font face="Verdana" color="#003300" size="2">If
you look @ the FileVersion class, there is a lot of stuff on there, and it is a super
wonky API anyway. I don't understand why it has a single static method that more or
less acts like an instance constructor. Why not just have a constructor that takes
a string or a FileInfo or even a FileStream. Bad API design. I prefer the above method
(for the assembly case) that returns the Version class since it is super simple. I
realize that the native file version is a string, so can contain more stuff, but the
4-part version number is really all I want.</font>
            <br />
          </span>
        </p>
        <img width="0" height="0" src="http://hoser.lander.ca/aggbug.ashx?id=0947990c-3ca6-4e15-9a39-cfc609b5141b" />
      </body>
      <title>FileVersion Sample</title>
      <guid isPermaLink="false">http://hoser.lander.ca/PermaLink,guid,0947990c-3ca6-4e15-9a39-cfc609b5141b.aspx</guid>
      <link>http://hoser.lander.ca/2006/11/29/FileVersionSample.aspx</link>
      <pubDate>Wed, 29 Nov 2006 03:39:29 GMT</pubDate>
      <description>&lt;p&gt;
I took another look at the FileVersion sample. I wish the API was actually a little
different. The API actually makes sense as a general use API, but it isn't as user-friendly
as I would like. I wish that there were an instance method on the Assembly class called
"GetFileVersion" or something like that it took nothing and returned a Version class.
&lt;/p&gt;
&lt;p&gt;
Here is more of less what it would look like, except that the GetFileVersion wouldn't
be static, it wouldn't take anything and would be on the assembly class.
&lt;/p&gt;
&lt;p&gt;
If you look @ the FileVersion class, there is a lot of stuff on there, and it is a
super wonky API anyway. I don't understand why it has a single static method that
more or less acts that the instance constructor. Why not just have a constructor that
takes a string or a FileInfo or even a FileStream. Bad API design. I prefer the Version
class a lot more since it is super simple. I
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; System;&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; System.Reflection;&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; System.Diagnostics;&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;namespace&lt;/span&gt; FileVersion&lt;br&gt;
{&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;class&lt;/span&gt; Program&lt;br&gt;
{&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;static&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;void&lt;/span&gt; Main(&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;string&lt;/span&gt;[]
args)&lt;br&gt;
{&lt;br&gt;
Assembly asm;&lt;br&gt;
Version ver;&lt;br&gt;
&lt;br&gt;
asm &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; Assembly.Load(&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"mscorlib,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"&lt;/span&gt;);&lt;br&gt;
ver &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; GetFileVersion(asm);&lt;br&gt;
Console.WriteLine(ver.ToString());&lt;br&gt;
}&lt;br&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;static&lt;/span&gt; Version
GetFileVersion(Assembly asm)&lt;br&gt;
{&lt;br&gt;
FileVersionInfo versionInfo;&lt;br&gt;
Version ver;&lt;br&gt;
&lt;br&gt;
versionInfo &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; FileVersionInfo.GetVersionInfo(asm.Location);&lt;br&gt;
ver &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; Version(versionInfo.FileMajorPart,
versionInfo.FileMinorPart, versionInfo.FileBuildPart, versionInfo.FilePrivatePart);&lt;br&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;return&lt;/span&gt; ver;&lt;br&gt;
}&lt;br&gt;
}&lt;br&gt;
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;font face=Verdana color=#003300 size=2&gt;If
you look @ the FileVersion class, there is a lot of stuff on there, and it is a super
wonky API anyway. I don't understand why it has a single static method that more or
less acts like an instance constructor. Why not just have a constructor that takes
a string or a FileInfo or even a FileStream. Bad API design. I prefer the above method
(for the assembly case) that returns the Version class since it is super simple. I
realize that the native file version is a string, so can contain more stuff, but the
4-part version number is really all I want.&lt;/font&gt;
&lt;br&gt;
&lt;/p&gt;
&gt;&lt;img width="0" height="0" src="http://hoser.lander.ca/aggbug.ashx?id=0947990c-3ca6-4e15-9a39-cfc609b5141b" /&gt;</description>
      <comments>http://hoser.lander.ca/CommentView,guid,0947990c-3ca6-4e15-9a39-cfc609b5141b.aspx</comments>
      <category>Code Samples</category>
      <category>Loader</category>
      <category>Versioning</category>
    </item>
    <item>
      <trackback:ping>http://hoser.lander.ca/Trackback.aspx?guid=59fd3932-1564-4412-b94f-edf4a447302e</trackback:ping>
      <pingback:server>http://hoser.lander.ca/pingback.aspx</pingback:server>
      <pingback:target>http://hoser.lander.ca/PermaLink,guid,59fd3932-1564-4412-b94f-edf4a447302e.aspx</pingback:target>
      <dc:creator>Rich Lander</dc:creator>
      <wfw:comment>http://hoser.lander.ca/CommentView,guid,59fd3932-1564-4412-b94f-edf4a447302e.aspx</wfw:comment>
      <wfw:commentRss>http://hoser.lander.ca/SyndicationService.asmx/GetEntryCommentsRss?guid=59fd3932-1564-4412-b94f-edf4a447302e</wfw:commentRss>
      <title>Native File Version</title>
      <guid isPermaLink="false">http://hoser.lander.ca/PermaLink,guid,59fd3932-1564-4412-b94f-edf4a447302e.aspx</guid>
      <link>http://hoser.lander.ca/2006/11/29/NativeFileVersion.aspx</link>
      <pubDate>Wed, 29 Nov 2006 00:00:36 GMT</pubDate>
      <description>&lt;p class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;
&lt;font face=Calibri color=#000000 size=3&gt;The file version number is a native code concept
– meaning not originating from the .NET Framework. This version number is a resource
found within the resource section of the &lt;/font&gt;&lt;a href="http://www.microsoft.com/whdc/system/platform/firmware/PECOFF.mspx"&gt;&lt;font face=Calibri size=3&gt;windows
PE (portable executable) format&lt;/font&gt;&lt;/a&gt;&lt;font face=Calibri color=#000000 size=3&gt; of
a managed or native code dll. This resource is named “FILEVERSION”. This version number
is used for information purposes only, not for any runtime purposes such as binding.
In addition, this version number does not have to conform to a particular format,
but is only a string, although it does typically takes the form of a simple four-part
number (i.e. 1.2.3.4).&lt;/font&gt;
&lt;/p&gt;
&lt;h3 style="MARGIN: 10pt 0in 0pt"&gt;&lt;font face=Cambria color=#4f81bd size=3&gt;Reading the
File Version&lt;/font&gt;
&lt;/h3&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;
&lt;font face=Calibri color=#000000 size=3&gt;The easiest way to view this number is to
view the properties of a file in Windows Explorer. The version number listed is the
file version number. The product version is also listed, although I don’t know how
the two numbers differ exactly. Naturally, you can access the file version from code.
The following code does just that, largely using the &lt;/font&gt;&lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt;System.Diagnostics.&lt;/font&gt;&lt;span style="COLOR: teal"&gt;FileVersionInfo&lt;/span&gt;&lt;/span&gt;&lt;font face=Calibri color=#000000 size=3&gt; class,
which I’ve never used before. In fact, I had to ask someone else on the loader team
for that information.&lt;/font&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;using&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt;&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt; System;&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;using&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt; System.Reflection;&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;using&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt; System.Diagnostics;&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;namespace&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt; FileVersion&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt;{&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/span&gt;&lt;span style="COLOR: blue"&gt;class&lt;/span&gt;&lt;font color=#000000&gt; &lt;/font&gt;&lt;span style="COLOR: teal"&gt;Program&lt;o:p&gt;&lt;/o:p&gt;
&lt;/span&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/span&gt;&lt;span style="COLOR: blue"&gt;static&lt;/span&gt;&lt;font color=#000000&gt; &lt;/font&gt;&lt;span style="COLOR: blue"&gt;void&lt;/span&gt;&lt;font color=#000000&gt; Main(&lt;/font&gt;&lt;span style="COLOR: blue"&gt;string&lt;/span&gt;&lt;font color=#000000&gt;[]
args)&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/span&gt;&lt;span style="COLOR: teal"&gt;Assembly&lt;/span&gt;&lt;font color=#000000&gt; asm
= &lt;/font&gt;&lt;span style="COLOR: teal"&gt;Assembly&lt;/span&gt;&lt;font color=#000000&gt;.Load(&lt;/font&gt;&lt;span style="COLOR: maroon"&gt;"mscorlib,
Version=2.0.0.0, Culture=neutral, &amp;nbsp;PublicKeyToken=b77a5c561934e089"&lt;/span&gt;&lt;font color=#000000&gt;);&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.Diagnostics.&lt;/font&gt;&lt;span style="COLOR: teal"&gt;FileVersionInfo&lt;/span&gt;&lt;font color=#000000&gt; fvi
= &lt;/font&gt;&lt;span style="COLOR: teal"&gt;FileVersionInfo&lt;/span&gt;&lt;font color=#000000&gt;.GetVersionInfo(asm.Location);&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/span&gt;&lt;span style="COLOR: teal"&gt;Console&lt;/span&gt;&lt;font color=#000000&gt;.WriteLine(fvi.FileVersion);&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt;}&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;
&lt;o:p&gt;
&lt;font face=Calibri color=#000000 size=3&gt;&amp;nbsp;&lt;/font&gt;
&lt;/o:p&gt;
&lt;/p&gt;
&lt;h3 style="MARGIN: 10pt 0in 0pt"&gt;&lt;font face=Cambria color=#4f81bd size=3&gt;Setting the
File Version&lt;/font&gt;
&lt;/h3&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;
&lt;font face=Calibri color=#000000 size=3&gt;The CLR provides an assembly-level custom
attribute to set this version number for an assembly from managed code. This attribute
is called &lt;/font&gt;&lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt;System.Reflection.&lt;/font&gt;&lt;span style="COLOR: teal"&gt;AssemblyFileVersion&lt;/span&gt;&lt;/span&gt;&lt;font face=Calibri color=#000000 size=3&gt;.
You can see how to set it below.&lt;/font&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;using&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt; System;&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;using&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt; System.Reflection;&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;
&lt;o:p&gt;
&lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
&lt;/o:p&gt;
&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt;[assembly:System.Reflection.&lt;/font&gt;&lt;span style="COLOR: teal"&gt;AssemblyFileVersion&lt;/span&gt;&lt;font color=#000000&gt;(&lt;/font&gt;&lt;span style="COLOR: maroon"&gt;"2.3.4.5"&lt;/span&gt;&lt;font color=#000000&gt;)]&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;
&lt;o:p&gt;
&lt;font color=#000000&gt;&amp;nbsp;&lt;/font&gt;
&lt;/o:p&gt;
&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;namespace&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt; ConsoleApplication1&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt;{&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/span&gt;&lt;span style="COLOR: blue"&gt;class&lt;/span&gt;&lt;font color=#000000&gt; &lt;/font&gt;&lt;span style="COLOR: teal"&gt;Program&lt;o:p&gt;&lt;/o:p&gt;
&lt;/span&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;font color=#000000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/span&gt;&lt;span style="COLOR: blue"&gt;static&lt;/span&gt;&lt;font color=#000000&gt; &lt;/font&gt;&lt;span style="COLOR: blue"&gt;void&lt;/span&gt;&lt;font color=#000000&gt; Main(&lt;/font&gt;&lt;span style="COLOR: blue"&gt;string&lt;/span&gt;&lt;font color=#000000&gt;[]
args)&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Console.WriteLine(&lt;/font&gt;&lt;span style="COLOR: maroon"&gt;"I
just set the file version!"&lt;/span&gt;&lt;font color=#000000&gt;);&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt;&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;font color=#000000&gt;}&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;
&lt;o:p&gt;
&lt;font face=Calibri color=#000000 size=3&gt;&amp;nbsp;&lt;/font&gt;
&lt;/o:p&gt;
&lt;/p&gt;
&lt;p class=MsoNormal style="MARGIN: 0in 0in 10pt"&gt;
&lt;font face=Calibri color=#000000 size=3&gt;You can actually set this attribute in Visual
Studio 2005 via the properties menu. In that case, you cannot set it in code, as I’ve
done above, since you’ll then have two instances of the attribute. You only need to
set the attribute directly, as I’ve done&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;above,
if you are using the compiler directly, from the commandline.&lt;/font&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://hoser.lander.ca/aggbug.ashx?id=59fd3932-1564-4412-b94f-edf4a447302e" /&gt;</description>
      <comments>http://hoser.lander.ca/CommentView,guid,59fd3932-1564-4412-b94f-edf4a447302e.aspx</comments>
      <category>Code Samples</category>
      <category>Loader</category>
      <category>Versioning</category>
    </item>
    <item>
      <trackback:ping>http://hoser.lander.ca/Trackback.aspx?guid=eec48629-9409-4526-832e-e9ad4553a82b</trackback:ping>
      <pingback:server>http://hoser.lander.ca/pingback.aspx</pingback:server>
      <pingback:target>http://hoser.lander.ca/PermaLink,guid,eec48629-9409-4526-832e-e9ad4553a82b.aspx</pingback:target>
      <dc:creator>Rich Lander</dc:creator>
      <wfw:comment>http://hoser.lander.ca/CommentView,guid,eec48629-9409-4526-832e-e9ad4553a82b.aspx</wfw:comment>
      <wfw:commentRss>http://hoser.lander.ca/SyndicationService.asmx/GetEntryCommentsRss?guid=eec48629-9409-4526-832e-e9ad4553a82b</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
The CLR loader offers a few APIs for loading assemblies, each of which have
a slightly different model and behaviour for how they go about the task. I'd like
to cover those and hopefully set folks on the best path for using loading assemblies.
</p>
        <p>
The APIs that I have in mind are:
</p>
        <ul>
          <li>
Assembly.Load(), and 
</li>
          <li>
Assembly.LoadWithPartialName()</li>
        </ul>
        <p>
First, you need to understand the concepts of fully-specified and partial assembly
names.  A fully specified name contains the following parts: the simple name,
version, culture, and public key or public key token. A partial name contains at least
the simple name and optionally any of the other parts of the full-specified name.
This concept is most relevant to the textual identity of an assembly. For example,
here is the fully-specified assembly textual identity for v2.0 System.dll: "System,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089". The textual identity
is a specially formatted string that is used to describe some or all of the aspects
of identifying an assembly. 
</p>
        <p>
Assembly.Load() takes a string as one of the overloads. The textual identity, as you
see formatted above, is just the sort of string that Load() is expecting. Load() also
takes other types, such as AssemblyName, but that's not important for the moment.
The textual identity can be fully or partially specified, and which one it is has
an important impact on the way that these loader APIs operate. The one you see above
for System.dll is fully specified, since all the parts are there. Note that there
is an additional part (or attribute) of the textual indentity that I've missed -- ProcessorArchitecture
-- but that complicates the discussion too much for not much benefit, so I'm
not going to cover it here.
</p>
        <p>
The following bit of code shows the use of these APIs with full- and partially-specified
names. The comments inline should pretty well explain what to expect
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">using</span> System;<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">using</span> System.Reflection;<br /><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">namespace</span> BindingByIdentity<br />
{<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">class</span> Program<br />
{<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">static</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span> Main(<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">string</span>[]
args)<br />
{<br />
Assembly asm;<br /><br /><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">//LoadWithPartialName()
looks in both the appbase and the GAC, looking for the best match,</span><br /><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">//where
"best match" isn't very clearly defined</span><br /><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">//In
addition, this method has been deprecated, which means "don't use this anymore, and
it may be removed later"</span><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">try</span><br />
{<br />
Console.WriteLine(<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"Calling
LoadWithPartialName"</span>);<br />
asm <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> Assembly.LoadWithPartialName(<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"System"</span>);<br />
Console.WriteLine(<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"Loaded:
{0}"</span>,asm.FullName);<br />
}<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">catch</span>(Exception
e)<br />
{<br />
Console.WriteLine(<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"LoadWithPartialName
threw with the following exception:"</span>);<br />
Console.WriteLine(e.Message);<br />
}<br /><br /><br /><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">//Load()
with a partial name looks only in the appbase</span><br /><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">//There
is no gaurantee that you will get the assembly that you want</span><br /><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">//In
the case that you have private bin paths specified, there are no order gaurantees</span><br /><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">//This
particular use of the Assembly.Load() will always throw, since this assembly will
not be found (because it is GAC'd)</span><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">try</span><br />
{<br />
Console.WriteLine(<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"Calling
Load"</span>);<br />
asm <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> Assembly.Load(<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"System,
PublicKeyToken=b77a5c561934e089"</span>);<br />
Console.WriteLine(<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"Loaded:
{0}"</span>, asm.FullName);<br />
}<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">catch</span>(Exception
e)<br />
{<br />
Console.WriteLine(<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"Load
threw with the following exception:"</span>);<br />
Console.WriteLine(e.Message);<br />
}<br /><br /><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">//Load()
with a fully-specified name looks in the appbase and the GAC and gaurantees that you</span><br /><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">//will
get the assembly that you asked for</span><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">try</span><br />
{<br />
Console.WriteLine(<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"Calling
Load"</span>);<br />
asm <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> Assembly.Load(<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"System,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"</span>);<br />
Console.WriteLine(<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"Loaded:
{0}"</span>, asm.FullName);<br />
}<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">catch</span> (Exception
e)<br />
{<br />
Console.WriteLine(<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"Load
threw with the following exception:"</span>);<br />
Console.WriteLine(e.Message);<br />
}<br /><br /><br />
}<br />
}<br />
}<br /></span>
        </pre>
        <p>
Output:
</p>
        <p>
          <font color="#000000">Calling LoadWithPartialName<br />
Loaded: System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089<br />
Calling Load<br />
Load threw with the following exception:<br />
Could not load file or assembly 'System, PublicKeyToken=b77a5c561934e089' or one<br />
 of its dependencies. The system cannot find the file specified.<br />
Calling Load<br />
Loaded: System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</font>
        </p>
        <p>
The moral of this story is that Assembly.LoadWithPartialName() is always bad and shouldn't
be used since its behaviour isn't well-defined or predictable, and that you should
always fully-specify the textual identity strings that you pass into Assembly.Load().
It may be the case that partially specified strings "work" since the assemblies you
are attempted to load are always going to be in the app-base, but the fully-specified
names do make your code and your intentions more clear, particularly for the next
guy that has to look at the code you wrote. The funny thing is that sometimes "the
next guy" is you, just two months later ;)
</p>
        <p>
There is also the case that your code might not be strong-named. You should still
specify everything but the publickey/token in that case.
</p>
        <img width="0" height="0" src="http://hoser.lander.ca/aggbug.ashx?id=eec48629-9409-4526-832e-e9ad4553a82b" />
      </body>
      <title>Loading by Identity</title>
      <guid isPermaLink="false">http://hoser.lander.ca/PermaLink,guid,eec48629-9409-4526-832e-e9ad4553a82b.aspx</guid>
      <link>http://hoser.lander.ca/2006/10/26/LoadingByIdentity.aspx</link>
      <pubDate>Thu, 26 Oct 2006 06:43:59 GMT</pubDate>
      <description>&lt;p&gt;
The&amp;nbsp;CLR loader&amp;nbsp;offers a few APIs for loading assemblies, each of which have
a slightly different model and behaviour for how they go about the task. I'd like
to cover those and hopefully set folks on the best path for using loading assemblies.
&lt;/p&gt;
&lt;p&gt;
The APIs that I have in mind are:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
Assembly.Load(), and 
&lt;li&gt;
Assembly.LoadWithPartialName()&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
First, you need to understand the concepts of fully-specified and partial assembly
names. &amp;nbsp;A fully specified name contains the following parts: the simple name,
version, culture, and public key or public key token. A partial name contains at least
the simple name and optionally any of the other parts of the full-specified name.
This concept is most relevant to the textual identity of an assembly. For example,
here is the fully-specified assembly textual identity for v2.0 System.dll: "System,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089". The textual identity
is a specially formatted string that is used to describe some or all of the aspects
of identifying an assembly. 
&lt;/p&gt;
&lt;p&gt;
Assembly.Load() takes a string as one of the overloads. The textual identity, as you
see formatted above, is just the sort of string that Load() is expecting. Load() also
takes other types, such as AssemblyName, but that's not important for the moment.
The textual identity can be fully or partially specified, and which one it is has
an important impact on the way that these loader APIs operate. The one you see above
for System.dll is fully specified, since all the parts are there. Note that there
is an additional part (or attribute) of the textual indentity that I've missed --&amp;nbsp;ProcessorArchitecture
--&amp;nbsp;but that complicates the discussion too much for not much benefit, so I'm
not going to cover it here.
&lt;/p&gt;
&lt;p&gt;
The following bit of code shows the use of these APIs with full- and partially-specified
names. The comments inline should pretty well explain what to expect
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; System;&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; System.Reflection;&lt;br&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;namespace&lt;/span&gt; BindingByIdentity&lt;br&gt;
{&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;class&lt;/span&gt; Program&lt;br&gt;
{&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;static&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;void&lt;/span&gt; Main(&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;string&lt;/span&gt;[]
args)&lt;br&gt;
{&lt;br&gt;
Assembly asm;&lt;br&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;//LoadWithPartialName()
looks in both the appbase and the GAC, looking for the best match,&lt;/span&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;//where
"best match" isn't very clearly defined&lt;/span&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;//In
addition, this method has been deprecated, which means "don't use this anymore, and
it may be removed later"&lt;/span&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;try&lt;/span&gt;
&lt;br&gt;
{&lt;br&gt;
Console.WriteLine(&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"Calling
LoadWithPartialName"&lt;/span&gt;);&lt;br&gt;
asm &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; Assembly.LoadWithPartialName(&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"System"&lt;/span&gt;);&lt;br&gt;
Console.WriteLine(&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"Loaded:
{0}"&lt;/span&gt;,asm.FullName);&lt;br&gt;
}&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;catch&lt;/span&gt;(Exception
e)&lt;br&gt;
{&lt;br&gt;
Console.WriteLine(&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"LoadWithPartialName
threw with the following exception:"&lt;/span&gt;);&lt;br&gt;
Console.WriteLine(e.Message);&lt;br&gt;
}&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;//Load()
with a partial name looks only in the appbase&lt;/span&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;//There
is no gaurantee that you will get the assembly that you want&lt;/span&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;//In
the case that you have private bin paths specified, there are no order gaurantees&lt;/span&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;//This
particular use of the Assembly.Load() will always throw, since this assembly will
not be found (because it is GAC'd)&lt;/span&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;try&lt;/span&gt;
&lt;br&gt;
{&lt;br&gt;
Console.WriteLine(&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"Calling
Load"&lt;/span&gt;);&lt;br&gt;
asm &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; Assembly.Load(&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"System,
PublicKeyToken=b77a5c561934e089"&lt;/span&gt;);&lt;br&gt;
Console.WriteLine(&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"Loaded:
{0}"&lt;/span&gt;, asm.FullName);&lt;br&gt;
}&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;catch&lt;/span&gt;(Exception
e)&lt;br&gt;
{&lt;br&gt;
Console.WriteLine(&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"Load
threw with the following exception:"&lt;/span&gt;);&lt;br&gt;
Console.WriteLine(e.Message);&lt;br&gt;
}&lt;br&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;//Load()
with a fully-specified name looks in the appbase and the GAC and gaurantees that you&lt;/span&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;//will
get the assembly that you asked for&lt;/span&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;try&lt;/span&gt;
&lt;br&gt;
{&lt;br&gt;
Console.WriteLine(&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"Calling
Load"&lt;/span&gt;);&lt;br&gt;
asm &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; Assembly.Load(&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"System,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"&lt;/span&gt;);&lt;br&gt;
Console.WriteLine(&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"Loaded:
{0}"&lt;/span&gt;, asm.FullName);&lt;br&gt;
}&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;catch&lt;/span&gt; (Exception
e)&lt;br&gt;
{&lt;br&gt;
Console.WriteLine(&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"Load
threw with the following exception:"&lt;/span&gt;);&lt;br&gt;
Console.WriteLine(e.Message);&lt;br&gt;
}&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
}&lt;br&gt;
}&lt;br&gt;
}&lt;br&gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
Output:
&lt;/p&gt;
&lt;p&gt;
&lt;font color=#000000&gt;Calling LoadWithPartialName&lt;br&gt;
Loaded: System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089&lt;br&gt;
Calling Load&lt;br&gt;
Load threw with the following exception:&lt;br&gt;
Could not load file or assembly 'System, PublicKeyToken=b77a5c561934e089' or one&lt;br&gt;
&amp;nbsp;of its dependencies. The system cannot find the file specified.&lt;br&gt;
Calling Load&lt;br&gt;
Loaded: System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
The moral of this story is that Assembly.LoadWithPartialName() is always bad and shouldn't
be used since its behaviour isn't well-defined or predictable, and that you should
always fully-specify the textual identity strings that you pass into Assembly.Load().
It may be the case that partially specified strings "work" since the assemblies you
are attempted to load are always going to be in the app-base, but the fully-specified
names do make your code and your intentions more clear, particularly for the next
guy that has to look at the code you wrote. The funny thing is that sometimes "the
next guy" is you, just two months later ;)
&lt;/p&gt;
&lt;p&gt;
There is also the case that your code might not be strong-named. You should still
specify everything but the publickey/token in that case.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://hoser.lander.ca/aggbug.ashx?id=eec48629-9409-4526-832e-e9ad4553a82b" /&gt;</description>
      <comments>http://hoser.lander.ca/CommentView,guid,eec48629-9409-4526-832e-e9ad4553a82b.aspx</comments>
      <category>Code Samples</category>
      <category>Loader</category>
      <category>Versioning</category>
    </item>
    <item>
      <trackback:ping>http://hoser.lander.ca/Trackback.aspx?guid=d5cf6962-ad80-4a69-893a-5c1a6f5f3c44</trackback:ping>
      <pingback:server>http://hoser.lander.ca/pingback.aspx</pingback:server>
      <pingback:target>http://hoser.lander.ca/PermaLink,guid,d5cf6962-ad80-4a69-893a-5c1a6f5f3c44.aspx</pingback:target>
      <dc:creator>Rich Lander</dc:creator>
      <wfw:comment>http://hoser.lander.ca/CommentView,guid,d5cf6962-ad80-4a69-893a-5c1a6f5f3c44.aspx</wfw:comment>
      <wfw:commentRss>http://hoser.lander.ca/SyndicationService.asmx/GetEntryCommentsRss?guid=d5cf6962-ad80-4a69-893a-5c1a6f5f3c44</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Here is a better example of using an anonymous delegate that proves, in my mind, that
they are lexical closures. I sure hope <a href="http://hoser.lander.ca/PermaLink,guid,1ae8f9b3-766b-49cc-a3dd-f7aa4cf3bcfa.aspx">my
definition </a>of closures is correct ;)
</p>
        <p>
Here's the code. Please do guess what it does or better yet, compile and run it. The
important aspect is to guess which values of "s" do or do not match.
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">using</span> System;<br /><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">using</span> System.Threading;<br /><br /><br /><br /><br /><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">namespace</span> AnonDelegateTest2<br /><br />
{<br /><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">class</span> Program<br /><br />
{<br /><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">delegate</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span> FunkyDelegate();<br /><br /><br /><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">static</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span> Main(<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">string</span>[]
args)<br /><br />
{<br /><br />
FunkyDelegate fd <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> GetDelegate();<br /><br /><br /><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">for</span> (Int32
i <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> 0;
i &lt; 10; i++)<br /><br />
{<br /><br />
fd();<br /><br />
}<br /><br />
}<br /><br /><br /><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">static</span> FunkyDelegate
GetDelegate()<br /><br />
{<br /><br />
String s <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> DateTime.Now.Ticks.ToString();<br /><br /><br /><br />
FunkyDelegate fd <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">delegate</span>()<br /><br />
{<br /><br />
Console.WriteLine(<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"Entering
fd"</span>);<br /><br />
Console.WriteLine(s);<br /><br />
s <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> DateTime.Now.Ticks.ToString();<br /><br />
Thread.Sleep(100);<br /><br />
Console.WriteLine(s);<br /><br />
Console.WriteLine(<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"Leaving
fd"</span>);<br /><br />
Console.WriteLine();<br /><br />
};<br /><br /><br /><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">return</span> fd;<br /><br />
}<br /><br />
}<br /><br /><br /><br />
}<br /></span>
        </pre>
        <img width="0" height="0" src="http://hoser.lander.ca/aggbug.ashx?id=d5cf6962-ad80-4a69-893a-5c1a6f5f3c44" />
      </body>
      <title>Another Anonymous Delegate Example </title>
      <guid isPermaLink="false">http://hoser.lander.ca/PermaLink,guid,d5cf6962-ad80-4a69-893a-5c1a6f5f3c44.aspx</guid>
      <link>http://hoser.lander.ca/2005/12/03/AnotherAnonymousDelegateExample.aspx</link>
      <pubDate>Sat, 03 Dec 2005 02:06:27 GMT</pubDate>
      <description>&lt;p&gt;
Here is a better example of using an anonymous delegate that proves, in my mind,&amp;nbsp;that
they are lexical closures. I sure hope &lt;a href="http://hoser.lander.ca/PermaLink,guid,1ae8f9b3-766b-49cc-a3dd-f7aa4cf3bcfa.aspx"&gt;my
definition &lt;/a&gt;of closures is correct ;)
&lt;/p&gt;
&lt;p&gt;
Here's the code. Please do guess what it does or better yet, compile and run it. The
important aspect is to guess which values of "s" do or do not match.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; System;&lt;br&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; System.Threading;&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;namespace&lt;/span&gt; AnonDelegateTest2&lt;br&gt;
&lt;br&gt;
{&lt;br&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;class&lt;/span&gt; Program&lt;br&gt;
&lt;br&gt;
{&lt;br&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;delegate&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;void&lt;/span&gt; FunkyDelegate();&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;static&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;void&lt;/span&gt; Main(&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;string&lt;/span&gt;[]
args)&lt;br&gt;
&lt;br&gt;
{&lt;br&gt;
&lt;br&gt;
FunkyDelegate fd &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; GetDelegate();&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;for&lt;/span&gt; (Int32
i &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; 0;
i &amp;lt; 10; i++)&lt;br&gt;
&lt;br&gt;
{&lt;br&gt;
&lt;br&gt;
fd();&lt;br&gt;
&lt;br&gt;
}&lt;br&gt;
&lt;br&gt;
}&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;static&lt;/span&gt; FunkyDelegate
GetDelegate()&lt;br&gt;
&lt;br&gt;
{&lt;br&gt;
&lt;br&gt;
String s &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; DateTime.Now.Ticks.ToString();&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
FunkyDelegate fd &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;delegate&lt;/span&gt;()&lt;br&gt;
&lt;br&gt;
{&lt;br&gt;
&lt;br&gt;
Console.WriteLine(&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"Entering
fd"&lt;/span&gt;);&lt;br&gt;
&lt;br&gt;
Console.WriteLine(s);&lt;br&gt;
&lt;br&gt;
s &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; DateTime.Now.Ticks.ToString();&lt;br&gt;
&lt;br&gt;
Thread.Sleep(100);&lt;br&gt;
&lt;br&gt;
Console.WriteLine(s);&lt;br&gt;
&lt;br&gt;
Console.WriteLine(&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"Leaving
fd"&lt;/span&gt;);&lt;br&gt;
&lt;br&gt;
Console.WriteLine();&lt;br&gt;
&lt;br&gt;
};&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;return&lt;/span&gt; fd;&lt;br&gt;
&lt;br&gt;
}&lt;br&gt;
&lt;br&gt;
}&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
}&lt;br&gt;
&lt;/span&gt;&lt;/pre&gt;&lt;img width="0" height="0" src="http://hoser.lander.ca/aggbug.ashx?id=d5cf6962-ad80-4a69-893a-5c1a6f5f3c44" /&gt;</description>
      <comments>http://hoser.lander.ca/CommentView,guid,d5cf6962-ad80-4a69-893a-5c1a6f5f3c44.aspx</comments>
      <category>AnonymousMethods</category>
      <category>Code Samples</category>
    </item>
    <item>
      <trackback:ping>http://hoser.lander.ca/Trackback.aspx?guid=3139853d-eff9-41ff-989f-4f6697946453</trackback:ping>
      <pingback:server>http://hoser.lander.ca/pingback.aspx</pingback:server>
      <pingback:target>http://hoser.lander.ca/PermaLink,guid,3139853d-eff9-41ff-989f-4f6697946453.aspx</pingback:target>
      <dc:creator>Rich Lander</dc:creator>
      <wfw:comment>http://hoser.lander.ca/CommentView,guid,3139853d-eff9-41ff-989f-4f6697946453.aspx</wfw:comment>
      <wfw:commentRss>http://hoser.lander.ca/SyndicationService.asmx/GetEntryCommentsRss?guid=3139853d-eff9-41ff-989f-4f6697946453</wfw:commentRss>
      <slash:comments>5</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I <a href="http://hoser.lander.ca/PermaLink,guid,20c75894-5947-4a62-a9c6-01b14516ecf8.aspx">posted
an interesting bit</a> of C# a couple weeks ago relating to anonymous delegates. I
asked in that post what the result of the following code is. Here is the code and
then the result
</p>
        <p>
          <strong>Code:<br /></strong>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">using</span> System;<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">using</span> System.Threading;<br /><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">namespace</span> AnonMethods<br />
{<br />
    <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">class</span> Program<br />
    {<br />
        <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">static</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span> Main(<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">string</span>[]
args)<br />
        {<br />
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">for</span> (Int32
i <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> 0;
i &lt; 20; i++)<br />
            {<br />
               
Thread t <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span> Thread(<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">delegate</span>()
{ Thread.Sleep(10); Console.WriteLine(i); });<br />
               
t.Start();<br />
            }<br />
            Thread.Sleep(5000);<br />
        }<br />
    }<br />
}<br /></span>
        </p>
        <p>
          <strong>Result:<br /></strong>6<br />
6<br />
6<br />
6<br />
6<br />
9<br />
12<br />
12<br />
12<br />
13<br />
13<br />
13<br />
15<br />
15<br />
19<br />
20<br />
20<br />
20<br />
20<br />
20
</p>
        <p>
          <font color="#000000">
            <strong>Why?<br /></strong>That's the strangest result from a for loop that I've ever seen. First, I'd
suggest that you compile the code for yourself (remember, <a href="http://msdn.microsoft.com/vstudio/express/visualCsharp/default.aspx">C#
Express</a> is free) and then take and then decompile it in either ildasm or <a href="http://www.aisto.com/roeder/dotnet/">Reflector</a>.
I find the C# in Reflector a whole lot easier to read than the IL, so I'd recommend
that you start with it. I imagine that you'll get a real kick out of what you see.</font>
        </p>
        <p>
          <font color="#000000">First, I'd like to caveat my explanation by saying that this
is a CLR guy's explanation of what the C# compiler is doing. I haven't talked to the
C# team, but just took a look at the code emitted by the compiler, which both
you and I are free to ponder. All that being said, here's the basic idea
of what I think is going  on without going into great detail of the mechanics
of anonymous delegates. </font>
        </p>
        <p>
          <font color="#000000">Once the C# source code is compiled, the anonymous delegate
goes away and is replaced by the mechanism that I'm about to describe. Put another
way, anonymous delegates are a C# source-code-only feature. For every anonymous delegate,
there is a strangely, but probably predictably, named nested class added to the
class that indirectly contained (within one of the methods within the class) the
anonymous delegate. The class, as you might expect, exposes some surface area
that is accessed from the parent class. The class is actually private, which means
it cannot be accessed from anywhere but the parent class. There is a default
constructor, which does nothing. There is a method that is the anonymous delegate
transformed into a regular named method. Remember, anonymous delegates are a C#, not
a CLR feature, so the CLR does need a named method to call. Lastly, there are a set
of fields exposed on this compiler-generated class -- and this is the part that gets
interesting -- that are the local variables that are accessed from
both the containing method and the anonymous delegate. Variables that are defined
in the containing method but not accessed from within the anonymous delegate or that
are defined and used within the anonymous delegate do not get this special treatment.
As you might guess, the C# compiler wires up the method that takes the delegate and
the generated named method. In addition, all accesses to the shared local variables
are wired up through the public fields on the generated class instead of through the
actual locals. Very interesting.</font>
        </p>
        <p>
          <font color="#000000">OK, so that's the basic explananation of how anonymous delegates
work. There are some other subtleties that I noticed, but they are not important for
this explanation. I'm going to get further insight on those from the C# team and will
post on those when I learn more about them.</font>
        </p>
        <p>
          <font color="#000000">So, then, how does all that lead to the strange and unexpected
results from the for loop. Well, since the "i" variable is accessed by both the containing
method and the anonymous delegate, the C# compiler rewires all accesses to "i" to
the "i" field on the generated class. And since each call to the anonymous delegate
in our example does its work on a separate thread, then "i" is updated by the for
loop on a different schedule than it is written by Console.WriteLine in the anonymous
delegate. That's why the value printed out by each call to the anonymous
delegate is essentially a race condition between the for loop continually iterating
and threads being </font>
          <font color="#000000">created and getting time on the processor(s).</font>
        </p>
        <p>
          <font color="#000000">Another aspect of this is that shared locals with
anonymous delegates essentially turn value types into reference types. That's
a really strange behaviour and realization.</font>
        </p>
        <p>
          <font color="#000000">Like I said in the earlier post, the trick is determining a
scenario where this behaviour is useful. I haven't found that yet, although I'm sure
it will be very interesting once I find it.</font>
        </p>
        <img width="0" height="0" src="http://hoser.lander.ca/aggbug.ashx?id=3139853d-eff9-41ff-989f-4f6697946453" />
      </body>
      <title>Anonymous Delegates -- Explained</title>
      <guid isPermaLink="false">http://hoser.lander.ca/PermaLink,guid,3139853d-eff9-41ff-989f-4f6697946453.aspx</guid>
      <link>http://hoser.lander.ca/2005/11/30/AnonymousDelegatesExplained.aspx</link>
      <pubDate>Wed, 30 Nov 2005 15:51:11 GMT</pubDate>
      <description>&lt;p&gt;
I &lt;a href="http://hoser.lander.ca/PermaLink,guid,20c75894-5947-4a62-a9c6-01b14516ecf8.aspx"&gt;posted
an interesting bit&lt;/a&gt; of C# a couple weeks ago relating to anonymous delegates. I
asked in that post what the result of the following code is. Here is the code and
then the result
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Code:&lt;br&gt;
&lt;/strong&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; System;&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; System.Threading;&lt;br&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;namespace&lt;/span&gt; AnonMethods&lt;br&gt;
{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;class&lt;/span&gt; Program&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;static&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;void&lt;/span&gt; Main(&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;string&lt;/span&gt;[]
args)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;for&lt;/span&gt; (Int32
i &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; 0;
i &amp;lt; 20; i++)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
Thread t &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; Thread(&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;delegate&lt;/span&gt;()
{ Thread.Sleep(10); Console.WriteLine(i); });&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
t.Start();&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Thread.Sleep(5000);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;
}&lt;br&gt;
&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Result:&lt;br&gt;
&lt;/strong&gt;6&lt;br&gt;
6&lt;br&gt;
6&lt;br&gt;
6&lt;br&gt;
6&lt;br&gt;
9&lt;br&gt;
12&lt;br&gt;
12&lt;br&gt;
12&lt;br&gt;
13&lt;br&gt;
13&lt;br&gt;
13&lt;br&gt;
15&lt;br&gt;
15&lt;br&gt;
19&lt;br&gt;
20&lt;br&gt;
20&lt;br&gt;
20&lt;br&gt;
20&lt;br&gt;
20
&lt;/p&gt;
&lt;p&gt;
&lt;font color=#000000&gt;&lt;strong&gt;Why?&lt;br&gt;
&lt;/strong&gt;That's the strangest result from a for loop that I've ever seen. First, I'd
suggest that you compile the code for yourself (remember, &lt;a href="http://msdn.microsoft.com/vstudio/express/visualCsharp/default.aspx"&gt;C#
Express&lt;/a&gt; is free) and then take and then decompile it in either ildasm or &lt;a href="http://www.aisto.com/roeder/dotnet/"&gt;Reflector&lt;/a&gt;.
I find the C# in Reflector a whole lot easier to read than the IL, so I'd recommend
that you start with it. I imagine that you'll get a real kick out of what you see.&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font color=#000000&gt;First, I'd like to caveat my explanation by saying that this is
a CLR guy's explanation of what the C# compiler is doing. I haven't talked to the
C# team, but&amp;nbsp;just took a look at the code emitted by the compiler, which both
you and I are free to&amp;nbsp;ponder. All that being said,&amp;nbsp;here's the basic idea
of what I think is going&amp;nbsp; on without going into great detail of the mechanics
of anonymous delegates. &lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font color=#000000&gt;Once the C# source code is&amp;nbsp;compiled, the anonymous delegate
goes away and is replaced by the mechanism that I'm about to describe. Put another
way, anonymous delegates are a C# source-code-only feature. For every anonymous delegate,
there is a strangely, but probably predictably, named&amp;nbsp;nested class added to the
class that indirectly contained (within one of the methods within the class)&amp;nbsp;the
anonymous delegate.&amp;nbsp;The class, as you might expect, exposes some surface area
that is accessed from the parent class. The class is actually private, which means
it cannot be accessed from anywhere but the parent class.&amp;nbsp;There is a default
constructor, which does nothing. There is a method that is the anonymous delegate
transformed into a regular named method. Remember, anonymous delegates are a C#, not
a CLR feature, so the CLR does need a named method to call. Lastly, there are a set
of fields exposed on this compiler-generated class -- and this is the part that gets
interesting -- that are the&amp;nbsp;local&amp;nbsp;variables&amp;nbsp;that are accessed from
both the containing method and the anonymous delegate. Variables that are defined
in the containing method but not accessed from within the anonymous delegate or that
are defined and used within the anonymous delegate do not get this special treatment.
As you might guess, the C# compiler wires up the method that takes the delegate and
the generated named method. In addition, all accesses to the shared local variables
are wired up through the public fields on the generated class instead of through the
actual locals. Very interesting.&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font color=#000000&gt;OK, so that's the basic explananation of how anonymous delegates
work. There are some other subtleties that I noticed, but they are not important for
this explanation. I'm going to get further insight on those from the C# team and will
post on those when I learn more about them.&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font color=#000000&gt;So, then, how does all that lead to the strange and unexpected
results from the for loop. Well, since the "i" variable is accessed by both the containing
method and the anonymous delegate, the C# compiler rewires all accesses to "i" to
the "i" field on the generated class. And since each call to the anonymous delegate
in our example does its work on a separate thread, then "i" is updated by the for
loop on a different schedule than it is written by Console.WriteLine in the anonymous
delegate. That's&amp;nbsp;why the value printed&amp;nbsp;out by each call to the anonymous
delegate is essentially a race condition between the for loop continually iterating
and threads being &lt;/font&gt;&lt;font color=#000000&gt;created and getting time on the processor(s).&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font color=#000000&gt;Another aspect of this is that&amp;nbsp;shared locals&amp;nbsp;with anonymous
delegates essentially turn value types into reference types.&amp;nbsp;That's a really
strange behaviour and realization.&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font color=#000000&gt;Like I said in the earlier post, the trick is determining a scenario
where this behaviour is useful. I haven't found that yet, although I'm sure it will
be very interesting once I find it.&lt;/font&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://hoser.lander.ca/aggbug.ashx?id=3139853d-eff9-41ff-989f-4f6697946453" /&gt;</description>
      <comments>http://hoser.lander.ca/CommentView,guid,3139853d-eff9-41ff-989f-4f6697946453.aspx</comments>
      <category>AnonymousMethods</category>
      <category>Code Samples</category>
    </item>
    <item>
      <trackback:ping>http://hoser.lander.ca/Trackback.aspx?guid=20c75894-5947-4a62-a9c6-01b14516ecf8</trackback:ping>
      <pingback:server>http://hoser.lander.ca/pingback.aspx</pingback:server>
      <pingback:target>http://hoser.lander.ca/PermaLink,guid,20c75894-5947-4a62-a9c6-01b14516ecf8.aspx</pingback:target>
      <dc:creator>Rich Lander</dc:creator>
      <wfw:comment>http://hoser.lander.ca/CommentView,guid,20c75894-5947-4a62-a9c6-01b14516ecf8.aspx</wfw:comment>
      <wfw:commentRss>http://hoser.lander.ca/SyndicationService.asmx/GetEntryCommentsRss?guid=20c75894-5947-4a62-a9c6-01b14516ecf8</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
What does the following C# code do? Guess and then run it.
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">using</span> System;<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">using</span> System.Threading;<br /><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">namespace</span> AnonMethods<br />
{<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">class</span> Program<br />
{<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">static</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span> Main(<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">string</span>[]
args)<br />
{<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">for</span> (Int32
i <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> 0;
i &lt; 20; i++)<br />
{<br />
Thread t <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span> Thread(<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">delegate</span>()
{ Thread.Sleep(10); Console.WriteLine(i); });<br />
t.Start();<br />
}<br />
Thread.Sleep(5000);<br />
}<br />
}<br />
}<br /></span>
        </pre>
        <p>
We were playing with this and similar code in the experts area at the Ottawa VS launch
yesterday. Fun, fun.
</p>
        <p>
My task ahead is to find a scenario that makes the subtleties of anonymous delegates
useful. Anonymous delegates are certainly useful. It is the trick above with "i" that
I'm talking about. I'll post in a couple days with more data on what is going on.
</p>
        <img width="0" height="0" src="http://hoser.lander.ca/aggbug.ashx?id=20c75894-5947-4a62-a9c6-01b14516ecf8" />
      </body>
      <title>Anonymous Delegates</title>
      <guid isPermaLink="false">http://hoser.lander.ca/PermaLink,guid,20c75894-5947-4a62-a9c6-01b14516ecf8.aspx</guid>
      <link>http://hoser.lander.ca/2005/11/11/AnonymousDelegates.aspx</link>
      <pubDate>Fri, 11 Nov 2005 14:51:50 GMT</pubDate>
      <description>&lt;p&gt;
What does the following C# code do? Guess and then run it.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; System;&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; System.Threading;&lt;br&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;namespace&lt;/span&gt; AnonMethods&lt;br&gt;
{&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;class&lt;/span&gt; Program&lt;br&gt;
{&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;static&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;void&lt;/span&gt; Main(&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;string&lt;/span&gt;[]
args)&lt;br&gt;
{&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;for&lt;/span&gt; (Int32
i &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; 0;
i &amp;lt; 20; i++)&lt;br&gt;
{&lt;br&gt;
Thread t &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; Thread(&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;delegate&lt;/span&gt;()
{ Thread.Sleep(10); Console.WriteLine(i); });&lt;br&gt;
t.Start();&lt;br&gt;
}&lt;br&gt;
Thread.Sleep(5000);&lt;br&gt;
}&lt;br&gt;
}&lt;br&gt;
}&lt;br&gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
We were playing with this and similar code in the experts area at the Ottawa VS launch
yesterday. Fun, fun.
&lt;/p&gt;
&lt;p&gt;
My task ahead is to find a scenario that makes the subtleties of anonymous delegates
useful. Anonymous delegates are certainly useful. It is the trick above with "i" that
I'm talking about. I'll post in a couple days with more data on what is going on.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://hoser.lander.ca/aggbug.ashx?id=20c75894-5947-4a62-a9c6-01b14516ecf8" /&gt;</description>
      <comments>http://hoser.lander.ca/CommentView,guid,20c75894-5947-4a62-a9c6-01b14516ecf8.aspx</comments>
      <category>AnonymousMethods</category>
      <category>Code Samples</category>
    </item>
    <item>
      <trackback:ping>http://hoser.lander.ca/Trackback.aspx?guid=7fcc7091-e486-4f69-8088-6d04ba217da7</trackback:ping>
      <pingback:server>http://hoser.lander.ca/pingback.aspx</pingback:server>
      <pingback:target>http://hoser.lander.ca/PermaLink,guid,7fcc7091-e486-4f69-8088-6d04ba217da7.aspx</pingback:target>
      <dc:creator>Rich Lander</dc:creator>
      <wfw:comment>http://hoser.lander.ca/CommentView,guid,7fcc7091-e486-4f69-8088-6d04ba217da7.aspx</wfw:comment>
      <wfw:commentRss>http://hoser.lander.ca/SyndicationService.asmx/GetEntryCommentsRss?guid=7fcc7091-e486-4f69-8088-6d04ba217da7</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
          <a href="http://winfx.msdn.microsoft.com/library/en-us/dv_fxdebug/html/76994ee6-9fa9-4059-b813-26578d24427c.asp">Managed
Debugging Assistants</a> are an existing feature (<a href="http://blogs.msdn.com/adam_nathan/archive/2003/5/13.aspx">Customer
Debug Probes</a>) improved on and expanded. They are exposed in a similar manner to
exceptions (don't let that scare you) but occur not because of an error condition,
but because the runtime has noticed a condition that could cause you problems. We've
talked to lots of customers over the years and have received feedback that there are
a set of scenarios that cause developers pain more often than others. As a result,
we've written a bunch of code -- the MDAs themselves -- that can be used while your
code is running to protect you against these pain points ... that's good, right?
</p>
        <p>
The MDA infrastructure is always "on", but only a few MDAs are activated by default.
That's easily changed, by selecting the "Debug" menu and "Exceptions" within it. From
there, you can turn on or off any MDA of your choosing, as you can see in the image
below. There is only one MDA turned on in that part of list in the image below, but
you turn on as many as you want.
</p>
        <p>
          <img alt="VS 2005 exceptions window" hspace="0" src="/CodeDownload/ExceptionsWindow.bmp" align="baseline" border="0" />
        </p>
        <p>
Anyway, I want to introduce you to the LoadFromContext MDA. If you want to know more
about the joys of LoadFrom, check out Suzanne's <a href="http://blogs.msdn.com/suzcook/archive/2003/05/29/57143.aspx">blog</a>.
I'll post more on binding contexts at some point, but want to stay on the subject
of this MDA for the moment. Here is some code that exercises this MDA -- this is just
an exe project attempting to load a built assembly from a class library project
in the same solution (hence the strange pathing) ...
</p>
        <p>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">using</span> System;<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">using</span> System.Reflection;<br /><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">namespace</span> LoadFromMDA<br />
{<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">class</span> Program<br />
{<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">static</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span> Main(<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">string</span>[]
args)<br />
{<br />
Assembly asm <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> Assembly.LoadFrom(<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">@"..\..\..\SomeAssembly\bin\debug\SomeAssembly.dll"</span>);<br />
Console.WriteLine(asm.FullName);<br />
}<br />
}<br />
}<br /></span>
        </p>
        <p>
If the MDA is enabled, I get the following exception-looking popup window as soon
as hit the first line of code.
</p>
        <p>
          <img alt="LoadFromContext MDA window" hspace="0" src="/CodeDownload/LoadFromContext.bmp" align="baseline" border="0" />
        </p>
        <p>
The reason that this MDA is so useful is that it is really quite difficult to determine
for certain which binding context an assembly was loading into, other than following
a certain set of rules and closely watching the way that the binder binds to dependent
assemblies. However, with this MDA, you can know for sure if you are loading assemblies
into the LoadFrom context.
</p>
        <p>
There are several other MDAs to play with. I recommend taking a look through them
and turning on ones that might apply to your program. There is no harm and you may
avoid your program throwing an exception in some edge-case scenarios for reasons that
may not be super clear to you. 
</p>
        <p>
 
</p>
        <img width="0" height="0" src="http://hoser.lander.ca/aggbug.ashx?id=7fcc7091-e486-4f69-8088-6d04ba217da7" />
      </body>
      <title>LoadFromContext MDA in VS 2005</title>
      <guid isPermaLink="false">http://hoser.lander.ca/PermaLink,guid,7fcc7091-e486-4f69-8088-6d04ba217da7.aspx</guid>
      <link>http://hoser.lander.ca/2005/05/12/LoadFromContextMDAInVS2005.aspx</link>
      <pubDate>Thu, 12 May 2005 01:27:53 GMT</pubDate>
      <description>&lt;p&gt;
&lt;a href="http://winfx.msdn.microsoft.com/library/en-us/dv_fxdebug/html/76994ee6-9fa9-4059-b813-26578d24427c.asp"&gt;Managed
Debugging Assistants&lt;/a&gt; are an existing feature (&lt;a href="http://blogs.msdn.com/adam_nathan/archive/2003/5/13.aspx"&gt;Customer
Debug Probes&lt;/a&gt;) improved on and expanded. They are exposed in a similar manner to
exceptions (don't let that scare you) but occur not because of an error condition,
but because the runtime has noticed a condition that could cause you problems. We've
talked to lots of customers over the years and have received feedback that there are
a set of scenarios that cause developers pain more often than others. As a result,
we've written a bunch of code -- the MDAs themselves -- that can be used while your
code is running to protect you against these pain points ... that's good, right?
&lt;/p&gt;
&lt;p&gt;
The MDA infrastructure is always "on", but only a few MDAs are activated by default.
That's easily changed, by selecting the "Debug" menu and "Exceptions" within it. From
there, you can turn on or off any MDA of your choosing, as you can see in the image
below. There is only one MDA turned on in that part of list in the image below, but
you turn on as many as you want.
&lt;/p&gt;
&lt;p&gt;
&lt;img alt="VS 2005 exceptions window" hspace=0 src="/CodeDownload/ExceptionsWindow.bmp" align=baseline border=0&gt;
&lt;/p&gt;
&lt;p&gt;
Anyway, I want to introduce you to the LoadFromContext MDA. If you want to know more
about the joys of LoadFrom, check out Suzanne's &lt;a href="http://blogs.msdn.com/suzcook/archive/2003/05/29/57143.aspx"&gt;blog&lt;/a&gt;.
I'll post more on binding contexts at some point, but want to stay on the subject
of this MDA for the moment. Here is some code that exercises this MDA -- this is just
an exe project attempting to load&amp;nbsp;a built assembly from a class library project
in the same solution (hence the strange pathing)&amp;nbsp;...
&lt;/p&gt;
&lt;p&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; System;&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; System.Reflection;&lt;br&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;namespace&lt;/span&gt; LoadFromMDA&lt;br&gt;
{&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;class&lt;/span&gt; Program&lt;br&gt;
{&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;static&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;void&lt;/span&gt; Main(&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;string&lt;/span&gt;[]
args)&lt;br&gt;
{&lt;br&gt;
Assembly asm &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; Assembly.LoadFrom(&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;@"..\..\..\SomeAssembly\bin\debug\SomeAssembly.dll"&lt;/span&gt;);&lt;br&gt;
Console.WriteLine(asm.FullName);&lt;br&gt;
}&lt;br&gt;
}&lt;br&gt;
}&lt;br&gt;
&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
If the MDA is enabled, I get the following exception-looking popup window as soon
as hit the first line of code.
&lt;/p&gt;
&lt;p&gt;
&lt;img alt="LoadFromContext MDA window" hspace=0 src="/CodeDownload/LoadFromContext.bmp" align=baseline border=0&gt;
&lt;/p&gt;
&lt;p&gt;
The reason that this MDA is so useful is that it is really quite difficult to determine
for certain which binding context an assembly was loading into, other than following
a certain set of rules and closely watching the way that the binder binds to dependent
assemblies. However, with this MDA, you can know for sure if you are loading assemblies
into the LoadFrom context.
&lt;/p&gt;
&lt;p&gt;
There are several other MDAs to play with. I recommend taking a look through them
and turning on ones that might apply to your program. There is no harm and you may
avoid your program throwing an exception in some edge-case scenarios for reasons that
may not be super clear to you. 
&lt;/p&gt;
&lt;p&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://hoser.lander.ca/aggbug.ashx?id=7fcc7091-e486-4f69-8088-6d04ba217da7" /&gt;</description>
      <comments>http://hoser.lander.ca/CommentView,guid,7fcc7091-e486-4f69-8088-6d04ba217da7.aspx</comments>
      <category>Code Samples</category>
      <category>Loader</category>
    </item>
    <item>
      <trackback:ping>http://hoser.lander.ca/Trackback.aspx?guid=80725524-e184-4e06-a61a-3e0c23e7c76f</trackback:ping>
      <pingback:server>http://hoser.lander.ca/pingback.aspx</pingback:server>
      <pingback:target>http://hoser.lander.ca/PermaLink,guid,80725524-e184-4e06-a61a-3e0c23e7c76f.aspx</pingback:target>
      <dc:creator>Rich Lander</dc:creator>
      <wfw:comment>http://hoser.lander.ca/CommentView,guid,80725524-e184-4e06-a61a-3e0c23e7c76f.aspx</wfw:comment>
      <wfw:commentRss>http://hoser.lander.ca/SyndicationService.asmx/GetEntryCommentsRss?guid=80725524-e184-4e06-a61a-3e0c23e7c76f</wfw:commentRss>
      <slash:comments>3</slash:comments>
      <title>Getting Roped Into Reflection</title>
      <guid isPermaLink="false">http://hoser.lander.ca/PermaLink,guid,80725524-e184-4e06-a61a-3e0c23e7c76f.aspx</guid>
      <link>http://hoser.lander.ca/2005/01/15/GettingRopedIntoReflection.aspx</link>
      <pubDate>Sat, 15 Jan 2005 05:10:21 GMT</pubDate>
      <description>&lt;p&gt;
&lt;font color=#000000&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;I decided
to whip up a quick app today to test some behaviour of Assembly.LoadFrom() that I
wasn't 100% certain about. In particular, I was curious about the dependency resolution
behaviour of Assembly.LoadFrom(), as compared to Assembly.Load(). I know that Load()
will find static dependencies for you, provided that the dependencies are in a place
that the loader looks (i.e. GAC, appbase, private bin paths). I also know that Assembly.LoadFile()
doesn't do any dependency resolution for you; you have to pre-load dependencies when
you use LoadFile(). So, the test is to determine which camp -- dependency resolution
or not -- that LoadFrom falls into. I'm pretty sure that it will fall into the dependency
resolution camp, but I've never explicitly tested this case in issolation, so I'm
checking it out. I've also written a bunch of apps that depended on LoadFrom resolving
dependencies for you, so this likely isn't going to end up being a big surprise.&lt;/span&gt;&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;
&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;b&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial"&gt;An Exercise in Reflection&lt;/span&gt;&lt;/b&gt;
&lt;o:p&gt;&lt;/o:p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;&lt;font color=#000000&gt;So,
I have to admit up front that I'm not great at using Reflection -- another bloke&amp;nbsp;works
on that. Anyway, I had to brush up on my reflection skills to get this working and
ask&lt;/font&gt;&lt;span style="COLOR: #003300"&gt; &lt;a href="http://blogs.msdn.com/joelpob" target=_blank&gt;Joel&lt;/a&gt;&amp;nbsp;if
I'd done it properly once I was done, which turned out to be mostly the case.&lt;/span&gt;&lt;/span&gt;
&lt;o:p&gt;&lt;/o:p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="FONT-SIZE: 10pt; COLOR: #003300; FONT-FAMILY: 'Courier New'"&gt;Here's the
main program:&lt;/span&gt;
&lt;o:p&gt;&lt;/o:p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="FONT-SIZE: 8.5pt; COLOR: black; FONT-FAMILY: 'Courier New'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="FONT-SIZE: 8.5pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;static&lt;/span&gt;&lt;span style="FONT-SIZE: 8.5pt; COLOR: black; FONT-FAMILY: 'Courier New'"&gt; &lt;/span&gt;&lt;span style="FONT-SIZE: 8.5pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;void&lt;/span&gt;&lt;span style="FONT-SIZE: 8.5pt; COLOR: black; FONT-FAMILY: 'Courier New'"&gt;&lt;?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" /&gt; 
&lt;st1:place w:st="on"&gt;Main&lt;/st1:place&gt;
(&lt;/span&gt;&lt;span style="FONT-SIZE: 8.5pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;string&lt;/span&gt;&lt;span style="FONT-SIZE: 8.5pt; COLOR: black; FONT-FAMILY: 'Courier New'"&gt;[]
args)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Assembly a;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Type t;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;MethodInfo
m;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Object[] myArgs;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Object myInt;&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Console.WriteLine(&lt;/span&gt;&lt;span style="FONT-SIZE: 8.5pt; BACKGROUND: #e4e4e4; COLOR: #666666; FONT-FAMILY: 'Courier New'"&gt;"In
main program"&lt;/span&gt;&lt;span style="FONT-SIZE: 8.5pt; COLOR: black; FONT-FAMILY: 'Courier New'"&gt;);&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="FONT-SIZE: 8.5pt; COLOR: green; FONT-FAMILY: 'Courier New'"&gt;//Loading
assembly&lt;/span&gt;&lt;span style="FONT-SIZE: 8.5pt; COLOR: black; FONT-FAMILY: 'Courier New'"&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="FONT-SIZE: 8.5pt; COLOR: green; FONT-FAMILY: 'Courier New'"&gt;//a
= Assembly.LoadFile(@"C:\Documents and Settings\rlander\My Documents\Visual Studio
2005\Projects\LoadFromApp\FooLibrary\bin\debug\FooLibrary.dll");&lt;/span&gt;&lt;span style="FONT-SIZE: 8.5pt; COLOR: black; FONT-FAMILY: 'Courier New'"&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;a &lt;/span&gt;&lt;span style="FONT-SIZE: 8.5pt; COLOR: red; FONT-FAMILY: 'Courier New'"&gt;=&lt;/span&gt;&lt;span style="FONT-SIZE: 8.5pt; COLOR: black; FONT-FAMILY: 'Courier New'"&gt; Assembly.LoadFrom(&lt;/span&gt;&lt;span style="FONT-SIZE: 8.5pt; BACKGROUND: #e4e4e4; COLOR: #666666; FONT-FAMILY: 'Courier New'"&gt;@"../../../FooLibrary/bin/debug/FooLibrary.dll"&lt;/span&gt;&lt;span style="FONT-SIZE: 8.5pt; COLOR: black; FONT-FAMILY: 'Courier New'"&gt;);&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="FONT-SIZE: 8.5pt; COLOR: green; FONT-FAMILY: 'Courier New'"&gt;//Getting
type to call into&lt;/span&gt;&lt;span style="FONT-SIZE: 8.5pt; COLOR: black; FONT-FAMILY: 'Courier New'"&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;t &lt;/span&gt;&lt;span style="FONT-SIZE: 8.5pt; COLOR: red; FONT-FAMILY: 'Courier New'"&gt;=&lt;/span&gt;&lt;span style="FONT-SIZE: 8.5pt; COLOR: black; FONT-FAMILY: 'Courier New'"&gt; a.GetType(&lt;/span&gt;&lt;span style="FONT-SIZE: 8.5pt; BACKGROUND: #e4e4e4; COLOR: #666666; FONT-FAMILY: 'Courier New'"&gt;"FooLibrary.Foo"&lt;/span&gt;&lt;span style="FONT-SIZE: 8.5pt; COLOR: black; FONT-FAMILY: 'Courier New'"&gt;);&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="FONT-SIZE: 8.5pt; COLOR: green; FONT-FAMILY: 'Courier New'"&gt;//Getting
the specific method that I'm interested in&lt;/span&gt;&lt;span style="FONT-SIZE: 8.5pt; COLOR: black; FONT-FAMILY: 'Courier New'"&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;m &lt;/span&gt;&lt;span style="FONT-SIZE: 8.5pt; COLOR: red; FONT-FAMILY: 'Courier New'"&gt;=&lt;/span&gt;&lt;span style="FONT-SIZE: 8.5pt; COLOR: black; FONT-FAMILY: 'Courier New'"&gt; t.GetMethod(&lt;/span&gt;&lt;span style="FONT-SIZE: 8.5pt; BACKGROUND: #e4e4e4; COLOR: #666666; FONT-FAMILY: 'Courier New'"&gt;"GetInt"&lt;/span&gt;&lt;span style="FONT-SIZE: 8.5pt; COLOR: black; FONT-FAMILY: 'Courier New'"&gt;,
BindingFlags.Static &lt;/span&gt;&lt;span style="FONT-SIZE: 8.5pt; COLOR: red; FONT-FAMILY: 'Courier New'"&gt;|&lt;/span&gt;&lt;span style="FONT-SIZE: 8.5pt; COLOR: black; FONT-FAMILY: 'Courier New'"&gt; BindingFlags.Public, &lt;/span&gt;&lt;span style="FONT-SIZE: 8.5pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;null&lt;/span&gt;&lt;span style="FONT-SIZE: 8.5pt; COLOR: black; FONT-FAMILY: 'Courier New'"&gt;, &lt;/span&gt;&lt;span style="FONT-SIZE: 8.5pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;new&lt;/span&gt;&lt;span style="FONT-SIZE: 8.5pt; COLOR: black; FONT-FAMILY: 'Courier New'"&gt; Type[]
{ &lt;/span&gt;&lt;span style="FONT-SIZE: 8.5pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;typeof&lt;/span&gt;&lt;span style="FONT-SIZE: 8.5pt; COLOR: black; FONT-FAMILY: 'Courier New'"&gt;(System.Int32)},&lt;/span&gt;&lt;span style="FONT-SIZE: 8.5pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;null&lt;/span&gt;&lt;span style="FONT-SIZE: 8.5pt; COLOR: black; FONT-FAMILY: 'Courier New'"&gt;);&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="FONT-SIZE: 8.5pt; COLOR: green; FONT-FAMILY: 'Courier New'"&gt;//args
to pass to the method&lt;/span&gt;&lt;span style="FONT-SIZE: 8.5pt; COLOR: black; FONT-FAMILY: 'Courier New'"&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;myArgs &lt;/span&gt;&lt;span style="FONT-SIZE: 8.5pt; COLOR: red; FONT-FAMILY: 'Courier New'"&gt;=&lt;/span&gt;&lt;span style="FONT-SIZE: 8.5pt; COLOR: black; FONT-FAMILY: 'Courier New'"&gt; &lt;/span&gt;&lt;span style="FONT-SIZE: 8.5pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;new&lt;/span&gt;&lt;span style="FONT-SIZE: 8.5pt; COLOR: black; FONT-FAMILY: 'Courier New'"&gt; Object[1]
{5};&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="FONT-SIZE: 8.5pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;try&lt;/span&gt;&lt;span style="FONT-SIZE: 8.5pt; COLOR: black; FONT-FAMILY: 'Courier New'"&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="FONT-SIZE: 8.5pt; COLOR: green; FONT-FAMILY: 'Courier New'"&gt;//Calling
the method and printing the return value&lt;/span&gt;&lt;span style="FONT-SIZE: 8.5pt; COLOR: black; FONT-FAMILY: 'Courier New'"&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;myInt &lt;/span&gt;&lt;span style="FONT-SIZE: 8.5pt; COLOR: red; FONT-FAMILY: 'Courier New'"&gt;=&lt;/span&gt;&lt;span style="FONT-SIZE: 8.5pt; COLOR: black; FONT-FAMILY: 'Courier New'"&gt; m.Invoke(&lt;/span&gt;&lt;span style="FONT-SIZE: 8.5pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;null&lt;/span&gt;&lt;span style="FONT-SIZE: 8.5pt; COLOR: black; FONT-FAMILY: 'Courier New'"&gt;,
myArgs);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Console.WriteLine(myInt.ToString());&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="FONT-SIZE: 8.5pt; COLOR: green; FONT-FAMILY: 'Courier New'"&gt;//in
case something bad happens&lt;/span&gt;&lt;span style="FONT-SIZE: 8.5pt; COLOR: black; FONT-FAMILY: 'Courier New'"&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="FONT-SIZE: 8.5pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;catch&lt;/span&gt;&lt;span style="FONT-SIZE: 8.5pt; COLOR: black; FONT-FAMILY: 'Courier New'"&gt; (TargetInvocationException
e)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Console.WriteLine(e.InnerException.ToString());&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/span&gt;
&lt;o:p&gt;&lt;/o:p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font color=#000000&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;Indeed,
there is more to this program than just the Main method, however, the additional aspects
are not particularly interesting. The code above is the whole of a Console exe project,
called LoadFromApp, in VS 2005. There are two other projects that I wrote too: (1)
a Class Library project called FooLibrary that the exe loads via Assembly.LoadFrom(),
and (2) another Class Library project called BarLibrary to which FooLibrary statically
links. You can see these too/two in the code sample, which is linked at the bottom
of the entry. &lt;/span&gt;
&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font color=#000000&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;The
expected behaviour, now that you have a mental model of my project, is that BarLibrary
is automatically loaded by the CLR loader when I load FooLibrary from LoadFromApp
via Assembly.LoadFrom(). BarLibrary is necessarily in the same directory as FooLibrary;
otherwise, there would be no way that LoadFrom() could find BarLibrary, unless BarLibrary
was in the GAC or something like that. This is why VS adds referenced assemblies into
your bin/debug (or release) folder when you build an assembly.&lt;/span&gt;
&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font color=#000000&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;The
interesting thing is that this exercise turned out to be more of a lesson in reflection
than in the loader, but that's how these things end up. I'll tell you what I learned
about LoadFrom and then reiterate what Joel told me about reflection.&lt;/span&gt;
&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;b&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial"&gt;What I Learned&lt;/span&gt;&lt;/b&gt;
&lt;o:p&gt;&lt;/o:p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font color=#000000&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;First,
the loader part. So, it does indeed turn out to be true that Assembly.LoadFrom() does
resolve dependencies, provided that there are in a findable place, which turns out
to be any place that would be consulted for Assembly.Load() and the path (minus filename.dll)
of the Assembly.LoadFrom(String path). That wasn't too hard, but was worth the exercise.&lt;/span&gt;
&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font color=#000000&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;You'll
also notice that I commented out Assembly.LoadFile(), just above Assembly.LoadFrom().
I tried this too for kicks, which resulted in an exception of type System.Reflection.TargetInvocationException.
That's the &amp;#8220;something bad happens&amp;#8221; and what the exception handler is for
above. So, this proves that LoadFile() does not resolve depencies, which I already
knew, but was a good test anyway.&lt;/span&gt;
&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font color=#000000&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;Now,
onto the reflection aspect. The comments in the source above mostly explain what is
going on, I hope. If not, I load an assembly, get a known type from that assembly,
then get a known method from that type and then call through on that method with known
data. Do remember/realize that this isn&amp;#8217;t normally what .net code looks like.
Reflection is one of the mechanisms of handling late bound scenarios for .net coders.
It is probably the most flexible, but not the most approachable.&lt;/span&gt;
&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font color=#000000&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;Back
to the interesting review of reflection &amp;#8230; The line that I'm most interested
in is the call to t.GetMethod, which is the instance method MethodInfo.GetMethod.
I'll re-print it here:&lt;/span&gt;
&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 'Courier New'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 'Courier New'"&gt;//Getting
the specific method that I'm interested in&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: #003300; FONT-FAMILY: 'Courier New'"&gt;
&lt;br&gt;
&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 'Courier New'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;m &lt;/span&gt;&lt;span style="FONT-SIZE: 8.5pt; COLOR: red; FONT-FAMILY: 'Courier New'"&gt;=&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 'Courier New'"&gt; t.GetMethod(&lt;/span&gt;&lt;span style="FONT-SIZE: 8.5pt; BACKGROUND: #e4e4e4; COLOR: #666666; FONT-FAMILY: 'Courier New'"&gt;"GetInt"&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 'Courier New'"&gt;,
BindingFlags.Static &lt;/span&gt;&lt;span style="FONT-SIZE: 8.5pt; COLOR: red; FONT-FAMILY: 'Courier New'"&gt;|&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 'Courier New'"&gt; BindingFlags.Public, &lt;/span&gt;&lt;span style="FONT-SIZE: 8.5pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;null&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 'Courier New'"&gt;, &lt;/span&gt;&lt;span style="FONT-SIZE: 8.5pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;new&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 'Courier New'"&gt; Type[]
{ &lt;/span&gt;&lt;span style="FONT-SIZE: 8.5pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;typeof&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 'Courier New'"&gt;(System.Int32)},&lt;/span&gt;&lt;span style="FONT-SIZE: 8.5pt; COLOR: blue; FONT-FAMILY: 'Courier New'"&gt;null&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 'Courier New'"&gt;);&lt;/span&gt;
&lt;o:p&gt;&lt;/o:p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font color=#000000&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;The
GetMethod method allows you to specify enough information for the reflection system
to get a single method for you from a given type. The thing is that you'll get some
type of exception if you haven't specified enough information to single out your desired
method from the set of methods on the type. Put another way, you&amp;#8217;ll get this
exception if the reflection system ends up finding &amp;gt;1 methods that fit your description
since the method only returns MethodInfo, not MethodInfo[]. GetMethods() returns MethodInfo[]
and obviously doesn&amp;#8217;t have this particular problem. So, you need to be pretty
specific about the method that you want. It may not be neccesary this version, but
you may add overloads next version and forget that your GetMethod call was pretty
sloppy and then stuff breaks, which is bad.&lt;/span&gt;
&lt;o:p&gt;&lt;/o:p&gt;
&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font color=#000000&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;It is
the first two and&amp;nbsp;fourth parameters that are the most interesting to me. The
first one is the method name. So, this differentiates the method from all other methods.
You can make the search case insensitive via the BindingFlags, but I would not recommend
this approach since the CLR doesn&amp;#8217;t work this way and it will slow down the
whole operation. The next parameter is the BindingFlags. I've stated that the method
I'm looking for is static and public. So, the search will not retreive any instance
or internal/protected/private methods that happen to have the same name, which is
quite possible. You&amp;#8217;ll need to pick among these flags carefully to ensure that
you have the correct level of specificity. The fourth parameter specifies the signature
of the method. Rememer, the CLR doesn't consider the return type when determining
which overload of a method to call, so the signature is just the types of the parameters
that the method takes. At this point, you will have provided enough data that the
reflection system can narrow its search of methods on the given time to 1, or 0 if
it is not there.&lt;/span&gt;&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font color=#000000&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"&gt;The
actual code sample that I wrote is on another machine. I'll post it in a day or two
when I get a chance. The sample is nothing big, but you'll be able to see how LoadFrom()
and LoadFile() work for yourself with my small contrived example.&lt;/span&gt;&lt;/font&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://hoser.lander.ca/aggbug.ashx?id=80725524-e184-4e06-a61a-3e0c23e7c76f" /&gt;</description>
      <comments>http://hoser.lander.ca/CommentView,guid,80725524-e184-4e06-a61a-3e0c23e7c76f.aspx</comments>
      <category>Code Samples</category>
      <category>Loader</category>
    </item>
    <item>
      <trackback:ping>http://hoser.lander.ca/Trackback.aspx?guid=4d6820a1-c059-44bc-8b2e-84b2c6a61a39</trackback:ping>
      <pingback:server>http://hoser.lander.ca/pingback.aspx</pingback:server>
      <pingback:target>http://hoser.lander.ca/PermaLink,guid,4d6820a1-c059-44bc-8b2e-84b2c6a61a39.aspx</pingback:target>
      <dc:creator>Rich Lander</dc:creator>
      <wfw:comment>http://hoser.lander.ca/CommentView,guid,4d6820a1-c059-44bc-8b2e-84b2c6a61a39.aspx</wfw:comment>
      <wfw:commentRss>http://hoser.lander.ca/SyndicationService.asmx/GetEntryCommentsRss?guid=4d6820a1-c059-44bc-8b2e-84b2c6a61a39</wfw:commentRss>
      <slash:comments>11</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I was reading the <a href="http://groups-beta.google.com/group/microsoft.public.dotnet.framework.clr">CLR
newsgroup</a> and noticed the following <a href="http://groups-beta.google.com/group/microsoft.public.dotnet.framework.clr/browse_thread/thread/4375104e7e5ba3b5/790bc102ee499293">post</a> about
unhandled exceptions. I wasn't quite sure exactly how this worked, so I wrote a quick
app that caught exceptions from other domains in the defaul domain. This behaviour
was what the poster was looking for. 
</p>
        <p>
You can download the code [<a href="/CodeDownload/AppDomains/ExceptionListener2003.zip">Everett </a>| <a href="/CodeDownload/AppDomains/ExceptionListener2005.zip">Whidbey </a>]
to see how it works.
</p>
        <p>
The basic idea is that you register for the AppDomain.UnhandledException event. This event
will be fired for any exception that is not handled in any domain in the process.
You cannot swallow the exception at this point, so your app will die no matter what.
You can, however, do any cleanup that is required or put up some kind of user notification.
In the case below, you can see that I tell the user that an unhandled exception occured.
I'm sure that this is going to be quite useful to them ;)
</p>
        <p>
Here is the bulk of the code:
</p>
        <p>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">    <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">class</span> Program<br />
    {<br />
        <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">static</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span> Main(<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">string</span>[]
args)<br />
        {<br /><br />
            AppDomain.CurrentDomain.UnhandledException
+= <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span> UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);<br /><br />
            AppDomain
domain2 <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> AppDomain.CreateDomain(<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"domain2"</span>);<br />
            domain2.CreateInstance(<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"DomainLib"</span>, <span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"DomainLib.ThisClassThrows"</span>);<br /><br />
        }<br /><br />
        <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">static</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span> CurrentDomain_UnhandledException(<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">object</span> sender,
UnhandledExceptionEventArgs e)<br />
        {<br />
            Exception
ex <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> (Exception)e.ExceptionObject;<br />
            Console.WriteLine(<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"Unhandled
exception!!"</span>);<br />
            Console.WriteLine(ex.InnerException.Message);<br />
        }<br />
    }</span>
        </p>
        <p>
Another option, which is a good idea, is to use try/catch blocks in your code around
calls to other domains. That way you can catch the exceptions that come
across the domain boundary as opposed to just settle for process termination.
</p>
        <img width="0" height="0" src="http://hoser.lander.ca/aggbug.ashx?id=4d6820a1-c059-44bc-8b2e-84b2c6a61a39" />
      </body>
      <title>AppDomain Unhandled Exception</title>
      <guid isPermaLink="false">http://hoser.lander.ca/PermaLink,guid,4d6820a1-c059-44bc-8b2e-84b2c6a61a39.aspx</guid>
      <link>http://hoser.lander.ca/2004/12/15/AppDomainUnhandledException.aspx</link>
      <pubDate>Wed, 15 Dec 2004 18:45:03 GMT</pubDate>
      <description>&lt;p&gt;
I was reading the &lt;a href="http://groups-beta.google.com/group/microsoft.public.dotnet.framework.clr"&gt;CLR
newsgroup&lt;/a&gt; and noticed the following &lt;a href="http://groups-beta.google.com/group/microsoft.public.dotnet.framework.clr/browse_thread/thread/4375104e7e5ba3b5/790bc102ee499293"&gt;post&lt;/a&gt; about
unhandled exceptions. I wasn't quite sure exactly how this worked, so I wrote a quick
app that caught exceptions from other domains in the defaul domain. This behaviour
was what the poster was looking for. 
&lt;p&gt;
You can download the code [&lt;a href="/CodeDownload/AppDomains/ExceptionListener2003.zip"&gt;Everett &lt;/a&gt;| &lt;a href="/CodeDownload/AppDomains/ExceptionListener2005.zip"&gt;Whidbey &lt;/a&gt;]
to see how it works.
&lt;/p&gt;
&lt;p&gt;
The basic idea is that you&amp;nbsp;register for the AppDomain.UnhandledException event.&amp;nbsp;This&amp;nbsp;event
will be fired for&amp;nbsp;any exception that is not handled in any domain in the process.
You cannot swallow the exception at this point, so your app will die no matter what.
You can, however, do any cleanup that is required or put up some kind of user notification.
In the case below, you can see that I tell the user that an unhandled exception occured.
I'm sure that this is going to be quite useful to them ;)
&lt;/p&gt;
&lt;p&gt;
Here is the bulk of the code:
&lt;/p&gt;
&lt;p&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;class&lt;/span&gt; Program&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;static&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;void&lt;/span&gt; Main(&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;string&lt;/span&gt;[]
args)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;AppDomain.CurrentDomain.UnhandledException
+= &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;AppDomain
domain2 &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; AppDomain.CreateDomain(&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"domain2"&lt;/span&gt;);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;domain2.CreateInstance(&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"DomainLib"&lt;/span&gt;, &lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"DomainLib.ThisClassThrows"&lt;/span&gt;);&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;static&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;void&lt;/span&gt; CurrentDomain_UnhandledException(&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;object&lt;/span&gt; sender,
UnhandledExceptionEventArgs e)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Exception
ex &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; (Exception)e.ExceptionObject;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Console.WriteLine(&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"Unhandled
exception!!"&lt;/span&gt;);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Console.WriteLine(ex.InnerException.Message);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
Another option, which is a good idea, is to&amp;nbsp;use try/catch blocks in your code&amp;nbsp;around
calls to&amp;nbsp;other&amp;nbsp;domains. That way you can catch the exceptions that come
across the domain boundary as opposed to just settle for process termination.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://hoser.lander.ca/aggbug.ashx?id=4d6820a1-c059-44bc-8b2e-84b2c6a61a39" /&gt;</description>
      <comments>http://hoser.lander.ca/CommentView,guid,4d6820a1-c059-44bc-8b2e-84b2c6a61a39.aspx</comments>
      <category>AppDomains</category>
      <category>Code Samples</category>
    </item>
  </channel>
</rss>