<rss version="0.92">
  <channel>
    <title>Sanity Free Coding</title>
    <link>http://www.sanity-free.com/</link>
    <description>Methods to the Madness</description>
    <language>en</language>
        <item>
      <title>A CRC8 implementation in C#</title>
      <description>So I've tackled &lt;a href='/12/crc32_implementation_in_csharp.html' target='_blank'&gt;CRC32&lt;/a&gt;, &lt;a href='/134/standard_crc_16_in_csharp.html' target='_blank'&gt;CRC16&lt;/a&gt;, &lt;a href='/133/crc_16_ccitt_in_csharp.html' target='_blank'&gt;CRC16-CCITT&lt;/a&gt; implementations, and found myself wanting a CRC8 class to create checksums for small data (1-2 bytes) sets.  Not having ever used or seen CRC8 in use before, I did some reading, and as far as I can tell, I've implemented it correctly...  It works anyway, so without further ado, here's my implementation:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class='code'&gt;&lt;span class='keyword'&gt;public static class&lt;/span&gt; &lt;span class='class_def'&gt;Crc8&lt;/span&gt; {
    &lt;span class='keyword'&gt;static byte&lt;/span&gt;[] table = &lt;span class='keyword'&gt;new byte&lt;/span&gt;[256];
    &lt;span class='comment'&gt;// x&lt;sup&gt;8&lt;/sup&gt; + x&lt;sup&gt;7&lt;/sup&gt; + x&lt;sup&gt;6&lt;/sup&gt; + x&lt;sup&gt;4&lt;/sup&gt; + x&lt;sup&gt;2&lt;/sup&gt; + 1&lt;/span&gt;
    &lt;span class='keyword'&gt;const byte&lt;/span&gt; poly = 0xd5;

    &lt;span class='keyword'&gt;public static byte&lt;/span&gt; ComputeChecksum(&lt;span class='keyword'&gt;params byte&lt;/span&gt;[] bytes ) {
        &lt;span class='keyword'&gt;byte&lt;/span&gt; crc = 0;
        &lt;span class='keyword'&gt;if&lt;/span&gt; ( bytes != &lt;span class='keyword'&gt;null&lt;/span&gt; &amp;&amp; bytes.Length &amp;gt; 0 ) {
            &lt;span class='keyword'&gt;foreach&lt;/span&gt; ( &lt;span class='keyword'&gt;byte&lt;/span&gt; b in bytes ) {
                crc = table[crc ^ b];
            }
        }
        &lt;span class='keyword'&gt;return&lt;/span&gt; crc;
    } 

    &lt;span class='keyword'&gt;static&lt;/span&gt; Crc8( ) {
        &lt;span class='keyword'&gt;for&lt;/span&gt; ( &lt;span class='keyword'&gt;int&lt;/span&gt; i = 0; i &amp;lt; 256; ++i ) {
            &lt;span class='keyword'&gt;int&lt;/span&gt; temp = i;
            &lt;span class='keyword'&gt;for&lt;/span&gt; ( &lt;span class='keyword'&gt;int&lt;/span&gt; j = 0; j &amp;lt; 8; ++j ) {
                &lt;span class='keyword'&gt;if&lt;/span&gt; ( ( temp &amp;amp; 0x80 ) != 0 ) {
                    temp = ( temp &amp;lt;&amp;lt; 1 ) ^ poly;
                } &lt;span class='keyword'&gt;else&lt;/span&gt; {
                    temp &amp;lt;&amp;lt;= 1;
                }
            }
            table[i] = (&lt;span class='keyword'&gt;byte&lt;/span&gt;)temp;
        }
    }
}&lt;/pre&gt;
&lt;br /&gt;
This one differs slightly (API wise) from my previous ones, mainly because I was using it for checksum'ing smaller bits of data, so I made the ComputeChecksum method a params argument instead of a first class array type.&lt;br /&gt;
&lt;br /&gt;
Here's a sample of how I was using it (and testing it):&lt;br /&gt;
&lt;pre class='code'&gt;&lt;span class='keyword'&gt;byte&lt;/span&gt; crc = &lt;span class='class_def'&gt;Crc8&lt;/span&gt;.ComputeChecksum( 1, 2, 3 );
&lt;span class='keyword'&gt;byte&lt;/span&gt; check = &lt;span class='class_def'&gt;Crc8&lt;/span&gt;.ComputeChecksum( 1, 2, 3, crc );
&lt;span class='comment'&gt;// here check should equal 0 to show that the checksum is accurate&lt;/span&gt;
&lt;span class='keyword'&gt;if&lt;/span&gt; ( check != 0 ) {
    Console.WriteLine( &lt;span class='string'&gt;&amp;quot;Error in the checksum&amp;quot;&lt;/span&gt; );
}&lt;/pre&gt;&lt;br /&gt;
If you have any questions / comments / corrections, post them in the &lt;a href='/forum/viewforum.php?id=5' target='_blank'&gt;forums&lt;/a&gt;.</description>
      <pubDate>Thu, 04 Feb 2010 22:20:18 EST</pubDate>
      <link>http://sanity-free.org/146/crc8_implementation_in_csharp.html</link>
    </item>
        <item>
      <title>Pre-increment vs post-increment operators</title>
      <description>Ok, so here's a question for you that most people should have learned in school, but didn't.  You have to answer this without running to visual studio.&lt;br/&gt;
&lt;br/&gt;
&lt;ol&gt;
&lt;li&gt;What is the difference between ++i and i++?&lt;/li&gt;
&lt;li&gt;What is the output of the following piece of code:&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&quot;code&quot;&gt;&lt;span class=&quot;keyword&quot;&gt;for&lt;/span&gt;(&lt;span class=&quot;keyword&quot;&gt;int&lt;/span&gt; i = 0; i &amp;lt; 5; ++i) {
    &lt;span class=&quot;class_def&quot;&gt;Console&lt;/span&gt;.WriteLine(i);
}&lt;/pre&gt;
Is it: 
A) &lt;pre class=&quot;code&quot;&gt;1
2
3
4&lt;/pre&gt;

or: 
B)&lt;pre class=&quot;code&quot;&gt;0
1
2
3
4&lt;/pre&gt;

I'm sure there are quite a few who would choose A, but this is in fact incorrect (go ahead and run it in visual studio now).&lt;br/&gt;
&lt;br/&gt;
Most of us when we learned C style programming were taught that pre-increment increments the value before taking the value, and that post increment takes the value then increments.  After being taught that, pretty much every professor, every code sample, and everybody under the sun used post increment for every single iterative example in existence. &lt;br /&gt;
&lt;br /&gt;
Question is... WHY?&lt;br/&gt;
&lt;br/&gt;
&lt;!-- readmore --&gt;
Inside a loop, I cannot see why anyone would use post increment.  What is the order of operation?  Declare, loop body, compare, increment, loop body, compare, increment, ....  &lt;br/&gt;
&lt;br/&gt;
Unlike C++ (where there are there are actually 2 distinct operators: a pre and a post increment operator) C#, has only one generic &quot;increment&quot; / &quot;decrement&quot; operator, so exactly how the operator works is left completely out of our hands (and is handled internally by the framework).  As stated in the ECMA spec (page 176 of the 4&lt;sup&gt;th&lt;/sup&gt; edition of the ECMA-334):&lt;br/&gt;
&lt;blockquote style=&quot;font-style: italic; font-weight: bold;&quot;&gt;The run-time processing of a postfix increment or decrement operation of the form x++ or x-- consists of
the following steps:
&lt;ul&gt;
  &lt;li&gt;If x is classified as a variable:
    &lt;ul&gt;
      &lt;li&gt;x is evaluated to produce the variable.&lt;/li&gt;
      &lt;li&gt;The value of x is saved.&lt;/li&gt;
      &lt;li&gt;The saved value of x is converted to the operand type of the selected operator and the operator is invoked with this value as its argument.&lt;/li&gt;
      &lt;li&gt;The value returned by the operator is converted to the type of x and stored in the location given by the evaluation of x.&lt;/li&gt;
      &lt;li&gt;The saved value of x becomes the result of the operation.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;&lt;/blockquote&gt;
By comparison, here is what is stated for a pre-increment operation (page 196 of ECMA-334 4&lt;sup&gt;th&lt;/sup&gt; edition):&lt;br/&gt;
&lt;blockquote style=&quot;font-style: italic; font-weight: bold;&quot;&gt;The run-time processing of a prefix increment or decrement operation of the form ++x or --x consists of the
following steps:
&lt;ul&gt;
  &lt;li&gt;If x is classified as a variable:
    &lt;ul&gt;
      &lt;li&gt;x is evaluated to produce the variable.&lt;/li&gt;
      &lt;li&gt;The value of x is converted to the operand type of the selected operator and the operator is invoked with this value as its argument.&lt;/li&gt;
      &lt;li&gt;The value returned by the operator is converted to the type of x. The resulting value is stored in the location given by the evaluation of x and becomes the result of the operation.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;&lt;/blockquote&gt;

So in the case of post increment, a copy has to be made, the op_Increment is invoked using the copy, the value is returned, and the copy overwrites the original value.&lt;br /&gt;
&lt;br /&gt;
With pre increment, the value is passed into the op_Increment method, and its value is returned.&lt;br/&gt;
&lt;br/&gt;
In the case of a loop, we are not using the value returned pre-increment, and are therefore only using the increment operation to alter the original value, so using post increment is completely pointless.  Why anyone uses it is beyond me.  Maybe its a case of not actually understanding whats going on.&lt;br /&gt;
&lt;br /&gt;
There are a few places where it actually makes sense to use a post increment operation, but even these probably have better solutions to them as well:&lt;br /&gt;
&lt;pre class=&quot;code&quot;&gt;&lt;span class=&quot;keyword&quot;&gt;int&lt;/span&gt; i = 0;
&lt;span class=&quot;keyword&quot;&gt;int&lt;/span&gt; j = i++;
&lt;span class=&quot;comment&quot;&gt;// or&lt;/span&gt;
&lt;span class=&quot;keyword&quot;&gt;int&lt;/span&gt;[] table = { 0, 1, 2, 3, 4, 5, 6 };
&lt;span class=&quot;keyword&quot;&gt;int&lt;/span&gt; i = 0;
&lt;span class=&quot;keyword&quot;&gt;int&lt;/span&gt; j = table[i++];
&lt;span class=&quot;keyword&quot;&gt;int&lt;/span&gt; k = table[i++];
&lt;span class=&quot;keyword&quot;&gt;int&lt;/span&gt; l = table[i++];
&lt;/pre&gt;
So the moral of the story is... post increment wisely (have a good reason to use it, otherwise pre-increment).</description>
      <pubDate>Thu, 18 Dec 2008 04:47:08 EST</pubDate>
      <link>http://sanity-free.org/145/preincrement_vs_postincrement_operators.html</link>
    </item>
        <item>
      <title>A C# Command Line Args processing class</title>
      <description>The other day I was working on our video game's content server app.  The way its set up is fairly complicated.  It basically has 3 modes that it can run in (as a windows service, as a fully blown console shell, or as a windows application).&lt;br/&gt;
&lt;br/&gt;
Being tired of writing long ugly blocks of code for command line arg's processing, I decided to build out an object (not at all like my last command line parser class) that would manage the command line args in an intuitive manner.&lt;br/&gt;
&lt;br/&gt;
There are Three basic ways to use this class:
&lt;ol&gt;
&lt;li&gt;Sort of like a dictionary&lt;/li&gt;
&lt;li&gt;With one big event handler&lt;/li&gt;
&lt;li&gt;By registering specific event handlers&lt;/li&gt;
&lt;/ol&gt;
What I typically do is add the args that are passed into the main function into an arraylist or List&amp;lt;string&amp;gt; and simply ask if the list contains this switch or that switch:
&lt;br/&gt;
&lt;pre class=&quot;code&quot;&gt;&lt;span class=&quot;keyword&quot;&gt;static class&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;Program&lt;/span&gt; {
    &lt;span class=&quot;keyword&quot;&gt;static void&lt;/span&gt; Main(&lt;span class=&quot;keyword&quot;&gt;string&lt;/span&gt;[] args) {
        &lt;span class=&quot;class_def&quot;&gt;List&lt;/span&gt;&amp;lt;&lt;span class=&quot;keyword&quot;&gt;string&lt;/span&gt;&amp;gt; cmdArgs = &lt;span class=&quot;keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;List&lt;/span&gt;&amp;lt;&lt;span class=&quot;keyword&quot;&gt;string&lt;/span&gt;&amp;gt;(args);
        &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt;(cmdArgs.Contains(&lt;span class=&quot;string&quot;&gt;&amp;quot;/myswitch&amp;quot;&lt;/span&gt;))) {
            &lt;span class=&quot;comment&quot;&gt;// ... whatever ...&lt;/span&gt;
        }
    }
}&lt;/pre&gt;
&lt;br /&gt;
&lt;!-- readmore --&gt;
As you can see, it still takes quite a bit of code.  My first iteration of my &lt;span class=&quot;class_def&quot;&gt;CommandLineArgs&lt;/span&gt; class parsed out the command line switches and placed everything in a dictionary&lt;br/&gt;

&lt;pre class=&quot;code&quot;&gt;&lt;span class=&quot;keyword&quot;&gt;static class&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;Program&lt;/span&gt; {
    &lt;span class=&quot;keyword&quot;&gt;static void&lt;/span&gt; Main(&lt;span class=&quot;keyword&quot;&gt;string&lt;/span&gt;[] args) {
        &lt;span class=&quot;class_def&quot;&gt;CommandLineArgs&lt;/span&gt; cmdArgs = &lt;span class=&quot;keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;CommandLineArgs&lt;/span&gt;();
        cmdArgs.IgnoreCase = &lt;span class=&quot;keyword&quot;&gt;true&lt;/span&gt;;
        &lt;span class=&quot;comment&quot;&gt;// this adds a switch prefix regular expression used to determine what is a switch and what is not.&lt;/span&gt;
        &lt;span class=&quot;comment&quot;&gt;// whats passed in is a normal Regex pattern.&lt;/span&gt;
        cmdArgs.PrefixRegexPatternList.Add(&lt;span class=&quot;string&quot;&gt;&amp;quot;/{1}&amp;quot;&lt;/span&gt;); &lt;span class=&quot;comment&quot;&gt;// /switch&lt;/span&gt;
        cmdArgs.PrefixRegexPatternList.Add(&lt;span class=&quot;string&quot;&gt;&amp;quot;-{1,2}&amp;quot;&lt;/span&gt;); &lt;span class=&quot;comment&quot;&gt;// --switch or -switch&lt;/span&gt;
        &lt;span class=&quot;comment&quot;&gt;// once the args are parsed, the class strips off the switch prefix (here either / or - or --).&lt;/span&gt;
        cmdArgs.ProcessCommandLineArgs(args);
        &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt;(cmdArgs.ContainsSwitch(&lt;span class=&quot;string&quot;&gt;&amp;quot;myswitch&amp;quot;&lt;/span&gt;))) {
            &lt;span class=&quot;comment&quot;&gt;// ... whatever ...&lt;/span&gt;
        }
    }
}&lt;/pre&gt;

But that was pretty much just like using the list like I had been doing.&lt;br/&gt;
&lt;br/&gt;
The next iteration added a SwitchMatch event so that when ProcessCommandLineArgs was called, as switches were parsed, it would kick of an event for each match, providing the switch name (minus the switch prefix) and the value of that switch.

&lt;pre class=&quot;code&quot;&gt;&lt;span class=&quot;keyword&quot;&gt;static class&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;Program&lt;/span&gt; {
    &lt;span class=&quot;keyword&quot;&gt;static void&lt;/span&gt; Main(&lt;span class=&quot;keyword&quot;&gt;string&lt;/span&gt;[] args) {
        &lt;span class=&quot;class_def&quot;&gt;CommandLineArgs&lt;/span&gt; cmdArgs = &lt;span class=&quot;keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;CommandLineArgs&lt;/span&gt;();
        cmdArgs.IgnoreCase = &lt;span class=&quot;keyword&quot;&gt;true&lt;/span&gt;;
        cmdArgs.PrefixRegexPatternList.Add(&lt;span class=&quot;string&quot;&gt;&amp;quot;/{1}&amp;quot;&lt;/span&gt;);
        cmdArgs.PrefixRegexPatternList.Add(&lt;span class=&quot;string&quot;&gt;&amp;quot;-{1,2}&amp;quot;&lt;/span&gt;);
        cmdArgs.SwitchMatch += (sender, e) =&gt; {
            &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt;(!e.IsValidSwitch) &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt;
            &lt;span class=&quot;keyword&quot;&gt;switch&lt;/span&gt;(e.Switch) {
                &lt;span class=&quot;keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;string&quot;&gt;&amp;quot;foo&amp;quot;&lt;/span&gt;: {
                    &lt;span class=&quot;comment&quot;&gt;// ... handle the /foo -foo or --foo switch logic here ...&lt;/span&gt;
                } &lt;span class=&quot;keyword&quot;&gt;break&lt;/span&gt;;
            }
        };
        cmdArgs.ProcessCommandLineArgs(args);
    }
}&lt;/pre&gt;

By the way, I'm using .NET 3.5 here (notice the nice lambda event handler there... yes, I am lazy and lambda's lend themselves nicely to how I do things) if you couldn't tell.  If you're using .NET 1.1 or 2.0 then you can just create an event handler like you normally would.&lt;br/&gt;
&lt;br/&gt;
This worked nicely.  Only problem now is that I have to have the huge switch statement in my main class.  So in the next iteration I decided it would be nice to be able to register specific switch handlers (one for &amp;quot;/foo&amp;quot;, one for &amp;quot;--bar&amp;quot; and so on.&lt;br/&gt;
&lt;br/&gt;
&lt;pre class=&quot;code&quot;&gt;&lt;span class=&quot;keyword&quot;&gt;static class&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;Program&lt;/span&gt; {
    &lt;span class=&quot;keyword&quot;&gt;static void&lt;/span&gt; Main(&lt;span class=&quot;keyword&quot;&gt;string&lt;/span&gt;[] args) {
        &lt;span class=&quot;class_def&quot;&gt;CommandLineArgs&lt;/span&gt; cmdArgs = &lt;span class=&quot;keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;CommandLineArgs&lt;/span&gt;();
        cmdArgs.IgnoreCase = &lt;span class=&quot;keyword&quot;&gt;true&lt;/span&gt;;
        cmdArgs.PrefixRegexPatternList.Add(&lt;span class=&quot;string&quot;&gt;&amp;quot;/{1}&amp;quot;&lt;/span&gt;);
        cmdArgs.PrefixRegexPatternList.Add(&lt;span class=&quot;string&quot;&gt;&amp;quot;-{1,2}&amp;quot;&lt;/span&gt;);
        cmdArgs.RegisterSpecificSwitchMatchHandler(&amp;quot;foo&amp;quot;, (sender, e) =&gt; {
            &lt;span class=&quot;comment&quot;&gt;// handle the /foo -foo or --foo switch logic here.&lt;/span&gt;
            &lt;span class=&quot;comment&quot;&gt;// this method will only be called for the foo switch.&lt;/span&gt;
        });
        cmdArgs.ProcessCommandLineArgs(args);
    }
}&lt;/pre&gt;

One other item I should also note is that there is also an InvalidArgs array too.  On the events, there is an IsValidSwitch property that will let you know whether or not the switch is valid or not (since the SwitchMatch event is triggered whether or not the switch is valid) so if your command line contains any values that are not directly tied to a switch. For instance if you don't quote a string with spaces: &lt;em&gt;/foo this is a string&lt;/em&gt; like this &lt;em&gt;/foo &amp;quot;this is a string&amp;quot;&lt;/em&gt; then /foo's value will be &amp;quot;this&amp;quot; and is, a, string will all be contained in the invalid args list.  If you use a switch prefix not accounted for in your prefix regex list, it will also be added to your InvalidArgs list (ex: &lt;em&gt;yourapp.exe ^switch &quot;values&quot;&lt;/em&gt; both &lt;em&gt;^switch&lt;/em&gt; and &lt;em&gt;values&lt;/em&gt; will be placed in your InvalidArgs list.&lt;br/&gt;
&lt;br/&gt;
This class accepts the following formats:
&lt;ul&gt;
&lt;li&gt;&amp;lt;switch prefix&amp;gt;switch=value&lt;/li&gt;
&lt;li&gt;&amp;lt;switch prefix&amp;gt;switch value&lt;/li&gt;
&lt;/ul&gt;
&lt;br/&gt;
&lt;br/&gt;
So in the spirit of sharing, here is my &lt;span class=&quot;class_def&quot;&gt;CommandLineArgs&lt;/span&gt; class:

&lt;pre class=&quot;code&quot;&gt;&lt;span class=&quot;keyword&quot;&gt;using&lt;/span&gt; System;
&lt;span class=&quot;keyword&quot;&gt;using&lt;/span&gt; System.Collections.Generic;
&lt;span class=&quot;keyword&quot;&gt;using&lt;/span&gt; System.Text.RegularExpressions;

&lt;span class=&quot;keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;CommandLineArgs&lt;/span&gt; {
    &lt;span class=&quot;keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;string&lt;/span&gt; InvalidSwitchIdent&lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt;ier = &lt;span class=&quot;string&quot;&gt;&amp;quot;INVALID&amp;quot;&lt;/span&gt;;
    &lt;span class=&quot;class_def&quot;&gt;List&lt;/span&gt;&lt;&lt;span class=&quot;keyword&quot;&gt;string&lt;/span&gt;&gt; prefixRegexPatternList = &lt;span class=&quot;keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;List&lt;/span&gt;&lt;&lt;span class=&quot;keyword&quot;&gt;string&lt;/span&gt;&gt;();
    &lt;span class=&quot;class_def&quot;&gt;Dictionary&lt;/span&gt;&lt;&lt;span class=&quot;keyword&quot;&gt;string&lt;/span&gt;, &lt;span class=&quot;keyword&quot;&gt;string&lt;/span&gt;&gt; arguments = &lt;span class=&quot;keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;Dictionary&lt;/span&gt;&lt;&lt;span class=&quot;keyword&quot;&gt;string&lt;/span&gt;, &lt;span class=&quot;keyword&quot;&gt;string&lt;/span&gt;&gt;();
    &lt;span class=&quot;class_def&quot;&gt;List&lt;/span&gt;&lt;&lt;span class=&quot;keyword&quot;&gt;string&lt;/span&gt;&gt; invalidArgs = &lt;span class=&quot;keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;List&lt;/span&gt;&lt;&lt;span class=&quot;keyword&quot;&gt;string&lt;/span&gt;&gt;();
    &lt;span class=&quot;class_def&quot;&gt;Dictionary&lt;/span&gt;&lt;&lt;span class=&quot;keyword&quot;&gt;string&lt;/span&gt;, EventHandler&lt;&lt;span class=&quot;class_def&quot;&gt;CommandLineArgsMatchEventArgs&lt;/span&gt;&gt;&gt; handlers = &lt;span class=&quot;keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;Dictionary&lt;/span&gt;&lt;&lt;span class=&quot;keyword&quot;&gt;string&lt;/span&gt;, EventHandler&lt;&lt;span class=&quot;class_def&quot;&gt;CommandLineArgsMatchEventArgs&lt;/span&gt;&gt;&gt;();
    &lt;span class=&quot;keyword&quot;&gt;bool&lt;/span&gt; ignoreCase = &lt;span class=&quot;keyword&quot;&gt;true&lt;/span&gt;;

    &lt;span class=&quot;keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;event&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;EventHandler&lt;/span&gt;&lt;&lt;span class=&quot;class_def&quot;&gt;CommandLineArgsMatchEventArgs&lt;/span&gt;&gt; SwitchMatch;

    &lt;span class=&quot;keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;int&lt;/span&gt; ArgCount { &lt;span class=&quot;keyword&quot;&gt;get&lt;/span&gt; { &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; arguments.Keys.Count; } }

    &lt;span class=&quot;keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;List&lt;/span&gt;&lt;&lt;span class=&quot;keyword&quot;&gt;string&lt;/span&gt;&gt; PrefixRegexPatternList {
        &lt;span class=&quot;keyword&quot;&gt;get&lt;/span&gt; { &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; prefixRegexPatternList; }
    }

    &lt;span class=&quot;keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;bool&lt;/span&gt; IgnoreCase {
        &lt;span class=&quot;keyword&quot;&gt;get&lt;/span&gt; { &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; ignoreCase; }
        &lt;span class=&quot;keyword&quot;&gt;set&lt;/span&gt; { ignoreCase = &lt;span class=&quot;keyword&quot;&gt;value&lt;/span&gt;; }
    }

    &lt;span class=&quot;keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;string&lt;/span&gt;[] InvalidArgs {
        &lt;span class=&quot;keyword&quot;&gt;get&lt;/span&gt; { &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; invalidArgs.ToArray(); }
    }

    &lt;span class=&quot;keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;this&lt;/span&gt;[&lt;span class=&quot;keyword&quot;&gt;string&lt;/span&gt; key] {
        &lt;span class=&quot;keyword&quot;&gt;get&lt;/span&gt; {
            &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt; (ContainsSwitch(key)) &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; arguments[key];
            &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;null&lt;/span&gt; ;
        }
    }

    &lt;span class=&quot;keyword&quot;&gt;protected&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;virtual&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;void&lt;/span&gt; OnSwitchMatch(&lt;span class=&quot;class_def&quot;&gt;CommandLineArgsMatchEventArgs&lt;/span&gt; e) {
        &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt; (handlers.ContainsKey(e.Switch) &amp;&amp; handlers[e.Switch] != &lt;span class=&quot;keyword&quot;&gt;null&lt;/span&gt; ) handlers[e.Switch](&lt;span class=&quot;keyword&quot;&gt;this&lt;/span&gt;, e);
        &lt;span class=&quot;keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt; (SwitchMatch != &lt;span class=&quot;keyword&quot;&gt;null&lt;/span&gt; ) SwitchMatch(&lt;span class=&quot;keyword&quot;&gt;this&lt;/span&gt;, e);
    }

    &lt;span class=&quot;keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;void&lt;/span&gt; RegisterSpecificSwitchMatchHandler(&lt;span class=&quot;keyword&quot;&gt;string&lt;/span&gt; switchName, &lt;span class=&quot;class_def&quot;&gt;EventHandler&lt;/span&gt;&lt;&lt;span class=&quot;class_def&quot;&gt;CommandLineArgsMatchEventArgs&lt;/span&gt;&gt; handler) {
        &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt; (handlers.ContainsKey(switchName)) handlers[switchName] = handler;
        &lt;span class=&quot;keyword&quot;&gt;else&lt;/span&gt; handlers.Add(switchName, handler);
    }

    &lt;span class=&quot;keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;bool&lt;/span&gt; ContainsSwitch(&lt;span class=&quot;keyword&quot;&gt;string&lt;/span&gt; switchName) {
        &lt;span class=&quot;keyword&quot;&gt;foreach&lt;/span&gt; (&lt;span class=&quot;keyword&quot;&gt;string&lt;/span&gt; pattern &lt;span class=&quot;keyword&quot;&gt;in&lt;/span&gt; prefixRegexPatternList) {
            &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt; (&lt;span class=&quot;class_def&quot;&gt;Regex&lt;/span&gt;.IsMatch(switchName, pattern, &lt;span class=&quot;class_def&quot;&gt;RegexOptions&lt;/span&gt;.Compiled)) {
                switchName = &lt;span class=&quot;class_def&quot;&gt;Regex&lt;/span&gt;.Replace(switchName, pattern, &lt;span class=&quot;string&quot;&gt;&quot;&quot;&lt;/span&gt;, &lt;span class=&quot;class_def&quot;&gt;RegexOptions&lt;/span&gt;.Compiled);
            }
        }
        &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt; (ignoreCase) {
            &lt;span class=&quot;keyword&quot;&gt;foreach&lt;/span&gt; (&lt;span class=&quot;keyword&quot;&gt;string&lt;/span&gt; key &lt;span class=&quot;keyword&quot;&gt;in&lt;/span&gt; arguments.Keys) {
                &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt; (key.ToLower() == switchName.ToLower()) &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;true&lt;/span&gt;;
            }
        } &lt;span class=&quot;keyword&quot;&gt;else&lt;/span&gt; {
            &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; arguments.ContainsKey(switchName);
        }
        &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;false&lt;/span&gt;;
    }

    &lt;span class=&quot;keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;void&lt;/span&gt; ProcessCommandLineArgs(&lt;span class=&quot;keyword&quot;&gt;string&lt;/span&gt;[] args) {
        &lt;span class=&quot;keyword&quot;&gt;for&lt;/span&gt; (&lt;span class=&quot;keyword&quot;&gt;int&lt;/span&gt; i = 0; i &amp;lt; args.Length; i++) {
            &lt;span class=&quot;keyword&quot;&gt;string&lt;/span&gt; value = ignoreCase ? args[i].ToLower() : args[i];
            &lt;span class=&quot;keyword&quot;&gt;foreach&lt;/span&gt; (&lt;span class=&quot;keyword&quot;&gt;string&lt;/span&gt; prefix &lt;span class=&quot;keyword&quot;&gt;in&lt;/span&gt; prefixRegexPatternList) {
                &lt;span class=&quot;keyword&quot;&gt;string&lt;/span&gt; pattern = &lt;span class=&quot;keyword&quot;&gt;string&lt;/span&gt;.Format(&lt;span class=&quot;string&quot;&gt;&quot;^{0}&quot;&lt;/span&gt;, prefix);
                &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt; (&lt;span class=&quot;class_def&quot;&gt;Regex&lt;/span&gt;.IsMatch(value, pattern, &lt;span class=&quot;class_def&quot;&gt;RegexOptions&lt;/span&gt;.Compiled)) {
                    value = &lt;span class=&quot;class_def&quot;&gt;Regex&lt;/span&gt;.Replace(value, pattern, &lt;span class=&quot;string&quot;&gt;&quot;&quot;&lt;/span&gt;, &lt;span class=&quot;class_def&quot;&gt;RegexOptions&lt;/span&gt;.Compiled);
                    &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt; (value.Contains(&lt;span class=&quot;string&quot;&gt;&quot;=&quot;&lt;/span&gt;)) { &lt;span class=&quot;comment&quot;&gt;// &amp;quot;&amp;lt;prefix&amp;gt;Param=Value&amp;quot;&lt;/span&gt;
                        &lt;span class=&quot;keyword&quot;&gt;int&lt;/span&gt; idx = value.IndexOf(&lt;span class=&quot;string&quot;&gt;'='&lt;/span&gt;);
                        &lt;span class=&quot;keyword&quot;&gt;string&lt;/span&gt; n = value.SubString(0, idx);
                        &lt;span class=&quot;keyword&quot;&gt;string&lt;/span&gt; v = value.SubString(idx + 1, value.Length - n.Length - 1);
                        OnSwitchMatch(&lt;span class=&quot;keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;CommandLineArgsMatchEventArgs&lt;/span&gt;(n, v));
                        arguments.Add(n, v);
                    } &lt;span class=&quot;keyword&quot;&gt;else&lt;/span&gt; { &lt;span class=&quot;comment&quot;&gt;// &amp;quot;&amp;lt;prefix&amp;gt;Param Value&amp;quot;&lt;/span&gt;
                        &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt; (i + 1 &amp;lt; args.Length) {
                            &lt;span class=&quot;keyword&quot;&gt;string&lt;/span&gt; @switch = value;
                            &lt;span class=&quot;keyword&quot;&gt;string&lt;/span&gt; val  = args[i + 1];
                            OnSwitchMatch(&lt;span class=&quot;keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;CommandLineArgsMatchEventArgs&lt;/span&gt;(@switch, val));
                            arguments.Add(value, val);
                            i++;
                        } &lt;span class=&quot;keyword&quot;&gt;else&lt;/span&gt; {
                            OnSwitchMatch(&lt;span class=&quot;keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;CommandLineArgsMatchEventArgs&lt;/span&gt;(value, &lt;span class=&quot;keyword&quot;&gt;null&lt;/span&gt; ));
                            arguments.Add(value, &lt;span class=&quot;keyword&quot;&gt;null&lt;/span&gt; );
                        }
                    }
                } &lt;span class=&quot;keyword&quot;&gt;else&lt;/span&gt; { &lt;span class=&quot;comment&quot;&gt;// invalid arg ...&lt;/span&gt;
                    OnSwitchMatch(&lt;span class=&quot;keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;CommandLineArgsMatchEventArgs&lt;/span&gt;(InvalidSwitchIdentifier, value, &lt;span class=&quot;keyword&quot;&gt;false&lt;/span&gt;));
                    invalidArgs.Add(value);
                }
            }
        }
    }
}

&lt;span class=&quot;keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;CommandLineArgsMatchEventArgs&lt;/span&gt; : &lt;span class=&quot;class_def&quot;&gt;EventArgs&lt;/span&gt; {
    &lt;span class=&quot;keyword&quot;&gt;string&lt;/span&gt; @switch;
    &lt;span class=&quot;keyword&quot;&gt;string&lt;/span&gt; value;
    &lt;span class=&quot;keyword&quot;&gt;bool&lt;/span&gt; isValidSwitch = &lt;span class=&quot;keyword&quot;&gt;true&lt;/span&gt;;

    &lt;span class=&quot;keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;string&lt;/span&gt; Switch {
        &lt;span class=&quot;keyword&quot;&gt;get&lt;/span&gt; { &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; @switch; } 
    }

    &lt;span class=&quot;keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;string&lt;/span&gt; Value {
        &lt;span class=&quot;keyword&quot;&gt;get&lt;/span&gt; { &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; value; }
    }

    &lt;span class=&quot;keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;bool&lt;/span&gt; IsValidSwitch {
        &lt;span class=&quot;keyword&quot;&gt;get&lt;/span&gt; { &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; isValidSwitch; }
    }

    &lt;span class=&quot;keyword&quot;&gt;public&lt;/span&gt; CommandLineArgsMatchEventArgs(&lt;span class=&quot;keyword&quot;&gt;string&lt;/span&gt; @switch, &lt;span class=&quot;keyword&quot;&gt;string&lt;/span&gt; value)
        : &lt;span class=&quot;keyword&quot;&gt;this&lt;/span&gt;(@switch, value, &lt;span class=&quot;keyword&quot;&gt;true&lt;/span&gt;) { }

    &lt;span class=&quot;keyword&quot;&gt;public&lt;/span&gt; CommandLineArgsMatchEventArgs(&lt;span class=&quot;keyword&quot;&gt;string&lt;/span&gt; @switch, &lt;span class=&quot;keyword&quot;&gt;string&lt;/span&gt; value, &lt;span class=&quot;keyword&quot;&gt;bool&lt;/span&gt; isValidSwitch) {
        &lt;span class=&quot;keyword&quot;&gt;this&lt;/span&gt;.@switch = @switch;
        &lt;span class=&quot;keyword&quot;&gt;this&lt;/span&gt;.value = value;
        &lt;span class=&quot;keyword&quot;&gt;this&lt;/span&gt;.isValidSwitch = isValidSwitch;
    }
}&lt;/pre&gt;</description>
      <pubDate>Thu, 31 Jan 2008 18:49:18 EST</pubDate>
      <link>http://sanity-free.org/144/csharp_command_line_args_processing_class.html</link>
    </item>
        <item>
      <title>C# .NET Single Instance Application</title>
      <description>Today I wanted to refactor some code that prohibited my application from running multiple instances of itself.&lt;br /&gt;
&lt;br /&gt;
Previously I had use System.Diagnostics.Process to search for an instance of my myapp.exe in the process list.  While this works, it brings on a lot of overhead, and I wanted something cleaner.&lt;br /&gt;
&lt;br /&gt;
Knowing that I could use a mutex for this (but never having done it before) I set out to cut down my code and simplify my life.&lt;br /&gt;
&lt;br /&gt;
In the class of my application main I created a static named Mutex:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;code&quot;&gt;&lt;span class=&quot;keyword&quot;&gt;static class&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;Program&lt;/span&gt; {
    &lt;span class=&quot;keyword&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;Mutex&lt;/span&gt; mutex = &lt;span class=&quot;keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;Mutex&lt;/span&gt;(&lt;span class=&quot;keyword&quot;&gt;true&lt;/span&gt;, &lt;span class=&quot;string&quot;&gt;&amp;quot;{8F6F0AC4-B9A1-45fd-A8CF-72F04E6BDE8F}&amp;quot;&lt;/span&gt;);
    [&lt;span class=&quot;class_def&quot;&gt;STAThread&lt;/span&gt;]
    ...
}&lt;/pre&gt;

Having a named mutex allows us to stack synchronization across multiple threads and processes which is just the magic I'm looking for.&lt;br /&gt;
&lt;br /&gt;
Mutex.WaitOne has an overload that specifies an amount of time for us to wait.  Since we're not actually wanting to synchronizing our code (more just check if it is currently in use) we use the overload with two parameters: Mutex.WaitOne(Timespan timeout, bool exitContext).  Wait one returns true if it is able to enter, and false if it wasn't.  In this case, we don't want to wait at all; If our mutex is being used, skip it, and move on, so we pass in TimeSpan.Zero (wait 0 milliseconds), and set the exitContext to true so we can exit the synchronization context before we try to aquire a lock on it. Using this, we wrap our Application.Run code inside something like this:&lt;br /&gt;
&lt;br /&gt;
&lt;!-- readmore --&gt;
&lt;pre class=&quot;code&quot;&gt;&lt;span class=&quot;keyword&quot;&gt;static class&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;Program&lt;/span&gt; {
    &lt;span class=&quot;keyword&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;Mutex&lt;/span&gt; mutex = &lt;span class=&quot;keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;Mutex&lt;/span&gt;(&lt;span class=&quot;keyword&quot;&gt;true&lt;/span&gt;, &lt;span class=&quot;string&quot;&gt;&amp;quot;{8F6F0AC4-B9A1-45fd-A8CF-72F04E6BDE8F}&amp;quot;&lt;/span&gt;);
    [&lt;span class=&quot;class_def&quot;&gt;STAThread&lt;/span&gt;]
    &lt;span class=&quot;keyword&quot;&gt;static void&lt;/span&gt; Main() {
        &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt;(mutex.WaitOne(&lt;span class=&quot;class_def&quot;&gt;TimeSpan&lt;/span&gt;.Zero, &lt;span class=&quot;keyword&quot;&gt;true&lt;/span&gt;)) {
            &lt;span class=&quot;class_def&quot;&gt;Application&lt;/span&gt;.EnableVisualStyles();
            &lt;span class=&quot;class_def&quot;&gt;Application&lt;/span&gt;.SetCompatibleTextRenderingDefault(&lt;span class=&quot;keyword&quot;&gt;false&lt;/span&gt;);
            &lt;span class=&quot;class_def&quot;&gt;Application&lt;/span&gt;.Run(&lt;span class=&quot;keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;Form1&lt;/span&gt;());
            mutex.ReleaseMutex();
        } &lt;span class=&quot;keyword&quot;&gt;else&lt;/span&gt; {
            &lt;span class=&quot;class_def&quot;&gt;MessageBox&lt;/span&gt;.Show(&lt;span class=&quot;string&quot;&gt;&quot;only one instance at a time&quot;&lt;/span&gt;);
        }
    }
}&lt;/pre&gt;

So, if our app is running, WaitOne will return false, and we'll get a message box.&lt;br /&gt;
&lt;br /&gt;
Instead of showing a message box, I opted to utilize a little Win32 to notify my running instance that someone forgot that it was already running (by bringing itself to the top of all the other windows). To achieve this I used &lt;a href=&quot;http://msdn2.microsoft.com/en-us/library/ms644944.aspx&quot; title=&quot;PostMessage documentation on MSDN&quot;&gt;PostMessage&lt;/a&gt; to broadcast a custom message to every window (the custom message was registered with &lt;a href=&quot;http://msdn2.microsoft.com/en-us/library/ms644947.aspx&quot; title=&quot;RegisterWindowMessage documentation on MSDN&quot;&gt;RegisterWindowMessage&lt;/a&gt; by my running application, which means only my application knows what it is) then my second instance exits.  The running application instance would receive that notification and process it.  In order to do that, I overrode WndProc in my main form and listened for my custom notification.  When I received that notification I set the form's TopMost property to true to bring it up on top.&lt;br /&gt;
&lt;br /&gt;
Here is what I ended up with:&lt;br /&gt;
&lt;br /&gt;
Program.cs
&lt;pre class=&quot;code&quot;&gt;&lt;span class=&quot;keyword&quot;&gt;static class&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;Program&lt;/span&gt; {
    &lt;span class=&quot;keyword&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;Mutex&lt;/span&gt; mutex = &lt;span class=&quot;keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;Mutex&lt;/span&gt;(&lt;span class=&quot;keyword&quot;&gt;true&lt;/span&gt;, &lt;span class=&quot;string&quot;&gt;&quot;{8F6F0AC4-B9A1-45fd-A8CF-72F04E6BDE8F}&quot;&lt;/span&gt;);
    [&lt;span class=&quot;class_def&quot;&gt;STAThread&lt;/span&gt;]
    &lt;span class=&quot;keyword&quot;&gt;static void&lt;/span&gt; Main() {
        &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt;(mutex.WaitOne(&lt;span class=&quot;class_def&quot;&gt;TimeSpan&lt;/span&gt;.Zero, &lt;span class=&quot;keyword&quot;&gt;true&lt;/span&gt;)) {
            &lt;span class=&quot;class_def&quot;&gt;Application&lt;/span&gt;.EnableVisualStyles();
            &lt;span class=&quot;class_def&quot;&gt;Application&lt;/span&gt;.SetCompatibleTextRenderingDefault(&lt;span class=&quot;keyword&quot;&gt;false&lt;/span&gt;);
            &lt;span class=&quot;class_def&quot;&gt;Application&lt;/span&gt;.Run(&lt;span class=&quot;keyword&quot;&gt;new&lt;/span&gt; Form1());
            mutex.ReleaseMutex();
        } &lt;span class=&quot;keyword&quot;&gt;else&lt;/span&gt; {
            &lt;span class=&quot;comment&quot;&gt;// send our Win32 message to make the currently running instance
            // jump on top of all the other windows&lt;/span&gt;
            &lt;span class=&quot;class_def&quot;&gt;NativeMethods&lt;/span&gt;.PostMessage(
                (&lt;span class=&quot;class_def&quot;&gt;IntPtr&lt;/span&gt;)&lt;span class=&quot;class_def&quot;&gt;NativeMethods&lt;/span&gt;.HWND_BROADCAST,
                &lt;span class=&quot;class_def&quot;&gt;NativeMethods&lt;/span&gt;.WM_SHOWME,
                &lt;span class=&quot;class_def&quot;&gt;IntPtr&lt;/span&gt;.Zero,
                &lt;span class=&quot;class_def&quot;&gt;IntPtr&lt;/span&gt;.Zero);
        }
    }
}&lt;/pre&gt;

NativeMethods.cs
&lt;pre class=&quot;code&quot;&gt;&lt;span class=&quot;comment&quot;&gt;// this class just wraps some Win32 stuffthat we're going to use&lt;/span&gt;
&lt;span class=&quot;keyword&quot;&gt;internal class&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;NativeMethods&lt;/span&gt; {
    &lt;span class=&quot;keyword&quot;&gt;public const int&lt;/span&gt; HWND_BROADCAST = 0xffff;
    &lt;span class=&quot;keyword&quot;&gt;public static readonly int&lt;/span&gt; WM_SHOWME = RegisterWindowMessage(&lt;span class=&quot;string&quot;&gt;&quot;WM_SHOWME&quot;&lt;/span&gt;);
    [&lt;span class=&quot;class_def&quot;&gt;DllImport&lt;/span&gt;(&lt;span class=&quot;string&quot;&gt;&quot;user32&quot;&lt;/span&gt;)]
    &lt;span class=&quot;keyword&quot;&gt;public static extern bool&lt;/span&gt; PostMessage(&lt;span class=&quot;class_def&quot;&gt;IntPtr&lt;/span&gt; hwnd, &lt;span class=&quot;keyword&quot;&gt;int&lt;/span&gt; msg, &lt;span class=&quot;class_def&quot;&gt;IntPtr&lt;/span&gt; wparam, &lt;span class=&quot;class_def&quot;&gt;IntPtr&lt;/span&gt; lparam);
    [&lt;span class=&quot;class_def&quot;&gt;DllImport&lt;/span&gt;(&lt;span class=&quot;string&quot;&gt;&quot;user32&quot;&lt;/span&gt;)]
    &lt;span class=&quot;keyword&quot;&gt;public static extern int&lt;/span&gt; RegisterWindowMessage(&lt;span class=&quot;keyword&quot;&gt;string&lt;/span&gt; message);
}&lt;/pre&gt;

and&lt;br /&gt;
&lt;br /&gt;
Form1.cs (front side partial)
&lt;pre class=&quot;code&quot;&gt;&lt;span class=&quot;keyword&quot;&gt;public partial class&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;Form1&lt;/span&gt; : &lt;span class=&quot;class_def&quot;&gt;Form&lt;/span&gt; {
    &lt;span class=&quot;keyword&quot;&gt;public&lt;/span&gt; Form1() {
        InitializeComponent();
    }
    &lt;span class=&quot;keyword&quot;&gt;protected override void&lt;/span&gt; WndProc(&lt;span class=&quot;keyword&quot;&gt;ref&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;Message&lt;/span&gt; m) {
        &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt;(m.Msg == &lt;span class=&quot;class_def&quot;&gt;NativeMethods&lt;/span&gt;.WM_SHOWME) {
            ShowMe();
        }
        &lt;span class=&quot;keyword&quot;&gt;base&lt;/span&gt;.WndProc(&lt;span class=&quot;keyword&quot;&gt;ref&lt;/span&gt; m);
    }
    &lt;span class=&quot;keyword&quot;&gt;private void&lt;/span&gt; ShowMe() {
        &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt;(WindowState == &lt;span class=&quot;class_def&quot;&gt;FormWindowState&lt;/span&gt;.Minimized) {
            WindowState = &lt;span class=&quot;class_def&quot;&gt;FormWindowState&lt;/span&gt;.Normal;
        }
        &lt;span class=&quot;comment&quot;&gt;// get our current &amp;quot;TopMost&amp;quot; value (ours will always be false though)&lt;/span&gt;
        &lt;span class=&quot;keyword&quot;&gt;bool&lt;/span&gt; top = TopMost;
        &lt;span class=&quot;comment&quot;&gt;// make our form jump to the top of everything&lt;/span&gt;
        TopMost = &lt;span class=&quot;keyword&quot;&gt;true&lt;/span&gt;;
        &lt;span class=&quot;comment&quot;&gt;// set it back to whatever it was&lt;/span&gt;
        TopMost = top;
    }
}&lt;/pre&gt;</description>
      <pubDate>Wed, 06 Jun 2007 20:35:32 EDT</pubDate>
      <link>http://sanity-free.org/143/csharp_dotnet_single_instance_application.html</link>
    </item>
        <item>
      <title>Creating Fake Enums</title>
      <description>In .NET enums are a strongly typed flag-like object.  Flags would normally defined as a macro, or constant variable.  The trouble with typical numeric flags is that a) the coder using the flag may or may not know all of the possible values, or b) might accidentally type the wrong constant or macro.  Since the flag is just some magic number they may end up with a logic error that may be hard to find under certain circumstances.  With enums I can provide the same logic (some numeric flag) but create a type that I can use to enforce some subset of numbers that are checked at compile time, and can be verified at run time ( Enum.IsDefined ).  As with everything, enums have their faults.  What if I want to extend some enum object (polymorphic enums?), or give it additional functionality or meta data. Yesterday at work we ran across a case where being able to extend an enum was mandatory.  All of our enums have numeric and string values, and are typically written like:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;code&quot;&gt;
&lt;span class=&quot;keyword&quot;&gt;public enum&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;Foo&lt;/span&gt; {
    [&lt;span class=&quot;class_def&quot;&gt;FriendlyName&lt;/span&gt;(&lt;span class=&quot;string&quot;&gt;&amp;quot;One Foo&amp;quot;&lt;/span&gt;)]
    One = 1,
    [&lt;span class=&quot;class_def&quot;&gt;FriendlyName&lt;/span&gt;(&lt;span class=&quot;string&quot;&gt;&amp;quot;Two Foo&amp;quot;&lt;/span&gt;)]
    Two = 2,
    [&lt;span class=&quot;class_def&quot;&gt;FriendlyName&lt;/span&gt;(&lt;span class=&quot;string&quot;&gt;&amp;quot;Three Foo&amp;quot;&lt;/span&gt;)]
    Three = 3
}&lt;/pre&gt;

the FriendlyName attribute enables you to have both a numeric flag value as well as some human readable value of the enum element other than &quot;One&quot; or &quot;Two&quot; or &quot;SomeEnumValue&quot;(ie: Foo.One.ToString())&lt;br /&gt;
&lt;br /&gt;
Our company uses code generation like most fish use water.  We use enums quite a bit and each enum type and all of their values are stored in a database which is used by the code generation tool to generate 99% of our business and data layer code.  Since our code is generated, if we go in and manually add additional values that don't need to be stored in the database, our generation tool will blow away any hand written changes on every build.  We would have killed to have something like a partial enum, but alas, no such thing exists.&lt;br /&gt;
&lt;br /&gt;
I set out to try to create a &amp;quot;Fake Enum&amp;quot; class that acted and felt like an enum but allowed us to extend it by way of the partial class or inheritance.  Well after a little poking around, inheritance flew out the window (just over complex without resorting to some generics base type), but the ability to do a partial fake enum was definitely doable.&lt;br /&gt;
&lt;br /&gt;
Here's the code I came up with:&lt;br /&gt;
&lt;br /&gt;
&lt;!-- readmore --&gt;
&lt;pre class=&quot;code&quot;&gt;
&lt;span class=&quot;keyword&quot;&gt;using&lt;/span&gt; System;
&lt;span class=&quot;keyword&quot;&gt;using&lt;/span&gt; System.ComponentModel;
&lt;span class=&quot;keyword&quot;&gt;using&lt;/span&gt; System.Globalization
&lt;span class=&quot;keyword&quot;&gt;using&lt;/span&gt; System.Reflection;

[&lt;span class=&quot;class_def&quot;&gt;TypeConverter&lt;/span&gt;(&lt;span class=&quot;keyword&quot;&gt;typeof&lt;/span&gt;(&lt;span class=&quot;class_def&quot;&gt;FakeEnum&lt;/span&gt;.&lt;span class=&quot;class_def&quot;&gt;FakeEnumConverter&lt;/span&gt;))]
&lt;span class=&quot;keyword&quot;&gt;public partial class&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;FakeEnum&lt;/span&gt; {

    &lt;span class=&quot;comment&quot;&gt;// typical &quot;Enum&quot; declarations, sort of like One = 1, Two = 2, Three&lt;/span&gt;
    &lt;span class=&quot;keyword&quot;&gt;public static readonly&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;FakeEnum&lt;/span&gt; One = &lt;span class=&quot;keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;FakeEnum&lt;/span&gt;(1, &lt;span class=&quot;string&quot;&gt;&quot;One's Friendly Name&quot;&lt;/span&gt;);
    &lt;span class=&quot;keyword&quot;&gt;public static readonly&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;FakeEnum&lt;/span&gt; Two = &lt;span class=&quot;keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;FakeEnum&lt;/span&gt;(2, &lt;span class=&quot;string&quot;&gt;&quot;Two's Friendly Name&quot;&lt;/span&gt;);
    &lt;span class=&quot;keyword&quot;&gt;public static readonly&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;FakeEnum&lt;/span&gt; Three = &lt;span class=&quot;keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;FakeEnum&lt;/span&gt;(3, &lt;span class=&quot;string&quot;&gt;&quot;Three's Friendly Name&quot;&lt;/span&gt;);
    &lt;span class=&quot;keyword&quot;&gt;public static readonly&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;FakeEnum&lt;/span&gt; Four = &lt;span class=&quot;keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;FakeEnum&lt;/span&gt;(4);
    &lt;span class=&quot;keyword&quot;&gt;public static readonly&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;FakeEnum&lt;/span&gt; Five = &lt;span class=&quot;keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;FakeEnum&lt;/span&gt;(5);
    &lt;span class=&quot;keyword&quot;&gt;public static readonly&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;FakeEnum&lt;/span&gt; Six = &lt;span class=&quot;keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;FakeEnum&lt;/span&gt;(6);


    &lt;span class=&quot;comment&quot;&gt;// implementation to provide &quot;Enum&quot; like functionality&lt;/span&gt;
    &lt;span class=&quot;keyword&quot;&gt;int&lt;/span&gt; value;
    &lt;span class=&quot;keyword&quot;&gt;string&lt;/span&gt; friendlyName;

    &lt;span class=&quot;keyword&quot;&gt;public string&lt;/span&gt; FriendlyName { &lt;span class=&quot;keyword&quot;&gt;get&lt;/span&gt; { &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; friendlyName; } }

    &lt;span class=&quot;keyword&quot;&gt;public override string&lt;/span&gt; ToString() {
        &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; ToString(&lt;span class=&quot;string&quot;&gt;&quot;&quot;&lt;/span&gt;);
    }

    &lt;span class=&quot;keyword&quot;&gt;public virtual string&lt;/span&gt; ToString(&lt;span class=&quot;keyword&quot;&gt;string&lt;/span&gt; format) {
        &lt;span class=&quot;keyword&quot;&gt;foreach&lt;/span&gt;(&lt;span class=&quot;class_def&quot;&gt;FieldInfo&lt;/span&gt; staticField in GetType().GetFields(&lt;span class=&quot;class_def&quot;&gt;BindingFlags&lt;/span&gt;.Static | &lt;span class=&quot;class_def&quot;&gt;BindingFlags&lt;/span&gt;.Public | &lt;span class=&quot;class_def&quot;&gt;BindingFlags&lt;/span&gt;.FlattenHierarchy)) {
            &lt;span class=&quot;class_def&quot;&gt;FakeEnum&lt;/span&gt; temp = staticField.GetValue(&lt;span class=&quot;keyword&quot;&gt;null&lt;/span&gt;) &lt;span class=&quot;keyword&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;FakeEnum&lt;/span&gt;;
            &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt;(temp == &lt;span class=&quot;keyword&quot;&gt;null&lt;/span&gt;) &lt;span class=&quot;keyword&quot;&gt;continue&lt;/span&gt;;
            &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt;(temp.value == value) {
                &lt;span class=&quot;keyword&quot;&gt;switch&lt;/span&gt;(format) {
                    &lt;span class=&quot;keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;string&quot;&gt;&quot;fn&quot;&lt;/span&gt;: {
                        &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; temp.friendlyName;
                    }
                    &lt;span class=&quot;keyword&quot;&gt;default&lt;/span&gt;: {
                        &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; staticField.Name;
                    }
                }
            }
        }
        &lt;span class=&quot;keyword&quot;&gt;return base&lt;/span&gt;.ToString();
    }

    &lt;span class=&quot;keyword&quot;&gt;public override int&lt;/span&gt; GetHashCode() {
        &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; value.GetHashCode();
    }

    &lt;span class=&quot;keyword&quot;&gt;public override bool&lt;/span&gt; Equals(&lt;span class=&quot;keyword&quot;&gt;object&lt;/span&gt; obj) {
        &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt;(&lt;span class=&quot;keyword&quot;&gt;object&lt;/span&gt;.ReferenceEquals(obj, &lt;span class=&quot;keyword&quot;&gt;null&lt;/span&gt;)) &lt;span class=&quot;keyword&quot;&gt;return false&lt;/span&gt;;
        &lt;span class=&quot;class_def&quot;&gt;FakeEnum&lt;/span&gt; temp = obj &lt;span class=&quot;keyword&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;FakeEnum&lt;/span&gt;;
        &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt;(!&lt;span class=&quot;keyword&quot;&gt;object&lt;/span&gt;.ReferenceEquals(temp, &lt;span class=&quot;keyword&quot;&gt;null&lt;/span&gt;)) {
            &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; temp.value == value;
        } &lt;span class=&quot;keyword&quot;&gt;else&lt;/span&gt; {
            &lt;span class=&quot;keyword&quot;&gt;return false&lt;/span&gt;;
        }
    }

    &lt;span class=&quot;keyword&quot;&gt;public static object&lt;/span&gt; Parse(&lt;span class=&quot;class_def&quot;&gt;Type&lt;/span&gt; type, &lt;span class=&quot;keyword&quot;&gt;int&lt;/span&gt; value) {
        &lt;span class=&quot;keyword&quot;&gt;foreach&lt;/span&gt;(&lt;span class=&quot;class_def&quot;&gt;FieldInfo&lt;/span&gt; fieldInfo &lt;span class=&quot;keyword&quot;&gt;in&lt;/span&gt; type.GetFields(&lt;span class=&quot;class_def&quot;&gt;BindingFlags&lt;/span&gt;.FlattenHierarchy | &lt;span class=&quot;class_def&quot;&gt;BindingFlags&lt;/span&gt;.Static | &lt;span class=&quot;class_def&quot;&gt;BindingFlags&lt;/span&gt;.Public)) {
            &lt;span class=&quot;class_def&quot;&gt;FakeEnum&lt;/span&gt; enumValue = fieldInfo.GetValue(&lt;span class=&quot;keyword&quot;&gt;null&lt;/span&gt;) &lt;span class=&quot;keyword&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;FakeEnum&lt;/span&gt;;
            &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt;(enumValue != &lt;span class=&quot;keyword&quot;&gt;null&lt;/span&gt;) {
                &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt;(enumValue.value == value) {
                    &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; enumValue;
                }
            }
        }
        &lt;span class=&quot;keyword&quot;&gt;throw new&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;ArgumentException&lt;/span&gt;(&lt;span class=&quot;keyword&quot;&gt;string&lt;/span&gt;.Format(&lt;span class=&quot;string&quot;&gt;&quot;{0} is not defined in {1}&quot;&lt;/span&gt;, value, type.Name));
    }

    &lt;span class=&quot;keyword&quot;&gt;public static object&lt;/span&gt; Parse(&lt;span class=&quot;class_def&quot;&gt;Type&lt;/span&gt; type, &lt;span class=&quot;keyword&quot;&gt;string&lt;/span&gt; value) {
        &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; Parse(type, value, &lt;span class=&quot;keyword&quot;&gt;false&lt;/span&gt;);
    }

    &lt;span class=&quot;keyword&quot;&gt;public static object&lt;/span&gt; Parse(&lt;span class=&quot;class_def&quot;&gt;Type&lt;/span&gt; type, &lt;span class=&quot;keyword&quot;&gt;string&lt;/span&gt; value, &lt;span class=&quot;keyword&quot;&gt;bool&lt;/span&gt; ignoreCase) {
        &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt;(&lt;span class=&quot;keyword&quot;&gt;string&lt;/span&gt;.IsNullOrEmpty(value)) &lt;span class=&quot;keyword&quot;&gt;throw new&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;ArgumentNullException&lt;/span&gt;(&lt;span class=&quot;string&quot;&gt;&quot;value was either null or empty&quot;&lt;/span&gt;);
        &lt;span class=&quot;keyword&quot;&gt;foreach&lt;/span&gt;(&lt;span class=&quot;class_def&quot;&gt;FieldInfo&lt;/span&gt; field &lt;span class=&quot;keyword&quot;&gt;in&lt;/span&gt; type.GetFields(&lt;span class=&quot;class_def&quot;&gt;BindingFlags&lt;/span&gt;.Public | &lt;span class=&quot;class_def&quot;&gt;BindingFlags&lt;/span&gt;.Static | &lt;span class=&quot;class_def&quot;&gt;BindingFlags&lt;/span&gt;.FlattenHierarchy)) {
            &lt;span class=&quot;keyword&quot;&gt;bool&lt;/span&gt; isMatch = &lt;span class=&quot;keyword&quot;&gt;false&lt;/span&gt;;
            &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt;(ignoreCase) {
                isMatch = &lt;span class=&quot;class_def&quot;&gt;StringComparer&lt;/span&gt;.InvariantCultureIgnoreCase.Compare(field.Name, value) == 0;
            } &lt;span class=&quot;keyword&quot;&gt;else&lt;/span&gt; {
                isMatch = &lt;span class=&quot;class_def&quot;&gt;StringComparer&lt;/span&gt;.InvariantCulture.Compare(field.Name, value) == 0;
            }
            &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt;(isMatch) {
                &lt;span class=&quot;class_def&quot;&gt;FakeEnum&lt;/span&gt; temp = field.GetValue(&lt;span class=&quot;keyword&quot;&gt;null&lt;/span&gt;) &lt;span class=&quot;keyword&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;FakeEnum&lt;/span&gt;;
                &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt;(temp == &lt;span class=&quot;keyword&quot;&gt;null&lt;/span&gt;) &lt;span class=&quot;keyword&quot;&gt;throw new&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;InvalidOperationException&lt;/span&gt;(&lt;span class=&quot;keyword&quot;&gt;string&lt;/span&gt;.Format(&lt;span class=&quot;string&quot;&gt;&quot;{0} not convertable to {1}&quot;&lt;/span&gt;, type, &lt;span class=&quot;keyword&quot;&gt;typeof&lt;/span&gt;(&lt;span class=&quot;class_def&quot;&gt;FakeEnum&lt;/span&gt;)));
                &lt;span class=&quot;keyword&quot;&gt;object&lt;/span&gt; instance = &lt;span class=&quot;class_def&quot;&gt;Activator&lt;/span&gt;.CreateInstance(type, &lt;span class=&quot;keyword&quot;&gt;new object&lt;/span&gt;[] { temp.value, temp.friendlyName });
                &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; instance;
            }
        }
        &lt;span class=&quot;keyword&quot;&gt;throw new&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;ArgumentException&lt;/span&gt;(&lt;span class=&quot;keyword&quot;&gt;string&lt;/span&gt;.Format(&lt;span class=&quot;string&quot;&gt;&quot;{0} is not defined in {1}&quot;&lt;/span&gt;, value, type.Name));
    }

    &lt;span class=&quot;keyword&quot;&gt;public static bool&lt;/span&gt; IsDefined(&lt;span class=&quot;class_def&quot;&gt;Type&lt;/span&gt; type, &lt;span class=&quot;keyword&quot;&gt;object&lt;/span&gt; value) {
        &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt;(&lt;span class=&quot;keyword&quot;&gt;object&lt;/span&gt;.ReferenceEquals(value, &lt;span class=&quot;keyword&quot;&gt;null&lt;/span&gt;)) &lt;span class=&quot;keyword&quot;&gt;throw new&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;ArgumentNullException&lt;/span&gt;(&lt;span class=&quot;string&quot;&gt;&quot;value&quot;&lt;/span&gt;);
        &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt;(&lt;span class=&quot;keyword&quot;&gt;typeof&lt;/span&gt;(&lt;span class=&quot;class_def&quot;&gt;FakeEnum&lt;/span&gt;).IsAssignableFrom(type)) {
            &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; IsDefined(type, ((&lt;span class=&quot;class_def&quot;&gt;FakeEnum&lt;/span&gt;)value).value);
        }
        &lt;span class=&quot;keyword&quot;&gt;return false&lt;/span&gt;;
    }

    &lt;span class=&quot;keyword&quot;&gt;public static bool&lt;/span&gt; IsDefined(&lt;span class=&quot;class_def&quot;&gt;Type&lt;/span&gt; type, &lt;span class=&quot;keyword&quot;&gt;int&lt;/span&gt; value) {
        &lt;span class=&quot;keyword&quot;&gt;foreach&lt;/span&gt;(&lt;span class=&quot;class_def&quot;&gt;FieldInfo&lt;/span&gt; staticField &lt;span class=&quot;keyword&quot;&gt;in&lt;/span&gt; type.GetFields(&lt;span class=&quot;class_def&quot;&gt;BindingFlags&lt;/span&gt;.Static | &lt;span class=&quot;class_def&quot;&gt;BindingFlags&lt;/span&gt;.Public | &lt;span class=&quot;class_def&quot;&gt;BindingFlags&lt;/span&gt;.FlattenHierarchy)) {
            &lt;span class=&quot;keyword&quot;&gt;object&lt;/span&gt; temp = staticField.GetValue(&lt;span class=&quot;keyword&quot;&gt;null&lt;/span&gt;);
            &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt;(temp == &lt;span class=&quot;keyword&quot;&gt;null&lt;/span&gt;) &lt;span class=&quot;keyword&quot;&gt;continue&lt;/span&gt;;
            &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt;(&lt;span class=&quot;keyword&quot;&gt;typeof&lt;/span&gt;(&lt;span class=&quot;class_def&quot;&gt;FakeEnum&lt;/span&gt;).IsAssignableFrom(type)) {
                &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt;(((&lt;span class=&quot;class_def&quot;&gt;FakeEnum&lt;/span&gt;)temp).value == value) &lt;span class=&quot;keyword&quot;&gt;return true&lt;/span&gt;;
            }
        }
        &lt;span class=&quot;keyword&quot;&gt;return false&lt;/span&gt;;
    }

    &lt;span class=&quot;keyword&quot;&gt;public static bool&lt;/span&gt; IsDefined(&lt;span class=&quot;class_def&quot;&gt;Type&lt;/span&gt; type, &lt;span class=&quot;keyword&quot;&gt;string&lt;/span&gt; value) {
        &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; IsDefined(type, value, false);
    }

    &lt;span class=&quot;keyword&quot;&gt;public static bool&lt;/span&gt; IsDefined(&lt;span class=&quot;class_def&quot;&gt;Type&lt;/span&gt; type, &lt;span class=&quot;keyword&quot;&gt;string&lt;/span&gt; value, &lt;span class=&quot;keyword&quot;&gt;bool&lt;/span&gt; ignoreCase) {
        &lt;span class=&quot;keyword&quot;&gt;foreach&lt;/span&gt;(&lt;span class=&quot;class_def&quot;&gt;FieldInfo&lt;/span&gt; staticField &lt;span class=&quot;keyword&quot;&gt;in&lt;/span&gt; type.GetFields(&lt;span class=&quot;class_def&quot;&gt;BindingFlags&lt;/span&gt;.Static | &lt;span class=&quot;class_def&quot;&gt;BindingFlags&lt;/span&gt;.Public | &lt;span class=&quot;class_def&quot;&gt;BindingFlags&lt;/span&gt;.FlattenHierarchy)) {
            &lt;span class=&quot;keyword&quot;&gt;bool&lt;/span&gt; isMatch = &lt;span class=&quot;keyword&quot;&gt;false&lt;/span&gt;;
            &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt;(ignoreCase) {
                isMatch = &lt;span class=&quot;class_def&quot;&gt;StringComparer&lt;/span&gt;.InvariantCultureIgnoreCase.Compare(staticField.Name, value) == 0;
            } else {
                isMatch = &lt;span class=&quot;class_def&quot;&gt;StringComparer&lt;/span&gt;.InvariantCulture.Compare(staticField.Name, value) == 0;
            }
            &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt;(isMatch) &lt;span class=&quot;keyword&quot;&gt;return true&lt;/span&gt;;
        }
        &lt;span class=&quot;keyword&quot;&gt;return false&lt;/span&gt;;
    }

    &lt;span class=&quot;keyword&quot;&gt;public static explicit operator&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;FakeEnum&lt;/span&gt;(&lt;span class=&quot;keyword&quot;&gt;int&lt;/span&gt; value) {
        &lt;span class=&quot;keyword&quot;&gt;foreach&lt;/span&gt;(&lt;span class=&quot;class_def&quot;&gt;FieldInfo&lt;/span&gt; staticField &lt;span class=&quot;keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;typeof&lt;/span&gt;(FakeEnum).GetFields(&lt;span class=&quot;class_def&quot;&gt;BindingFlags&lt;/span&gt;.Public | &lt;span class=&quot;class_def&quot;&gt;BindingFlags&lt;/span&gt;.Static | &lt;span class=&quot;class_def&quot;&gt;BindingFlags&lt;/span&gt;.FlattenHierarchy)) {
            &lt;span class=&quot;class_def&quot;&gt;FakeEnum&lt;/span&gt; temp = staticField.GetValue(&lt;span class=&quot;keyword&quot;&gt;null&lt;/span&gt;) &lt;span class=&quot;keyword&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;FakeEnum&lt;/span&gt;;
            &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt;(&lt;span class=&quot;keyword&quot;&gt;null&lt;/span&gt; == temp) &lt;span class=&quot;keyword&quot;&gt;continue&lt;/span&gt;;
            &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt;(temp.value == value) &lt;span class=&quot;keyword&quot;&gt;return temp&lt;/span&gt;;
        }
        &lt;span class=&quot;keyword&quot;&gt;return null&lt;/span&gt;;
    }

    &lt;span class=&quot;keyword&quot;&gt;public static explicit operator&lt;/span&gt; int(&lt;span class=&quot;class_def&quot;&gt;FakeEnum&lt;/span&gt; fakeEnum) {
        &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; fakeEnum.value;
    }

    &lt;span class=&quot;keyword&quot;&gt;public static implicit operator&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;FakeEnum&lt;/span&gt;(&lt;span class=&quot;keyword&quot;&gt;string&lt;/span&gt; value) {
        &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt;(&lt;span class=&quot;keyword&quot;&gt;string&lt;/span&gt;.IsNullOrEmpty(value)) &lt;span class=&quot;keyword&quot;&gt;return null&lt;/span&gt;;
        &lt;span class=&quot;keyword&quot;&gt;foreach&lt;/span&gt;(&lt;span class=&quot;class_def&quot;&gt;FieldInfo&lt;/span&gt; field &lt;span class=&quot;keyword&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;typeof&lt;/span&gt;(FakeEnum).GetFields(&lt;span class=&quot;class_def&quot;&gt;BindingFlags&lt;/span&gt;.Public | &lt;span class=&quot;class_def&quot;&gt;BindingFlags&lt;/span&gt;.Static | &lt;span class=&quot;class_def&quot;&gt;BindingFlags&lt;/span&gt;.FlattenHierarchy)) {
            &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt;(&lt;span class=&quot;class_def&quot;&gt;StringComparer&lt;/span&gt;.InvariantCultureIgnoreCase.Compare(field.Name, value) == 0) {
                &lt;span class=&quot;class_def&quot;&gt;FakeEnum&lt;/span&gt; temp = field.GetValue(&lt;span class=&quot;keyword&quot;&gt;null&lt;/span&gt;) &lt;span class=&quot;keyword&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;FakeEnum&lt;/span&gt;;
                &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt;( &lt;span class=&quot;keyword&quot;&gt;null&lt;/span&gt; == temp) &lt;span class=&quot;keyword&quot;&gt;throw new&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;InvalidOperationException&lt;/span&gt;(&lt;span class=&quot;keyword&quot;&gt;string&lt;/span&gt;.Format(&lt;span class=&quot;string&quot;&gt;&quot;{0} not convertable to {1}&quot;&lt;/span&gt;, value, &lt;span class=&quot;keyword&quot;&gt;typeof&lt;/span&gt;(&lt;span class=&quot;class_def&quot;&gt;FakeEnum&lt;/span&gt;)));
                &lt;span class=&quot;class_def&quot;&gt;FakeEnum&lt;/span&gt; instance = &lt;span class=&quot;keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;FakeEnum&lt;/span&gt;(temp.value, temp.friendlyName);
                &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; instance;
            }
        }
        &lt;span class=&quot;keyword&quot;&gt;return null&lt;/span&gt;;
    }

    &lt;span class=&quot;keyword&quot;&gt;public static bool operator&lt;/span&gt; == (&lt;span class=&quot;class_def&quot;&gt;FakeEnum&lt;/span&gt; a, &lt;span class=&quot;class_def&quot;&gt;FakeEnum&lt;/span&gt; b) { 
        &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt;(&lt;span class=&quot;keyword&quot;&gt;object&lt;/span&gt;.ReferenceEquals(a, &lt;span class=&quot;keyword&quot;&gt;null&lt;/span&gt;)) return &lt;span class=&quot;keyword&quot;&gt;false&lt;/span&gt;;      
        &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; a.Equals(b);
    }

    &lt;span class=&quot;keyword&quot;&gt;public static bool operator&lt;/span&gt; != (&lt;span class=&quot;class_def&quot;&gt;FakeEnum&lt;/span&gt; a, &lt;span class=&quot;class_def&quot;&gt;FakeEnum&lt;/span&gt; b) { 
        &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt;(&lt;span class=&quot;keyword&quot;&gt;object&lt;/span&gt;.ReferenceEquals(a, &lt;span class=&quot;keyword&quot;&gt;null&lt;/span&gt;)) return &lt;span class=&quot;keyword&quot;&gt;true&lt;/span&gt;;
        &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; !a.Equals(b);
    }

    &lt;span class=&quot;keyword&quot;&gt;protected&lt;/span&gt; FakeEnum(&lt;span class=&quot;keyword&quot;&gt;int&lt;/span&gt; value) : &lt;span class=&quot;keyword&quot;&gt;this&lt;/span&gt;(value, &lt;span class=&quot;keyword&quot;&gt;null&lt;/span&gt;) { }

    &lt;span class=&quot;keyword&quot;&gt;protected&lt;/span&gt; FakeEnum(&lt;span class=&quot;keyword&quot;&gt;int&lt;/span&gt; value, &lt;span class=&quot;keyword&quot;&gt;string&lt;/span&gt; friendlyName) {
        &lt;span class=&quot;keyword&quot;&gt;this&lt;/span&gt;.value = value;
        &lt;span class=&quot;keyword&quot;&gt;this&lt;/span&gt;.friendlyName = friendlyName;
        &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt;(&lt;span class=&quot;keyword&quot;&gt;string&lt;/span&gt;.IsNullOrEmpty(friendlyName)) {
            &lt;span class=&quot;keyword&quot;&gt;this&lt;/span&gt;.friendlyName = ToString();
        }
    }

    &lt;span class=&quot;keyword&quot;&gt;public class&lt;/span&gt; FakeEnumConverter : &lt;span class=&quot;class_def&quot;&gt;TypeConverter&lt;/span&gt; {
        &lt;span class=&quot;keyword&quot;&gt;public override object&lt;/span&gt; ConvertFrom(&lt;span class=&quot;class_def&quot;&gt;ITypeDescriptorContext&lt;/span&gt; context, &lt;span class=&quot;class_def&quot;&gt;CultureInfo&lt;/span&gt; culture, &lt;span class=&quot;keyword&quot;&gt;object&lt;/span&gt; value) {
            &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt;(value &lt;span class=&quot;keyword&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;string&lt;/span&gt;) {
                &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; (&lt;span class=&quot;class_def&quot;&gt;FakeEnum&lt;/span&gt;)((&lt;span class=&quot;keyword&quot;&gt;string&lt;/span&gt;)value);
            }
            &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;base&lt;/span&gt;.ConvertFrom(context, culture, value);
        }
    }
}&lt;/pre&gt;
&lt;br /&gt;
Since we were generating these guys, we'd put the class implementation in one file, and our enum like declarations in another.  It made it look a bit more &amp;quot;enum&amp;quot; like.&lt;br /&gt;
&lt;br /&gt;
From there, you can use it just like a normal enum except that you now have a built in friendly name that you can access:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;code&quot;&gt;&lt;span class=&quot;keyword&quot;&gt;public void&lt;/span&gt; MyMethod(&lt;span class=&quot;class_def&quot;&gt;FakeEnum&lt;/span&gt; flag) {
    &lt;span class=&quot;keyword&quot;&gt;switch&lt;/span&gt;(flag) {
        &lt;span class=&quot;keyword&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;FakeEnum&lt;/span&gt;.One: {
            &lt;span class=&quot;class_def&quot;&gt;Console&lt;/span&gt;.WriteLine(flag.FriendlyName);
        }
    }
}&lt;/pre&gt;
Since additional partial classes would be added, I didn't want to have to provide some functionality of ToString or explicit conversion for each partial class, so I resolved the ability to get the numeric value, string member name value through reflection.  Since I had full control over the type, I decided it would be nice to add the FriendlyNameAttribute functionality into my fake enum, so I did.&lt;br /&gt;
&lt;br /&gt;
Its implementation uses .net reflection to iterate over its members and obtain the enum like member and its associated and values (since every member we use like an enum will be a static instance of itself) and additionally its friendly name.  actually we could make our fake enum non-numeric based (this would kind of defeat the ability to do a flag like operation) but that wasn't something needed at the time so we just stuck with int.&lt;br /&gt;
&lt;br /&gt;
To be able to use these objects like a bit field (like enums are), one would need to add the | and &amp; operators so that fields could be combined or extracted (perhaps if I'm bored I'll add this to a future version).</description>
      <pubDate>Wed, 04 Apr 2007 19:15:58 EDT</pubDate>
      <link>http://sanity-free.org/140/creating_fake_enums.html</link>
    </item>
        <item>
      <title>C# 3.0 Lambda expressions</title>
      <description>I've finally seen the light for these little buggers.  As I wrote in my &lt;a href=&quot;http://www.sanity-free.org/126/the_csharp_3_0_language_specification.html&quot; target=&quot;_blank&quot; title=&quot;The C# 3.0 Specification&quot;&gt;original post&lt;/a&gt; that I really didn't glean much from the description of lambda expressions that are mentioned in the specification, but I ran across a great article on it on the code project that did a really good job at explaining it.&lt;br /&gt;
&lt;br /&gt;
So my meta blog for the day is a link to that article:&lt;br /&gt;
&lt;br /&gt;
&amp;raquo; &lt;a href=&quot;http://www.codeproject.com/csharp/lambdaexpressions.asp&quot; target=&quot;_blank&quot; title=&quot;Lambda Expressions and Expression Trees: An Introduction&quot;&gt;http://www.codeproject.com/csharp/lambdaexpressions.asp&lt;/a&gt;</description>
      <pubDate>Tue, 13 Feb 2007 13:51:13 EST</pubDate>
      <link>http://sanity-free.org/138/csharp_3_0_lambda_expressions.html</link>
    </item>
        <item>
      <title>Phalanger, PHP for .NET</title>
      <description> I ran across an article on &lt;a href=&quot;http://www.codeproject.com/cpnet/phalanger-intro.asp&quot; title=&quot;Code Project&quot;&gt;the code project&lt;/a&gt; about a PHP compiler / language extension for .NET.&lt;br /&gt;
&lt;br /&gt;
PHP has an extension for .NET that allows you to use .NET resources in PHP code, but this allows you to use PHP from .NET, with support for native PHP API's as well as managed compilation.  This project may end up making my article on &lt;a href=&quot;/125/php_webservices_and_csharp_dotnet_soap_clients.html&quot; title=&quot;PHP Webservices and C# / .NET SOAP Clients&quot; target=&quot;_blank&quot;&gt;nusoap &amp; C#&lt;/a&gt; obsolete, though it should make writing web services in PHP as easy as it is in C#.&lt;br /&gt;
&lt;br /&gt;
&amp;raquo;&amp;nbsp;&lt;a href=&quot;http://www.php-compiler.net&quot; title=&quot;Phalanger Site&quot; target=&quot;_blank&quot;&gt;Phalanger Home&lt;/a&gt;&lt;br /&gt;
&amp;raquo;&amp;nbsp;&lt;a href=&quot;http://www.codeproject.com/cpnet/phalanger-intro.asp&quot; title=&quot;Phalanger, PHP for .NET: Introduction for .NET developers&quot; target=&quot;_blank&quot;&gt;Code Project Article&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;big_image&quot;&gt;&lt;img src=&quot;/images/vsnetscreen.gif&quot; alt=&quot;Screen Shot in Visual Studio&quot; /&gt;&lt;/div&gt;</description>
      <pubDate>Fri, 26 Jan 2007 12:57:22 EST</pubDate>
      <link>http://sanity-free.org/137/phalanger_php_for_dotnet.html</link>
    </item>
        <item>
      <title>.NET Reflection</title>
      <description>What is reflection?  Its the image we seen in a mirror.  In .NET however its a system that lets you do at runtime what you would normally do in a text editor or visual studio.&lt;br /&gt;
&lt;br /&gt;
Like most object oriented programming languages, .net has the concept of a &quot;type.&quot;  a Type defines what an object is.  Whether your using a primitive such as an integer, or a complex data type, each thing in .net has a type that corresponds to what it is.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;code&quot;&gt;&lt;span class=&quot;keyword&quot;&gt;public class&lt;/span&gt; Foo {}&lt;/pre&gt;

This class is of type Foo.  Now this class is basically pointless... Why? Well because it has no members.  Actually it does have members because everything inherits from System.Object, and System.Object has 12 member methods some have overloads, some are static, some are private some are protected others are public.  For the sake of simplicity, we wont discuss System.Object, but will stick only to Foo.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;code&quot;&gt;&lt;span class=&quot;keyword&quot;&gt;public class&lt;/span&gt; Foo {
    &lt;span class=&quot;keyword&quot;&gt;private int&lt;/span&gt; a;
}&lt;/pre&gt;

Typically we'd never have a class that looked like this.  After all, the complier would never let us access Foo::a because it is a private member.  What does this have to do with reflection you may ask yourself?  The rules of the compiler do not apply to reflection.&lt;br /&gt;
&lt;br /&gt;
To understand reflection, you need to understand what a class is, how encapsulation works (what access modifiers are), what a function is and what variables are.  We could go a little further, but thats a good starting point.&lt;br /&gt;
&lt;br /&gt;
Lets take our previous class declaration and spice it up a bit.  &lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;code&quot;&gt;&lt;span class=&quot;keyword&quot;&gt;public class&lt;/span&gt; Foo {
    &lt;span class=&quot;keyword&quot;&gt;private int&lt;/span&gt; a;

    &lt;span class=&quot;keyword&quot;&gt;public int&lt;/span&gt; A { 
        &lt;span class=&quot;keyword&quot;&gt;get&lt;/span&gt; { &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; a; }
        &lt;span class=&quot;keyword&quot;&gt;set&lt;/span&gt; { a = &lt;span class=&quot;keyword&quot;&gt;value&lt;/span&gt;; }
    }

    &lt;span class=&quot;keyword&quot;&gt;public bool&lt;/span&gt; Bar(&lt;span class=&quot;keyword&quot;&gt;string&lt;/span&gt; input) {
        &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; input == &lt;span class=&quot;string&quot;&gt;&quot;Baz&quot;&lt;/span&gt;;
    }

    &lt;span class=&quot;keyword&quot;&gt;protected void&lt;/span&gt; Bing() {
        &lt;span class=&quot;class_def&quot;&gt;Console&lt;/span&gt;.WriteLine(&lt;span class=&quot;string&quot;&gt;&quot;I'm not very creative&quot;&lt;/span&gt;);
    }    

    &lt;span class=&quot;keyword&quot;&gt;public&lt;/span&gt; Foo() { }
}&lt;/pre&gt;

I've given Foo a makeover.  It now has a private variable, a public property, a public and a protected method and a constructor.  I've given Foo a few &quot;members.&quot;  &lt;br /&gt;
&lt;br /&gt;
On the surface (which I wont scratch much) reflection allows you to access, modify or invoke members of an object.&lt;br /&gt;
&lt;br /&gt;
In our editor we can do this very easily:&lt;br /&gt;
&lt;br /&gt;&lt;!-- readmore --&gt;
&lt;pre class=&quot;code&quot;&gt;&lt;span class=&quot;class_def&quot;&gt;Foo&lt;/span&gt; f = &lt;span class=&quot;keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;Foo&lt;/span&gt;();
&lt;span class=&quot;keyword&quot;&gt;bool&lt;/span&gt; b = f.Bar(&lt;span class=&quot;string&quot;&gt;&quot;Baz&quot;&lt;/span&gt;);&lt;/pre&gt;

So lets look at what we're doing here.    &lt;br /&gt;
&lt;br /&gt;
Where does Foo come from?  &lt;br /&gt;
&lt;br /&gt;
At the top (or bottom, depending on your endianness) of the Reflection world is the Assembly (a dll or exe).  .NET assemblies are files that (can) contain namespaces and classes.  Each assembly contains a manifest (aka a list) of other assemblies that it references.  It also contains info about what classes it contains.&lt;br /&gt;
&lt;br /&gt;
When a .NET application starts up, the framework spiders out and loads each assembly in each referenced assembly's manifest until everything referenced is loaded into the current &lt;a href=&quot;http://msdn2.microsoft.com/en-us/library/system.appdomain.aspx&quot; target=&quot;_blank&quot; title=&quot;MSDN Documentation on System.AppDomain&quot;&gt;AppDomain&lt;/a&gt;.  If Foo happens to be in one of these dll's then everything is fine, but what if it isn't? Lets start there. The AppDomain object contains a list of all loaded &quot;Assemblies&quot; (if you cared to look at whats loaded).&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;code&quot;&gt;&lt;span class=&quot;class_def&quot;&gt;Assembly&lt;/span&gt;[] asms = &lt;span class=&quot;class_def&quot;&gt;AppDomain&lt;/span&gt;.CurrentDomain.GetAssemblies();&lt;/pre&gt;

if an assembly is not loaded, we can load it ourselves (unlike other technologies, once an assembly is loaded into the app domain, it cannot be unloaded until the app domain is unloaded).&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;code&quot;&gt;&lt;span class=&quot;class_def&quot;&gt;Assembly&lt;/span&gt; fooAsm = &lt;span class=&quot;class_def&quot;&gt;Assembly&lt;/span&gt;.LoadFile(&lt;span class=&quot;string&quot;&gt;@&quot;C:\My Path\foo.dll&quot;&lt;/span&gt;);&lt;/pre&gt;
The Assembly object represents the actual dll or exe file and all that is contained within it.  From it, we can get a list of types that the assembly contains:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;code&quot;&gt;&lt;span class=&quot;class_def&quot;&gt;Type&lt;/span&gt;[] types = fooAsm.GetTypes();&lt;/pre&gt;

or request a particular type:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;code&quot;&gt;&lt;span class=&quot;class_def&quot;&gt;Type&lt;/span&gt; fooType = fooAsm.GetType(&lt;span class=&quot;string&quot;&gt;&quot;Foo&quot;&lt;/span&gt;);&lt;/pre&gt;
 if fooAsm did not contain a class named &quot;Foo&quot; then GetType would return null.&lt;br /&gt;
&lt;br /&gt;
So, now we've loaded the dll / assembly that contained our class, and we've gotten a Type object that contains information about the class Foo.  From fooType, we can get a list of Foo's members:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;code&quot;&gt;&lt;span class=&quot;class_def&quot;&gt;MemberInfo&lt;/span&gt;[] memberInfo = fooType.GetMembers();&lt;/pre&gt;
 MemberInfo is the base class for other member specific things like fields, methods, properties and constructors.  MemberInfo has a property named &quot;Name&quot; which will match the name you give the member at the source code level (variable a, property A, method Bar, and constructor Foo).  The System.Type object can also be used to get specific types of members like fields, methods, constructors and properties.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;code&quot;&gt;&lt;span class=&quot;class_def&quot;&gt;MethodInfo&lt;/span&gt;[] methods = fooType.GetMethods();
&lt;span class=&quot;class_def&quot;&gt;PropertyInfo&lt;/span&gt;[] properties = fooType.GetProperties();
&lt;span class=&quot;class_def&quot;&gt;FieldInfo&lt;/span&gt;[] fields = fooType.GetFields();
&lt;span class=&quot;class_def&quot;&gt;ConstructorInfo&lt;/span&gt;[] constructors = fooType.GetConstructors();&lt;/pre&gt;
 These objects contains more member specific info, such as method parameters, and return type, whether the property has a get and set or just a getter.  Each of these Get...() methods also has an overload that takes a &lt;a href=&quot;http://msdn2.microsoft.com/en-us/library/system.reflection.bindingflags.aspx&quot; target=&quot;_blank&quot; title=&quot;MSDN Documentation for BindingFlags&quot;&gt;BindingFlags&lt;/a&gt; parameter.  BindingFlags are things that must be considered when looking for a member.  if we use the GetMember(field,property,method or constructor) that takes a string, it will look for a member who's name matches that.  Reflection uses more than just the name to find the member that you're requesting, the binding flags give additional info as to the members access modifier (public / non-public), whether it's an instance member or a static member, and so on.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;code&quot;&gt;&lt;span class=&quot;class_def&quot;&gt;FieldInfo&lt;/span&gt; aField = fooType.GetField(&lt;span class=&quot;string&quot;&gt;&quot;a&quot;&lt;/span&gt;, &lt;span class=&quot;class_def&quot;&gt;BindingFlags&lt;/span&gt;.NonPublic | &lt;span class=&quot;class_def&quot;&gt;BindingFlags&lt;/span&gt;.Instance);&lt;/pre&gt;

In the above code, I get a FieldInfo object which represents the private int variable &quot;a&quot; and I can do the same with the method Bar, the parameterless constructor and the property.&lt;br /&gt;
&lt;br /&gt;
Great right?  Who the heck cares?  What does this have to do with the price of tea?  Lets see...&lt;br /&gt;
&lt;br /&gt;
When we simply wrote the code:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;code&quot;&gt;&lt;span class=&quot;class_def&quot;&gt;Foo&lt;/span&gt; f = &lt;span class=&quot;keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;Foo&lt;/span&gt;();&lt;/pre&gt;
 the compiler had to locate the assembly that contains foo, load the foo type and create an instance.  so lets do the same, but using reflection.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;code&quot;&gt;&lt;span class=&quot;class_def&quot;&gt;Assembly&lt;/span&gt; fooAsm = &lt;span class=&quot;class_def&quot;&gt;Assembly&lt;/span&gt;.LoadFile(&lt;span class=&quot;string&quot;&gt;@&quot;C:\My Path\foo.dll&quot;&lt;/span&gt;);
&lt;span class=&quot;class_def&quot;&gt;Type&lt;/span&gt; fooType = fooAsm.GetType(&lt;span class=&quot;string&quot;&gt;&quot;Foo&quot;&lt;/span&gt;);
&lt;span class=&quot;keyword&quot;&gt;object&lt;/span&gt; foo = &lt;span class=&quot;class_def&quot;&gt;Activator&lt;/span&gt;.CreateInstance(fooType);&lt;/pre&gt;
Keep in mind that when we load the assembly at runtime, we cannot simply do:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;code&quot;&gt;&lt;span class=&quot;class_def&quot;&gt;Foo&lt;/span&gt; f = (&lt;span class=&quot;class_def&quot;&gt;Foo&lt;/span&gt;)&lt;span class=&quot;class_def&quot;&gt;Activator&lt;/span&gt;.CreateInstance(fooType);&lt;/pre&gt;
 unless we add a reference to foo.dll inside visual studio or in our command line compile statement.  If we did that, then we don't need to use reflection.  We simply need an instance of foo.  At the time we code it, we don't care what its type actually is.  We know we have an instance of it, and can invoke its methods, access its properties and set its members through reflection (using the MemberInfo, MethodInfo, PropertyInfo, FieldInfo's that we get from its Type object).  With reflection, the boundaries of encapsulation no longer apply (but should still be respected).  We can access internal objects, we can access and manipulate private class members, or invoke private methods. Back to the matter of creating the instance of foo... One thing to keep in mind about using Activator.CreateInstance is that it actually uses the objects constructor to create the instance that gets returned, so if you CreateInstance as I have above, make sure you have a parameterless constructor in your class.  Otherwise, you'll also have to pass in the constructor args as the second set of parameters after the System.Type in CreateInstance.&lt;br /&gt;
&lt;br /&gt;
now lets imagine something crazy that we want to do:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;code&quot;&gt;&lt;span class=&quot;class_def&quot;&gt;Foo&lt;/span&gt; foo = &lt;span class=&quot;keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;Foo&lt;/span&gt;();
foo.a = 32;&lt;/pre&gt;
 we could never write code that would do this unless it was inside a member (method or property) of Foo (a is a private member and therefore isnt accessible externally to Foo).&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;code&quot;&gt;&lt;span class=&quot;class_def&quot;&gt;Assembly&lt;/span&gt; fooAsm = &lt;span class=&quot;class_def&quot;&gt;Assembly&lt;/span&gt;.LoadFile(&lt;span class=&quot;string&quot;&gt;@&quot;C:\My Path\foo.dll&quot;&lt;/span&gt;);
&lt;span class=&quot;class_def&quot;&gt;Type&lt;/span&gt; fooType = fooAsm.GetType(&lt;span class=&quot;string&quot;&gt;&quot;Foo&quot;&lt;/span&gt;);
&lt;span class=&quot;keyword&quot;&gt;object&lt;/span&gt; foo = &lt;span class=&quot;class_def&quot;&gt;Activator&lt;/span&gt;.CreateInstance(fooType);
&lt;span class=&quot;class_def&quot;&gt;FieldInfo&lt;/span&gt; fooFieldA = fooType.GetField(&lt;span class=&quot;string&quot;&gt;&quot;a&quot;&lt;/span&gt;, &lt;span class=&quot;class_def&quot;&gt;BindingFlags&lt;/span&gt;.NonPublic | &lt;span class=&quot;class_def&quot;&gt;BindingFlags&lt;/span&gt;.Instance);
fooFieldA.SetValue(foo, 32);
&lt;span class=&quot;keyword&quot;&gt;int&lt;/span&gt; aVal = (&lt;span class=&quot;keyword&quot;&gt;int&lt;/span&gt;)fooFieldA.GetValue(foo);&lt;/pre&gt;
so in the above code, we loaded the foo.dll, got the type that we were looking for (Foo), created an instance of it (using Activator.CreateInstance), got the field that we wanted to set, and used the FieldInfo object to set that field in our instance.  then for good measures, we got the value that we just set using that field info object.&lt;br /&gt;
&lt;br /&gt;
Each of the derived objects of MemberInfo (MethodInfo, FieldInfo, PropertyInfo, ConstructorInfo, EventInfo) all use the same concepts.  First, you obtain an instance of the object you wish to reflect upon, then you obtain the XXXXInfo object that represents the member you want to manipulate, then you use that object to get, set, invoke or handle whatever your trying to do.  That necessitates having a valid instance (which you have) and thats pretty much the basics of using reflection.&lt;br /&gt;
&lt;br /&gt;
This has been an end to end application of the use of reflection.  It is not meant as a style guide in how you use reflection.  You may very well know everything about the type you're reflecting upon.  In that instance, you may just want to reflectively invoke a method, or set a property.  You may actually want to modify the look and feel or a Windows control which provides no public means of achieving what you wish to achieve.&lt;br /&gt;
&lt;br /&gt;
Here's a little parting sample using the reflection to access an modify various parts of Foo.
&lt;pre class=&quot;code&quot;&gt;&lt;span class=&quot;class_def&quot;&gt;Assembly&lt;/span&gt; fooAsm = &lt;span class=&quot;class_def&quot;&gt;Assembly&lt;/span&gt;.LoadFile(&lt;span class=&quot;string&quot;&gt;@&quot;C:\My Path\foo.dll&quot;&lt;/span&gt;);
&lt;span class=&quot;class_def&quot;&gt;Type&lt;/span&gt; fooType = fooAsm.GetType(&lt;span class=&quot;string&quot;&gt;&quot;Foo&quot;&lt;/span&gt;);
&lt;span class=&quot;keyword&quot;&gt;object&lt;/span&gt; foo = &lt;span class=&quot;class_def&quot;&gt;Activator&lt;/span&gt;.CreateInstance(fooType);
&lt;span class=&quot;class_def&quot;&gt;MethodInfo&lt;/span&gt; barMethod = fooType.GetMethod(&lt;span class=&quot;string&quot;&gt;&quot;Bar&quot;&lt;/span&gt;, &lt;span class=&quot;class_def&quot;&gt;BindingFlags&lt;/span&gt;.Public | &lt;span class=&quot;class_def&quot;&gt;BindingFlags&lt;/span&gt;.Instance);
&lt;span class=&quot;comment&quot;&gt;// invoke ( instance, method parameters );&lt;/span&gt;
&lt;span class=&quot;keyword&quot;&gt;bool&lt;/span&gt; isBar = barMethod.Invoke(foo, &lt;span class=&quot;keyword&quot;&gt;new object&lt;/span&gt;[] { &lt;span class=&quot;string&quot;&gt;&quot;Baz&quot;&lt;/span&gt; });

&lt;span class=&quot;class_def&quot;&gt;MethodInfo&lt;/span&gt; bingMethod = fooType.GetMethod(&lt;span class=&quot;string&quot;&gt;&quot;Bing&quot;&lt;/span&gt;, &lt;span class=&quot;class_def&quot;&gt;BindingFlags&lt;/span&gt;.NonPublic | &lt;span class=&quot;class_def&quot;&gt;BindingFlags&lt;/span&gt;.Instance);
bingMethod.Invoke(foo, &lt;span class=&quot;keyword&quot;&gt;null&lt;/span&gt;);

&lt;span class=&quot;class_def&quot;&gt;PropertyInfo&lt;/span&gt; propertyA = fooType.GetProperty(&lt;span class=&quot;string&quot;&gt;&quot;A&quot;&lt;/span&gt;, &lt;span class=&quot;class_def&quot;&gt;BindingFlags&lt;/span&gt;.Public | &lt;span class=&quot;class_def&quot;&gt;BindingFlags&lt;/span&gt;.Instance);
propertyA.SetValue(foo, 33);
&lt;span class=&quot;keyword&quot;&gt;int&lt;/span&gt; aVal = propertyA.GetValue(foo, &lt;span class=&quot;keyword&quot;&gt;null&lt;/span&gt;); &lt;span class=&quot;comment&quot;&gt;// second parameter is for if the property was in indexer&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;
All of which equates to the following lines of code:&lt;br /&gt;
&lt;pre class=&quot;code&quot;&gt;
&lt;span class=&quot;class_def&quot;&gt;Foo&lt;/span&gt; foo = &lt;span class=&quot;keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;Foo&lt;/span&gt;();
&lt;span class=&quot;keyword&quot;&gt;bool&lt;/span&gt; isBar = foo.Bar(&lt;span class=&quot;string&quot;&gt;&quot;Baz&quot;&lt;/span&gt;);
foo.Bing();
foo.A = 33;
&lt;span class=&quot;keyword&quot;&gt;int&lt;/span&gt; aVal = foo.A;&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
If you have any questions, comments, or felt that this article was missing any crucial information, please register (its free) in the &lt;a href=&quot;/forum/&quot;&gt;FORUMS&lt;/a&gt; and post your thoughts there.</description>
      <pubDate>Sat, 06 Jan 2007 03:29:49 EST</pubDate>
      <link>http://sanity-free.org/135/dotnet_reflection.html</link>
    </item>
        <item>
      <title>Standard CRC 16 in C#</title>
      <description>I&amp;#39;ve been working on a service at work that will end up being this big cluster of servers that all talk with each other.&amp;nbsp; 
One of the things I needed was a small crc checksum for some of the more compact UDP messages that get sent around.&amp;nbsp; 
I thought about just using the CRC16-CCITT library I already had, but decided on using the standard CRC16 algorithm.&amp;nbsp; 
Since I posted the CRC32 and CRC16-CCITT implementations I thought I&amp;#39;d post this one too.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;code&quot;&gt;&lt;span class=&quot;keyword&quot;&gt;using&lt;/span&gt; System;

&lt;span class=&quot;keyword&quot;&gt;public class&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;Crc16 &lt;/span&gt;{
    &lt;span class=&quot;keyword&quot;&gt;const ushort &lt;/span&gt;polynomial = 0xA001;
    &lt;span class=&quot;keyword&quot;&gt;ushort&lt;/span&gt;[] table = &lt;span class=&quot;keyword&quot;&gt;new ushort&lt;/span&gt;[256];

    &lt;span class=&quot;keyword&quot;&gt;public ushort&lt;/span&gt; ComputeChecksum(&lt;span class=&quot;keyword&quot;&gt;byte&lt;/span&gt;[] bytes) {
        &lt;span class=&quot;keyword&quot;&gt;ushort &lt;/span&gt;crc = 0;
        &lt;span class=&quot;keyword&quot;&gt;for&lt;/span&gt;(&lt;span class=&quot;keyword&quot;&gt;int&lt;/span&gt; i = 0; i &amp;lt; bytes.Length; ++i) {
            &lt;span class=&quot;keyword&quot;&gt;byte&lt;/span&gt; index = (&lt;span class=&quot;keyword&quot;&gt;byte&lt;/span&gt;)(crc ^ bytes[i]);
            crc = (&lt;span class=&quot;keyword&quot;&gt;ushort&lt;/span&gt;)((crc &amp;gt;&amp;gt; 8) ^ table[index]);
        }
        &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; crc;
    }

    &lt;span class=&quot;keyword&quot;&gt;public byte&lt;/span&gt;[] ComputeChecksumBytes(&lt;span class=&quot;keyword&quot;&gt;byte&lt;/span&gt;[] bytes) {
        &lt;span class=&quot;keyword&quot;&gt;ushort&lt;/span&gt; crc = ComputeChecksum(bytes);
        &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;BitConverter&lt;/span&gt;.GetBytes(crc);
    }

    &lt;span class=&quot;keyword&quot;&gt;public&lt;/span&gt; Crc16() {
        &lt;span class=&quot;keyword&quot;&gt;ushort&lt;/span&gt; value;
        &lt;span class=&quot;keyword&quot;&gt;ushort&lt;/span&gt; temp;
        &lt;span class=&quot;keyword&quot;&gt;for&lt;/span&gt;(&lt;span class=&quot;keyword&quot;&gt;ushort&lt;/span&gt; i = 0; i &amp;lt; table.Length; ++i) {
            value = 0;
            temp = i;
            &lt;span class=&quot;keyword&quot;&gt;for&lt;/span&gt;(&lt;span class=&quot;keyword&quot;&gt;byte&lt;/span&gt; j = 0; j &amp;lt; 8; ++j) {
                &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt;(((value ^ temp) &amp;amp; 0x0001) != 0) {
                    value = (&lt;span class=&quot;keyword&quot;&gt;ushort&lt;/span&gt;)((value &amp;gt;&amp;gt; 1) ^ polynomial);
                }&lt;span class=&quot;keyword&quot;&gt;else&lt;/span&gt; {
                    value &amp;gt;&amp;gt;= 1;
                }
                temp &amp;gt;&amp;gt;= 1;
            }
            table[i] = value;
        }
    }
}&lt;/pre&gt;
If you need for this code to be CLS compliant, you can change the method signature's return type from ushort to int and it will operate the same (the ushort crc value will be implicitly converted from ushort to int)
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Links to the other C# CRC implementations&lt;/b&gt; &lt;a href=&quot;/133/crc_16_ccitt_in_csharp.html&quot;&gt;CRC16-CCITT&lt;/a&gt;, &lt;a href=&quot;/12/crc32_implementation_in_csharp.html&quot;&gt;CRC32&lt;/a&gt;, and &lt;a href='/146/crc8_implementation_in_csharp.html'&gt;CRC8&lt;/a&gt;</description>
      <pubDate>Fri, 15 Dec 2006 17:23:45 EST</pubDate>
      <link>http://sanity-free.org/134/standard_crc_16_in_csharp.html</link>
    </item>
        <item>
      <title>CRC 16 CCITT in C#</title>
      <description>Once again I found myself needing another type of CRC algorithm in C#.  I found one on code project, but their implementation  of CRC 16 CCITT didn&amp;#39;t produce that checksum I needed.  Come to find out there are different methods to calculate CRC 16 CCITT  which use different initial values for the crc.&lt;br /&gt; &lt;br /&gt; I ended up writing this one for my own purposes: &lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;code&quot;&gt;&lt;span class=&quot;keyword&quot;&gt;using&lt;/span&gt; System;

&lt;span class=&quot;keyword&quot;&gt;public enum&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;InitialCrcValue&lt;/span&gt; { Zeros, NonZero1 = 0xffff, NonZero2 = 0x1D0F }

&lt;span class=&quot;keyword&quot;&gt;public class&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;Crc16Ccitt&lt;/span&gt; {
    &lt;span class=&quot;keyword&quot;&gt;const ushort&lt;/span&gt; poly = 4129;
    &lt;span class=&quot;keyword&quot;&gt;ushort&lt;/span&gt;[] table = &lt;span class=&quot;keyword&quot;&gt;new ushort&lt;/span&gt;[256];
    &lt;span class=&quot;keyword&quot;&gt;ushort&lt;/span&gt; initialValue = 0;

    &lt;span class=&quot;keyword&quot;&gt;public ushort&lt;/span&gt; ComputeChecksum(byte[] bytes) {
        &lt;span class=&quot;keyword&quot;&gt;ushort&lt;/span&gt; crc = &lt;span class=&quot;keyword&quot;&gt;this&lt;/span&gt;.initialValue;
        &lt;span class=&quot;keyword&quot;&gt;for&lt;/span&gt;(&lt;span class=&quot;keyword&quot;&gt;int&lt;/span&gt; i = 0; i &amp;lt; bytes.Length; ++i) {
            crc = (&lt;span class=&quot;keyword&quot;&gt;ushort&lt;/span&gt;)((crc &amp;lt;&amp;lt; 8) ^ table[((crc &amp;gt;&amp;gt; 8) ^ (0xff &amp;amp; bytes[i]))]);
        }
        &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; crc;
    }

    &lt;span class=&quot;keyword&quot;&gt;public byte&lt;/span&gt;[] ComputeChecksumBytes(&lt;span class=&quot;keyword&quot;&gt;byte&lt;/span&gt;[] bytes) {
        &lt;span class=&quot;keyword&quot;&gt;ushort&lt;/span&gt; crc = ComputeChecksum(bytes);
        &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;BitConverter&lt;/span&gt;.GetBytes(crc);
    }

    &lt;span class=&quot;keyword&quot;&gt;public&lt;/span&gt; Crc16Ccitt(&lt;span class=&quot;class_def&quot;&gt;InitialCrcValue&lt;/span&gt; initialValue) {
        &lt;span class=&quot;keyword&quot;&gt;this&lt;/span&gt;.initialValue = (&lt;span class=&quot;keyword&quot;&gt;ushort&lt;/span&gt;)initialValue;
        &lt;span class=&quot;keyword&quot;&gt;ushort&lt;/span&gt; temp, a;
        &lt;span class=&quot;keyword&quot;&gt;for&lt;/span&gt;(&lt;span class=&quot;keyword&quot;&gt;int&lt;/span&gt; i = 0; i &amp;lt; table.Length; ++i) {
            temp = 0;
            a = (&lt;span class=&quot;keyword&quot;&gt;ushort&lt;/span&gt;)(i &amp;lt;&amp;lt; 8);
            &lt;span class=&quot;keyword&quot;&gt;for&lt;/span&gt;(&lt;span class=&quot;keyword&quot;&gt;int&lt;/span&gt; j = 0; j &amp;lt; 8; ++j) {
                &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt;(((temp ^ a) &amp;amp; 0x8000) != 0) {
                    temp = (&lt;span class=&quot;keyword&quot;&gt;ushort&lt;/span&gt;)((temp &amp;lt;&amp;lt; 1) ^ poly);
                } &lt;span class=&quot;keyword&quot;&gt;else&lt;/span&gt; {
                    temp &amp;lt;&amp;lt;= 1;
                }
                a &amp;lt;&amp;lt;= 1;
            }
            table[i] = temp;
        }
    }
}&lt;/pre&gt;
&lt;br /&gt; 
&lt;br /&gt;
This was used for testing during the time we were looking at the Battlefield 2142 auth token, and were trying to figure out what the last 2 bytes of that token were made of.&amp;nbsp; Battlefield 2142&amp;#39;s auth token uses the CRC 16 CCITT with the initial value of 0 (new Crc16Ccitt(InitialCrcValue.Zeros) in the above class.&amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
If you need for this code to be CLS compliant, you can change the method signature's return type from ushort to int and it will operate the same (the ushort crc value will be implicitly converted from ushort to int)&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Links to the other C# CRC implementations&lt;/b&gt; &lt;a href=&quot;/12/crc32_implementation_in_csharp.html&quot;&gt;CRC32&lt;/a&gt;, &lt;a href=&quot;/134/standard_crc_16_in_csharp.html&quot;&gt;CRC16&lt;/a&gt;, and &lt;a href='/146/crc8_implementation_in_csharp.html'&gt;CRC8&lt;/a&gt;</description>
      <pubDate>Wed, 08 Nov 2006 03:32:03 EST</pubDate>
      <link>http://sanity-free.org/133/crc_16_ccitt_in_csharp.html</link>
    </item>
        <item>
      <title>A Generic Singleton Pattern in C#</title>
      <description>I don't know why its never occured to me to implement the singleton pattern as a generic type before.  
I've always just created the singleton pattern as part of my class (not that we're talking about a lot 
of coding here), but it occured to me that a singleton could be a lot easier to implement and spot if I
used a generic type.&lt;br/&gt;
&lt;br/&gt;
Here is my generic Singleton class:
&lt;pre class=&quot;code&quot;&gt;&lt;span class=&quot;keyword&quot;&gt;public static class&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;Singleton&lt;/span&gt;&amp;lt;T&amp;gt; &lt;span class=&quot;keyword&quot;&gt;where&lt;/span&gt; T : &lt;span class=&quot;keyword&quot;&gt;new&lt;/span&gt;()
{
    &lt;span class=&quot;keyword&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;Mutex&lt;/span&gt; mutex = &lt;span class=&quot;keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;Mutex&lt;/span&gt;();
    &lt;span class=&quot;keyword&quot;&gt;static&lt;/span&gt; T instance;
    &lt;span class=&quot;keyword&quot;&gt;public static&lt;/span&gt; T Instance
    {
        &lt;span class=&quot;keyword&quot;&gt;get&lt;/span&gt;
        {
            mutex.WaitOne();
            &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt;(instance == &lt;span class=&quot;keyword&quot;&gt;null&lt;/span&gt;)
            {
                instance = &lt;span class=&quot;keyword&quot;&gt;new&lt;/span&gt; T();
            }
            mutex.ReleaseMutex();
            &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; instance;
        }
    }
}&lt;/pre&gt;&lt;br/&gt;
&lt;br/&gt;
Now, instead of implementing this same thing in every single class you want to act as a singleton, you 
simply use the generic definition: Foo.Instance.Whatever turns into Singleton&amp;lt;Foo&amp;gt;.Instance.Whatever.&lt;br/&gt;
&lt;br/&gt;
Now, this doesnt keep you from creating non singleton objects of whatever type you have (there may still 
be some clever way to do that, but I'm not familiar with it), but it does save you the time of implementing 
the singleton pattern on singleton objects.</description>
      <pubDate>Wed, 04 Oct 2006 14:51:56 EDT</pubDate>
      <link>http://sanity-free.org/132/generic_singleton_pattern_in_csharp.html</link>
    </item>
        <item>
      <title>Triple DES between PHP and C#</title>
      <description>			The past while at work I've been working through some annoyingly overcomplicated encryption
			issues.  The problem is not that Triple DES is all that complicated or annoying, it's just that
			when you have 2 different technologies (one doing the encrypting and one doing the decrypting)
			at work; it can be frustrating to get anything accomplished.&lt;br /&gt;
			&lt;br /&gt;
			Our issues stemmed around the fact that we were using .NET 1.1 and they were using Java (with
			the standard crypto providers).  There are a few subtleties between Microsoft's crypto providers
			and Java's.  .NET provides 3 (ok, really only 2) Padding modes, and Java provides like 5, but 
			they don't provide any in common.  One easy padding mode is to append 0x00 bytes to the end of 
			the final block to make it an even 64 bits wide.  The Java provider didn't have this, but its
			easy enough to add ( by appending null char's '\0' to the end of the string then calling 
			getBytes() ). So that's the route we took.  After overcoming a few problems with &quot;How do we
			encode our bytes used for the key and IV and still be compatible&quot; we were off and running.&lt;br /&gt;
			&lt;br /&gt;
			That made me wonder how compatible PHP and .NET's 3DES were.  Since I didn't see any &quot;Padding
			Mode&quot; for PHP's, I simply hand coded the padding the same way we did with the Java code.&lt;br /&gt;
			&lt;br /&gt;
			Here's my php script:&lt;!-- readmore --&gt;
			&lt;pre class=&quot;code&quot;&gt;&lt;span class=&quot;php_red&quot;&gt;&amp;lt;?php&lt;/span&gt;

&lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt;(!&lt;span class=&quot;keyword&quot;&gt;isset&lt;/span&gt;(&lt;span class=&quot;php_var&quot;&gt;$_POST&lt;/span&gt;[&lt;span class=&quot;php_string&quot;&gt;'op'&lt;/span&gt;])) {
    &lt;span class=&quot;php_red&quot;&gt;?&amp;gt;&lt;/span&gt;
&amp;lt;form id=&lt;span class=&quot;php_string&quot;&gt;&quot;form1&quot;&lt;/span&gt; name=&lt;span class=&quot;php_string&quot;&gt;&quot;form1&quot;&lt;/span&gt; method=&lt;span class=&quot;php_string&quot;&gt;&quot;post&quot;&lt;/span&gt; action=&lt;span class=&quot;php_string&quot;&gt;&quot;&quot;&lt;/span&gt;&amp;gt;
  enter text
  &amp;lt;input name=&lt;span class=&quot;php_string&quot;&gt;&quot;data&quot;&lt;/span&gt; type=&lt;span class=&quot;php_string&quot;&gt;&quot;text&quot;&lt;/span&gt; /&amp;gt;
  &amp;lt;input type=&lt;span class=&quot;php_string&quot;&gt;&quot;hidden&quot;&lt;/span&gt; value=&lt;span class=&quot;php_string&quot;&gt;&quot;op&quot;&lt;/span&gt; name=&lt;span class=&quot;php_string&quot;&gt;&quot;op&quot;&lt;/span&gt; /&amp;gt;
  &amp;lt;input type=&lt;span class=&quot;php_string&quot;&gt;&quot;submit&quot;&lt;/span&gt; name=&lt;span class=&quot;php_string&quot;&gt;&quot;Submit&quot;&lt;/span&gt; value=&lt;span class=&quot;php_string&quot;&gt;&quot;Submit&quot;&lt;/span&gt; /&amp;gt;
&amp;lt;/form&amp;gt;
    &lt;span class=&quot;php_red&quot;&gt;&amp;lt;?php&lt;/span&gt;
}&lt;span class=&quot;keyword&quot;&gt;else&lt;/span&gt; {
    &lt;span class=&quot;php_var&quot;&gt;$buffer&lt;/span&gt; = &lt;span class=&quot;php_var&quot;&gt;$_POST&lt;/span&gt;[&lt;span class=&quot;php_string&quot;&gt;'data'&lt;/span&gt;]; 
    &lt;span class=&quot;php_comments&quot;&gt;// get the amount of bytes to pad&lt;/span&gt;
    &lt;span class=&quot;php_var&quot;&gt;$extra&lt;/span&gt; = 8 - (&lt;span class=&quot;keyword&quot;&gt;strlen&lt;/span&gt;(&lt;span class=&quot;php_var&quot;&gt;$buffer&lt;/span&gt;) % 8);
    &lt;span class=&quot;php_comments&quot;&gt;// add the zero padding&lt;/span&gt;
    &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt;(&lt;span class=&quot;php_var&quot;&gt;$extra&lt;/span&gt; &amp;gt; 0) {
        &lt;span class=&quot;keyword&quot;&gt;for&lt;/span&gt;(&lt;span class=&quot;php_var&quot;&gt;$i&lt;/span&gt; = 0; &lt;span class=&quot;php_var&quot;&gt;$i&lt;/span&gt; &amp;lt; &lt;span class=&quot;php_var&quot;&gt;$extra&lt;/span&gt;; &lt;span class=&quot;php_var&quot;&gt;$i&lt;/span&gt;++) {
            &lt;span class=&quot;php_var&quot;&gt;$buffer&lt;/span&gt; .= &lt;span class=&quot;php_string&quot;&gt;&quot;\0&quot;&lt;/span&gt;;
        }
    }
    &lt;span class=&quot;php_comments&quot;&gt;// very simple ASCII key and IV&lt;/span&gt;
    &lt;span class=&quot;php_var&quot;&gt;$key&lt;/span&gt; = &lt;span class=&quot;php_string&quot;&gt;&quot;passwordDR0wSS@P6660juht&quot;&lt;/span&gt;;
    &lt;span class=&quot;php_var&quot;&gt;$iv&lt;/span&gt; = &lt;span class=&quot;php_string&quot;&gt;&quot;password&quot;&lt;/span&gt;;
    &lt;span class=&quot;php_comments&quot;&gt;// hex encode the return value&lt;/span&gt;
    &lt;span class=&quot;keyword&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;php_string&quot;&gt;&quot;Result: &quot;&lt;/span&gt;.&lt;span class=&quot;keyword&quot;&gt;bin2hex&lt;/span&gt;(&lt;span class=&quot;keyword&quot;&gt;mcrypt_cbc&lt;/span&gt;(MCRYPT_3DES, &lt;span class=&quot;php_var&quot;&gt;$key&lt;/span&gt;, &lt;span class=&quot;php_var&quot;&gt;$buffer&lt;/span&gt;, MCRYPT_ENCRYPT, &lt;span class=&quot;php_var&quot;&gt;$iv&lt;/span&gt;));	
}
&lt;span class=&quot;php_red&quot;&gt;?&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;
			&lt;br /&gt;
			&lt;img src=&quot;/images/html_input.gif&quot; border=&quot;0&quot; alt=&quot;Entering 'Test' into the textbox&quot;/&gt;&lt;br /&gt;
			&lt;br /&gt;
			Then entered in my text (in this case I used &quot;Test&quot; in both)&lt;br /&gt;
			&lt;br /&gt;
			Then I wrote up a sample in .NET to try to see if we had to massage the data or not:
			&lt;pre class=&quot;code&quot;&gt;&lt;span class=&quot;keyword&quot;&gt;using&lt;/span&gt; System;
&lt;span class=&quot;keyword&quot;&gt;using&lt;/span&gt; System.Security.Cryptography;
&lt;span class=&quot;keyword&quot;&gt;using&lt;/span&gt; System.Text;
&lt;span class=&quot;keyword&quot;&gt;using&lt;/span&gt; System.IO;

&lt;span class=&quot;keyword&quot;&gt;namespace&lt;/span&gt; TestBed {
    &lt;span class=&quot;keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;Program&lt;/span&gt; {
        &lt;span class=&quot;keyword&quot;&gt;static void&lt;/span&gt; Main(&lt;span class=&quot;keyword&quot;&gt;string&lt;/span&gt;[] args) {
            &lt;span class=&quot;keyword&quot;&gt;byte&lt;/span&gt;[] key = &lt;span class=&quot;class_def&quot;&gt;Encoding&lt;/span&gt;.ASCII.GetBytes(&lt;span class=&quot;string&quot;&gt;&quot;passwordDR0wSS@P6660juht&quot;&lt;/span&gt;);
            &lt;span class=&quot;keyword&quot;&gt;byte&lt;/span&gt;[] iv = &lt;span class=&quot;class_def&quot;&gt;Encoding&lt;/span&gt;.ASCII.GetBytes(&lt;span class=&quot;string&quot;&gt;&quot;password&quot;&lt;/span&gt;);
            &lt;span class=&quot;keyword&quot;&gt;byte&lt;/span&gt;[] data = &lt;span class=&quot;class_def&quot;&gt;Encoding&lt;/span&gt;.ASCII.GetBytes(&lt;span class=&quot;string&quot;&gt;&quot;Test&quot;&lt;/span&gt;);
            &lt;span class=&quot;keyword&quot;&gt;byte&lt;/span&gt;[] enc = &lt;span class=&quot;keyword&quot;&gt;new byte&lt;/span&gt;[0];
            &lt;span class=&quot;class_def&quot;&gt;TripleDES&lt;/span&gt; tdes = &lt;span class=&quot;class_def&quot;&gt;TripleDES&lt;/span&gt;.Create();
            tdes.IV = iv;
            tdes.Key = key;
            tdes.Mode = &lt;span class=&quot;class_def&quot;&gt;CipherMode&lt;/span&gt;.CBC;
            tdes.Padding = &lt;span class=&quot;class_def&quot;&gt;PaddingMode&lt;/span&gt;.Zeros;
            &lt;span class=&quot;class_def&quot;&gt;ICryptoTransform&lt;/span&gt; ict = tdes.CreateEncryptor();
            enc = ict.TransformFinalBlock(data, 0, data.Length);
            &lt;span class=&quot;class_def&quot;&gt;Console&lt;/span&gt;.WriteLine(Bin2Hex(enc));
            &lt;span class=&quot;class_def&quot;&gt;Console&lt;/span&gt;.ReadLine();
        }
        &lt;span class=&quot;comment&quot;&gt;// my bin2hex implementation&lt;/span&gt;		
        &lt;span class=&quot;keyword&quot;&gt;static string&lt;/span&gt; Bin2Hex(&lt;span class=&quot;keyword&quot;&gt;byte&lt;/span&gt;[] bin) {
            &lt;span class=&quot;class_def&quot;&gt;StringBuilder&lt;/span&gt; sb = &lt;span class=&quot;keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;StringBuilder&lt;/span&gt;(bin.Length * 2);
            &lt;span class=&quot;keyword&quot;&gt;foreach&lt;/span&gt;(&lt;span class=&quot;keyword&quot;&gt;byte&lt;/span&gt; b &lt;span class=&quot;keyword&quot;&gt;in&lt;/span&gt; bin) {
                sb.Append(b.ToString(&lt;span class=&quot;string&quot;&gt;&quot;x&quot;&lt;/span&gt;).PadLeft(2, '0'));
            }
            &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; sb.ToString();
        }
    }
}&lt;/pre&gt;		
			&amp;quot;47794945c0230c3d&amp;quot; Wouldn't you know it! It worked like a charm!&lt;br /&gt;
			&lt;br /&gt;
			Would have been nice if the Java -&amp;gt; .NET solution had been that easy.</description>
      <pubDate>Thu, 28 Sep 2006 04:17:32 EDT</pubDate>
      <link>http://sanity-free.org/131/triple_des_between_php_and_csharp.html</link>
    </item>
        <item>
      <title>A Command Line style string parser (in C#)</title>
      <description>Last week I was writing a service that could be run as a console app.  I needed to be 
able to parse a single string into an array like the shell does for command line apps, 
and the service does for start parameters.  I did a little googling, but found only
command line arg classes that put command line args into associative array's for
easy indexing, which isn't what I was looking for.&lt;br /&gt;
&lt;br /&gt;
The class had basically 2 requirements:
&lt;ol&gt;
&lt;li&gt;It had to be able to split the string on spaces (simple right?)&lt;/li&gt;
&lt;li&gt;It had to escape spaces that were quoted (as well as quotes that were escaped).&lt;/li&gt;
&lt;/ol&gt;
First I tried to iterate through the string a character at a time, looking for spaces
keeping the state of whether we were inside a quote or not, whether the current character
was an escaped quote, and so on.&lt;br /&gt;
&lt;br /&gt;
That was a major pain, so I stepped back and rethought my algorithm.  this is what I ended
up with, which was much simplier, and did exactly what I wanted without a lot of fuss.&lt;br /&gt;
&lt;br /&gt;
So here's my command line parser:&lt;br /&gt;

&lt;pre class=&quot;code&quot;&gt;&lt;span class=&quot;keyword&quot;&gt;public class&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;CommandLineParser&lt;/span&gt; {
    CommandLineParser() { }
    &lt;span class=&quot;keyword&quot;&gt;public static string&lt;/span&gt;[] Parse(&lt;span class=&quot;keyword&quot;&gt;string&lt;/span&gt; str) {        
        &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt;(&lt;span class=&quot;keyword&quot;&gt;str&lt;/span&gt; == &lt;span class=&quot;keyword&quot;&gt;null&lt;/span&gt; || !(str.Length &amp;gt; 0)) &lt;span class=&quot;keyword&quot;&gt;return new string&lt;/span&gt;[0];
        &lt;span class=&quot;keyword&quot;&gt;int&lt;/span&gt; idx = str.Trim().IndexOf(&lt;span class=&quot;string&quot;&gt;&quot; &quot;&lt;/span&gt;); 
        &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt;(idx == -1) &lt;span class=&quot;keyword&quot;&gt;return new string&lt;/span&gt;[] { str };
        &lt;span class=&quot;keyword&quot;&gt;int&lt;/span&gt; count = str.Length;
        &lt;span class=&quot;class_def&quot;&gt;ArrayList&lt;/span&gt; list = new &lt;span class=&quot;class_def&quot;&gt;ArrayList&lt;/span&gt;(); 
        &lt;span class=&quot;keyword&quot;&gt;while&lt;/span&gt;(count &amp;gt; 0) {
            &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt;(str[0] == &lt;span class=&quot;string&quot;&gt;'&quot;'&lt;/span&gt;) {
                &lt;span class=&quot;keyword&quot;&gt;int&lt;/span&gt; temp = str.IndexOf(&lt;span class=&quot;string&quot;&gt;&quot;\&quot;&quot;&lt;/span&gt;, 1, str.Length - 1);
                &lt;span class=&quot;keyword&quot;&gt;while&lt;/span&gt;(str[temp - 1] == &lt;span class=&quot;string&quot;&gt;'\\'&lt;/span&gt;) {
                    temp = str.IndexOf(&lt;span class=&quot;string&quot;&gt;&quot;\&quot;&quot;&lt;/span&gt;, temp + 1, str.Length - temp - 1);
                }
                idx = temp+1;
            }
            &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt;(str[0] == &lt;span class=&quot;string&quot;&gt;'\''&lt;/span&gt;) {   
                &lt;span class=&quot;keyword&quot;&gt;int&lt;/span&gt; temp = str.IndexOf(&lt;span class=&quot;string&quot;&gt;&quot;\'&quot;&lt;/span&gt;, 1, str.Length - 1);
                &lt;span class=&quot;keyword&quot;&gt;while&lt;/span&gt;(str[temp - 1] == &lt;span class=&quot;string&quot;&gt;'\\'&lt;/span&gt;) {
                    temp = str.IndexOf(&lt;span class=&quot;string&quot;&gt;&quot;\'&quot;&lt;/span&gt;, temp + 1, str.Length - temp - 1);
                }
                idx = temp+1;
            }
            &lt;span class=&quot;keyword&quot;&gt;string&lt;/span&gt; s = str.Substring(0, idx);
            &lt;span class=&quot;keyword&quot;&gt;int&lt;/span&gt; left = count - idx;
            str = str.Substring(idx, left).Trim();
            list.Add(s.Trim(&lt;span class=&quot;string&quot;&gt;'&quot;'&lt;/span&gt;));
            count = str.Length;
            idx = str.IndexOf(&lt;span class=&quot;string&quot;&gt;&quot; &quot;&lt;/span&gt;);
            &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt;(idx == -1) {
                string add = str.Trim(&lt;span class=&quot;string&quot;&gt;'&quot;'&lt;/span&gt;, &lt;span class=&quot;string&quot;&gt;' '&lt;/span&gt;);
                &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt;(add.Length &gt; 0) {
                    list.Add(add);
                }
                &lt;span class=&quot;keyword&quot;&gt;break&lt;/span&gt;;
            }
        }
        &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; (&lt;span class=&quot;keyword&quot;&gt;string&lt;/span&gt;[])list.ToArray(&lt;span class=&quot;keyword&quot;&gt;typeof&lt;/span&gt;(&lt;span class=&quot;keyword&quot;&gt;string&lt;/span&gt;));
    }
}&lt;/pre&gt;</description>
      <pubDate>Tue, 08 Aug 2006 17:02:40 EDT</pubDate>
      <link>http://sanity-free.org/128/command_line_style_string_parser_in_csharp.html</link>
    </item>
        <item>
      <title>An IPAddress Control (the Win32 SysIPAddress32 control in C#)</title>
      <description>The Common Controls library from the Platform SDK provides a control that Microsoft failed to provide a .NET counterpart to: the IPAddress Control (known as SysIPAddress32).&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;big_image&quot;&gt;&lt;img src=&quot;/images/tcpip_properties.gif&quot; border=&quot;0&quot; alt=&quot;The TCP/IP Properties dialog&quot; /&gt;&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
Quite a while ago I wrote a C# / .NET control that wrapped the functionality of the SysIPAddress32 control from comctl32.dll&lt;br /&gt;
&lt;br /&gt;
&lt;img src=&quot;/images/csharp_ipaddress_ctrl.gif&quot; border=&quot;0&quot; alt=&quot;My IPAddress Control Implementation&quot; /&gt;&lt;br /&gt;
&lt;br /&gt;
I had a much better implementation back then (with quite a few nice things included), but the hard drive that it lived on has since died, and I just haven't wanted to spend the time putting all that stuff back into the control.&lt;br /&gt;
&lt;br /&gt;
Some time after having written the &amp;quot;nice&amp;quot; version of this control, I posted the following code to another website.  Now, it's all I have left of the original.  There is a known problem with the handle to the font being disposed incorrectly, but the code that fixed that, was given to me by jfo from Microsoft, and I did a cut/paste of the solution without really paying much attention to it (kind of regretting that now...).  You may notice that all &quot;system&quot; fonts will always be displayed in BOLD in the forms designer (even when the font is set to something else).  THIS GOES AWAY AT RUNTIME, so don't worry! It's only a problem in the designer.&lt;br /&gt;
&lt;br /&gt;
Anyway, here it is, my IPAddress Control:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;code&quot;&gt;&lt;span class=&quot;comment&quot;&gt;/******************************************************/
/*          NULLFX FREE SOFTWARE LICENSE              */
/******************************************************/
/*  IPAddressControl                                  */
/*  by: Steve Whitley                                 */
/*  &amp;copy; 2004 NullFX Software                            */
/*                                                    */
/* NULLFX SOFTWARE DISCLAIMS ALL WARRANTIES,          */
/* RESPONSIBILITIES, AND LIABILITIES ASSOCIATED WITH  */
/* USE OF THIS CODE IN ANY WAY, SHAPE, OR FORM        */
/* REGARDLESS HOW IMPLICIT, EXPLICIT, OR OBSCURE IT   */
/* IS. IF THERE IS ANYTHING QUESTIONABLE WITH REGARDS */
/* TO THIS SOFTWARE BREAKING AND YOU GAIN A LOSS OF   */
/* ANY NATURE, WE ARE NOT THE RESPONSIBLE PARTY. USE  */
/* OF THIS SOFTWARE CREATES ACCEPTANCE OF THESE TERMS */
/*                                                    */
/* USE OF THIS CODE MUST RETAIN ALL COPYRIGHT NOTICES */
/* AND LICENSES (MEANING THIS TEXT).                  */
/*                                                    */
/******************************************************/&lt;/span&gt;


&lt;span class=&quot;keyword&quot;&gt;namespace&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;NullFX.Controls&lt;/span&gt; {
    &lt;span class=&quot;keyword&quot;&gt;using&lt;/span&gt; System;
    &lt;span class=&quot;keyword&quot;&gt;using&lt;/span&gt; System.Net;
    &lt;span class=&quot;keyword&quot;&gt;using&lt;/span&gt; System.Windows.Forms;
    &lt;span class=&quot;keyword&quot;&gt;using&lt;/span&gt; System.Runtime.InteropServices;
    &lt;span class=&quot;keyword&quot;&gt;using&lt;/span&gt; System.Diagnostics;
    &lt;span class=&quot;keyword&quot;&gt;using&lt;/span&gt; System.Drawing;
    [&lt;span class=&quot;class_def&quot;&gt;StructLayout&lt;/span&gt;(&lt;span class=&quot;class_def&quot;&gt;LayoutKind&lt;/span&gt;.Sequential)]
    &lt;span class=&quot;keyword&quot;&gt;public struct&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;Nmhdr&lt;/span&gt; {
        &lt;span class=&quot;keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;IntPtr&lt;/span&gt; HWndFrom;
        &lt;span class=&quot;keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;UIntPtr&lt;/span&gt; IdFrom;
        &lt;span class=&quot;keyword&quot;&gt;public int&lt;/span&gt; Code;
    }
    [&lt;span class=&quot;class_def&quot;&gt;StructLayout&lt;/span&gt;(&lt;span class=&quot;class_def&quot;&gt;LayoutKind&lt;/span&gt;.Sequential)]
    &lt;span class=&quot;keyword&quot;&gt;public struct&lt;/span&gt; NmIPAddress {
        &lt;span class=&quot;keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;Nmhdr&lt;/span&gt; Hdr;
        &lt;span class=&quot;keyword&quot;&gt;public int&lt;/span&gt; Field;
        &lt;span class=&quot;keyword&quot;&gt;public int&lt;/span&gt; Value;
    }
    [&lt;span class=&quot;keyword&quot;&gt;StructLayout&lt;/span&gt;(&lt;span class=&quot;keyword&quot;&gt;LayoutKind&lt;/span&gt;.Sequential)]
    &lt;span class=&quot;keyword&quot;&gt;public struct&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;InitCommonControlsEX&lt;/span&gt; {
        &lt;span class=&quot;keyword&quot;&gt;public int&lt;/span&gt; Size;
        &lt;span class=&quot;keyword&quot;&gt;public int&lt;/span&gt; Icc;
    }
    &lt;span class=&quot;keyword&quot;&gt;public enum&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;IPField&lt;/span&gt; { OctetOne = 0, OctetTwo = 1, OctetThree = 2, OctetFour = 3 }
    &lt;span class=&quot;keyword&quot;&gt;public delegate void&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;FieldChangedHandler&lt;/span&gt;(&lt;span class=&quot;keyword&quot;&gt;object&lt;/span&gt; sender, &lt;span class=&quot;class_def&quot;&gt;FieldChangedEventArgs&lt;/span&gt; e);
    &lt;span class=&quot;keyword&quot;&gt;public class&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;FieldChangedEventArgs&lt;/span&gt; : &lt;span class=&quot;class_def&quot;&gt;EventArgs&lt;/span&gt; {
        &lt;span class=&quot;keyword&quot;&gt;private int&lt;/span&gt; _field, _value;
        &lt;span class=&quot;keyword&quot;&gt;public int&lt;/span&gt; Field {
            &lt;span class=&quot;keyword&quot;&gt;get&lt;/span&gt; { &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; _field; }
        }
        &lt;span class=&quot;keyword&quot;&gt;public int&lt;/span&gt; Value {
            &lt;span class=&quot;keyword&quot;&gt;get&lt;/span&gt; { &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; _value; }
        }
        &lt;span class=&quot;keyword&quot;&gt;public&lt;/span&gt; FieldChangedEventArgs(&lt;span class=&quot;keyword&quot;&gt;int&lt;/span&gt; field, &lt;span class=&quot;keyword&quot;&gt;int&lt;/span&gt; value)
            : &lt;span class=&quot;keyword&quot;&gt;base&lt;/span&gt;() {
            _field = field;
            _value = value;
        }
    }
    &lt;span class=&quot;keyword&quot;&gt;public class&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;IPAddressControl&lt;/span&gt; : &lt;span class=&quot;class_def&quot;&gt;TextBox&lt;/span&gt; {
        &lt;span class=&quot;keyword&quot;&gt;private const int&lt;/span&gt; WM_NOTIFY = 0x004E,
            WM_USER = 0x0400,
            WM_REFLECT = WM_USER + 0x1C00,
            IPN_FIRST = -860,
            IPM_SETRANGE = (WM_USER + 103),
            IPM_GETADDRESS = (WM_USER + 102),
            IPM_SETADDRESS = (WM_USER + 101),
            IPM_CLEARADDRESS = (WM_USER + 100),
            IPM_ISBLANK = (WM_USER + 105),
            ICC_INTERNET_CLASSES = 0x00000800,
            CS_VREDRAW = 0x0001,
            CS_HREDRAW = 0x0002,
            CS_DBLCLKS = 0x0008,
            CS_GLOBALCLASS = 0x4000,
            WS_CHILD = 0x40000000,
            WS_VISIBLE = 0x10000000,
            WS_TABSTOP = 0x00010000,
            WS_EX_RIGHT = 0x00001000,
            WS_EX_LEFT = 0x00000000,
            WS_EX_RTLREADING = 0x00002000,
            WS_EX_LTRREADING = 0x00000000,
            WS_EX_LEFTSCROLLBAR = 0x00004000,
            WS_EX_RIGHTSCROLLBAR = 0x00000000,
            WS_EX_NOPARENTNOTIFY = 0x00000004,
            WS_EX_CLIENTEDGE = 0x00000200;
        &lt;span class=&quot;keyword&quot;&gt;private int&lt;/span&gt;[] values = &lt;span class=&quot;keyword&quot;&gt;new&lt;/span&gt; int[4];
        &lt;span class=&quot;keyword&quot;&gt;bool&lt;/span&gt; initialized = &lt;span class=&quot;keyword&quot;&gt;false&lt;/span&gt;;		
        &lt;span class=&quot;keyword&quot;&gt;public event&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;FieldChangedHandler&lt;/span&gt; FieldChanged;
        &lt;span class=&quot;keyword&quot;&gt;public&lt;/span&gt; IPAddressControl()
            : &lt;span class=&quot;keyword&quot;&gt;base&lt;/span&gt;() {
            &lt;span class=&quot;keyword&quot;&gt;for&lt;/span&gt;(&lt;span class=&quot;keyword&quot;&gt;int&lt;/span&gt; i = 0; i &lt; 4; i++)
                values[i] = 0;
        }
        [&lt;span class=&quot;class_def&quot;&gt;DllImport&lt;/span&gt;(&lt;span class=&quot;string&quot;&gt;&quot;comctl32&quot;&lt;/span&gt;)]
        &lt;span class=&quot;keyword&quot;&gt;static extern bool&lt;/span&gt; InitCommonControlsEx(&lt;span class=&quot;keyword&quot;&gt;ref&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;InitCommonControlsEX&lt;/span&gt; lpInitCtrls);

        &lt;span class=&quot;keyword&quot;&gt;protected virtual void&lt;/span&gt; OnFieldChanged(&lt;span class=&quot;class_def&quot;&gt;FieldChangedEventArgs&lt;/span&gt; e) {
            &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt;(FieldChanged != &lt;span class=&quot;keyword&quot;&gt;null&lt;/span&gt;) FieldChanged(&lt;span class=&quot;keyword&quot;&gt;this&lt;/span&gt;, e);
        }
        &lt;span class=&quot;keyword&quot;&gt;protected override&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;CreateParams&lt;/span&gt; CreateParams {
            &lt;span class=&quot;keyword&quot;&gt;get&lt;/span&gt; {
                &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt;(!initialized) {
                    &lt;span class=&quot;class_def&quot;&gt;InitCommonControlsEX&lt;/span&gt; ic = &lt;span class=&quot;keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;InitCommonControlsEX&lt;/span&gt;();
                    ic.Size = &lt;span class=&quot;class_def&quot;&gt;Marshal&lt;/span&gt;.SizeOf(&lt;span class=&quot;keyword&quot;&gt;typeof&lt;/span&gt;(&lt;span class=&quot;class_def&quot;&gt;InitCommonControlsEX&lt;/span&gt;));
                    ic.Icc = ICC_INTERNET_CLASSES;
                    initialized = InitCommonControlsEx(&lt;span class=&quot;keyword&quot;&gt;ref&lt;/span&gt; ic);
                }
                &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt;(initialized) {
                    &lt;span class=&quot;class_def&quot;&gt;CreateParams&lt;/span&gt; cp = base.CreateParams;
                    cp.ClassName = &lt;span class=&quot;string&quot;&gt;&quot;SysIPAddress32&quot;&lt;/span&gt;;
                    cp.Height = 23;
                    cp.ClassStyle = CS_VREDRAW | CS_HREDRAW | CS_DBLCLKS | CS_GLOBALCLASS;
                    cp.Style = WS_CHILD | WS_VISIBLE | WS_TABSTOP | 0x80;
                    cp.ExStyle = WS_EX_NOPARENTNOTIFY | WS_EX_CLIENTEDGE;
                    &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt;(RightToLeft == &lt;span class=&quot;class_def&quot;&gt;RightToLeft&lt;/span&gt;.No 
                                                    || (RightToLeft == &lt;span class=&quot;class_def&quot;&gt;RightToLeft&lt;/span&gt;.Inherit 
                                                    &amp;&amp; Parent.RightToLeft == &lt;span class=&quot;class_def&quot;&gt;RightToLeft&lt;/span&gt;.No)) {
                        cp.ExStyle |= WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR;
                    } else {
                        cp.ExStyle |= WS_EX_RIGHT | WS_EX_RTLREADING | WS_EX_LEFTSCROLLBAR;
                    }
                    &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; cp;
                } &lt;span class=&quot;keyword&quot;&gt;else&lt;/span&gt; {
                    &lt;span class=&quot;keyword&quot;&gt;return base&lt;/span&gt;.CreateParams;
                }
            }
        }
        &lt;span class=&quot;keyword&quot;&gt;public bool&lt;/span&gt; SetIPRange(&lt;span class=&quot;class_def&quot;&gt;IPField&lt;/span&gt; field, &lt;span class=&quot;keyword&quot;&gt;byte&lt;/span&gt; lowValue, &lt;span class=&quot;keyword&quot;&gt;byte&lt;/span&gt; highValue) {
            &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt;(!initialized) &lt;span class=&quot;keyword&quot;&gt;return false&lt;/span&gt;;		
            &lt;span class=&quot;class_def&quot;&gt;Message&lt;/span&gt; m = &lt;span class=&quot;class_def&quot;&gt;Message&lt;/span&gt;.Create(Handle, IPM_SETRANGE, (&lt;span class=&quot;class_def&quot;&gt;IntPtr&lt;/span&gt;)((&lt;span class=&quot;keyword&quot;&gt;int&lt;/span&gt;)field), MakeRange(lowValue, highValue));
            WndProc(&lt;span class=&quot;keyword&quot;&gt;ref&lt;/span&gt; m);
            &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; m.Result.ToInt32() &gt; 0;
        }
        &lt;span class=&quot;keyword&quot;&gt;public&lt;/span&gt; System.Net.&lt;span class=&quot;class_def&quot;&gt;IPAddress&lt;/span&gt; IPAddress {
            &lt;span class=&quot;keyword&quot;&gt;get&lt;/span&gt; {
                &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt;(!initialized) &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;IPAddress&lt;/span&gt;.None;			
                &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;IPAddress&lt;/span&gt;.Parse(&lt;span class=&quot;keyword&quot;&gt;base&lt;/span&gt;.Text);
            }
        }
        &lt;span class=&quot;keyword&quot;&gt;public bool&lt;/span&gt; IsBlank {
            &lt;span class=&quot;keyword&quot;&gt;get&lt;/span&gt; {
                &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt;(!initialized) &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; !(&lt;span class=&quot;keyword&quot;&gt;base&lt;/span&gt;.Text.Length &gt; 0);
                &lt;span class=&quot;class_def&quot;&gt;Message&lt;/span&gt; m = &lt;span class=&quot;class_def&quot;&gt;Message&lt;/span&gt;.Create(Handle, IPM_ISBLANK, &lt;span class=&quot;class_def&quot;&gt;IntPtr&lt;/span&gt;.Zero, &lt;span class=&quot;class_def&quot;&gt;IntPtr&lt;/span&gt;.Zero);
                WndProc(&lt;span class=&quot;keyword&quot;&gt;ref&lt;/span&gt; m);
                &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; m.Result.ToInt32() &gt; 0;
            }
        }
        &lt;span class=&quot;keyword&quot;&gt;new public void&lt;/span&gt; Clear() {
            &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt;(!initialized) {
                &lt;span class=&quot;keyword&quot;&gt;base&lt;/span&gt;.Clear();
                &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt;;
            }		
            &lt;span class=&quot;class_def&quot;&gt;Message&lt;/span&gt; m = &lt;span class=&quot;class_def&quot;&gt;Message&lt;/span&gt;.Create(Handle, IPM_CLEARADDRESS, &lt;span class=&quot;class_def&quot;&gt;IntPtr&lt;/span&gt;.Zero, &lt;span class=&quot;class_def&quot;&gt;IntPtr&lt;/span&gt;.Zero);
            WndProc(&lt;span class=&quot;keyword&quot;&gt;ref&lt;/span&gt; m);
        }
        &lt;span class=&quot;keyword&quot;&gt;private&lt;/span&gt; System.Net.&lt;span class=&quot;class_def&quot;&gt;IPAddress&lt;/span&gt; GetIpAddress(&lt;span class=&quot;class_def&quot;&gt;IntPtr&lt;/span&gt; ip) {
            &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt;(!initialized) &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;IPAddress&lt;/span&gt;.None;		
            &lt;span class=&quot;keyword&quot;&gt;return new&lt;/span&gt; IPAddress(ip.ToInt64());
        }
        &lt;span class=&quot;keyword&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;IntPtr&lt;/span&gt; MakeRange(&lt;span class=&quot;keyword&quot;&gt;byte&lt;/span&gt; low, &lt;span class=&quot;keyword&quot;&gt;byte&lt;/span&gt; high) {
            &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; (&lt;span class=&quot;class_def&quot;&gt;IntPtr&lt;/span&gt;)((&lt;span class=&quot;keyword&quot;&gt;int&lt;/span&gt;)((high &lt;&lt; 8) + low));
        }
        &lt;span class=&quot;keyword&quot;&gt;protected override void&lt;/span&gt; WndProc(&lt;span class=&quot;keyword&quot;&gt;ref&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;Message&lt;/span&gt; m) {
            &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt;(m.Msg == (WM_REFLECT + WM_NOTIFY)) {
                &lt;span class=&quot;class_def&quot;&gt;NmIPAddress&lt;/span&gt; ipInfo = (&lt;span class=&quot;class_def&quot;&gt;NmIPAddress&lt;/span&gt;)&lt;span class=&quot;class_def&quot;&gt;Marshal&lt;/span&gt;.PtrToStructure(m.LParam, &lt;span class=&quot;keyword&quot;&gt;typeof&lt;/span&gt;(&lt;span class=&quot;class_def&quot;&gt;NmIPAddress&lt;/span&gt;));
                &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt;(ipInfo.Hdr.Code == IPN_FIRST) {
                    &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt;(values[ipInfo.Field] != ipInfo.Value) {
                        values[ipInfo.Field] = ipInfo.Value;
                        OnFieldChanged(&lt;span class=&quot;keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;FieldChangedEventArgs&lt;/span&gt;(ipInfo.Field, ipInfo.Value));
                    }
                }
            }
            &lt;span class=&quot;keyword&quot;&gt;base&lt;/span&gt;.WndProc(&lt;span class=&quot;keyword&quot;&gt;ref&lt;/span&gt; m);
        }
    }
}&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
It does have one wrapped event from the original control and that is FieldChanged, which provides feedback when each octet is changed (similar to TextChanged for a text box, but only fires after the user has entered the entire text for that octet).  Likewise, if the value of that octet is larger than a byte, it is automatically changed back to 255 (or the max value of a byte).  When the event fires, it passes back, the Octet that was changed, and its value.&lt;br /&gt;
&lt;br /&gt;
You can specify the IP Range (which the original control allows, and I've wrapped into this control as well) so that you can edit the control to only accept a specified IP Address Range.&lt;br /&gt;
&lt;br /&gt;
I've also provided Native Clear functionality as well as the IsBlank from the original control.  I added a System.Net.IPAddress property so that when accessing the IPAddress of the control, you didn't need to parse the System.Net.IPAddress every time.  My original control completely removed the &amp;quot;Text&amp;quot; property so that you got and set the IPAddress through this property, but alas, that functionality doesn't exist in this control.&lt;br /&gt;
&lt;br /&gt;
UPDATE: 8/25/2006 - Made 2 changes to the source code. 1. added some Initialization logic to keep InitCommonCtrls from being called multiple times and, 2. added styles for ClassStyle, StyleEX and Style to keep an exception from being thrown in certain cases when a WM_GETTEXTLENGTH message was sent.&lt;br /&gt;
&lt;br /&gt;
For questions, comments, bugs, or anything else in this control, feel free to use the Windows Controls section of the forum &lt;a href=&quot;http://sanity-free.org/forum/viewforum.php?id=5&quot; title=&quot;Custom Windows Control Forum&quot;&gt;here&lt;/a&gt;.</description>
      <pubDate>Mon, 31 Jul 2006 01:16:43 EDT</pubDate>
      <link>http://sanity-free.org/127/an_ipaddress_control_the_win32_sysipaddress32_control_in_csharp.html</link>
    </item>
        <item>
      <title>The C# 3.0 language specification</title>
      <description>&lt;div class=&quot;big_image&quot;&gt;&lt;img src=&quot;/images/csharp3.gif&quot; border=&quot;0&quot; /&gt;&lt;/div&gt;
I know I'm a bit late here, but better late than never.&lt;br /&gt;
&lt;br /&gt;
I've been reading over some of the changes to C# for version 3.  I had heard that v3 would be v2 with the addition of the new WPF, but looking over the new spec (well, fairly new anyway), there is quite a bit more.&lt;br /&gt;
&lt;br /&gt;
Implicit types, method extensions, lambda expressions, object / collection initializers, and LINQ... C# is going to look really weird!  So much for simplicity.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Implicit types&lt;/b&gt; (csharp meets vb or php) allow you to define the variable without a (or with an implied) type:&lt;br /&gt;
&lt;pre class=&quot;code&quot;&gt;
&lt;span class=&quot;keyword&quot;&gt;var&lt;/span&gt; foo = &lt;span class=&quot;string&quot;&gt;&quot;hello&quot;&lt;/span&gt;;
&lt;span class=&quot;keyword&quot;&gt;var&lt;/span&gt; bar = 32;
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
so foo is implied to be of type string, and bar is implied to be of some numeric type.
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Method extensions&lt;/b&gt; look very strange.  Think of old dog learns new tricks meets operator overloading.  Method extensions allow you to create &quot;extension&quot; methods for existing types:
&lt;pre class=&quot;code&quot;&gt;&lt;span class=&quot;keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;Bar&lt;/span&gt; {
    &lt;span class=&quot;keyword&quot;&gt;string&lt;/span&gt; what = &lt;span class=&quot;string&quot;&gt;&quot;hello&quot;&lt;/span&gt;;
    &lt;span class=&quot;keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;string&lt;/span&gt; What { &lt;span class=&quot;keyword&quot;&gt;get&lt;/span&gt; { &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; what; } &lt;span class=&quot;keyword&quot;&gt;set&lt;/span&gt; { what = &lt;span class=&quot;keyword&quot;&gt;value&lt;/span&gt;; } }
    &lt;span class=&quot;keyword&quot;&gt;public&lt;/span&gt; Bar() {}
    &lt;span class=&quot;keyword&quot;&gt;public&lt;/span&gt; void Hello() { 
        &lt;span class=&quot;class_def&quot;&gt;Console&lt;/span&gt;.WriteLine(what); 
    }
}

&lt;span class=&quot;keyword&quot;&gt;public static class&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;Foo&lt;/span&gt; {
    &lt;span class=&quot;keyword&quot;&gt;public static void&lt;/span&gt; SayHelloToSteve(&lt;span class=&quot;keyword&quot;&gt;this&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;Bar&lt;/span&gt; bar) {
        bar.What += &lt;span class=&quot;string&quot;&gt;&quot; steve&quot;&lt;/span&gt;;
        bar.Hello();
    }
    &lt;span class=&quot;keyword&quot;&gt;public static void&lt;/span&gt; SayHelloToSomeone(&lt;span class=&quot;keyword&quot;&gt;this&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;Bar&lt;/span&gt; bar, &lt;span class=&quot;keyword&quot;&gt;string&lt;/span&gt; who) {
        bar.What = who;
        bar.Hello();
    } 
}

&lt;span class=&quot;keyword&quot;&gt;public class&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;Program&lt;/span&gt; {
    &lt;span class=&quot;keyword&quot;&gt;static void&lt;/span&gt; Main(&lt;span class=&quot;keyword&quot;&gt;string&lt;/span&gt;[] args) {
        &lt;span class=&quot;class_def&quot;&gt;Bar&lt;/span&gt; b = &lt;span class=&quot;keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;Bar&lt;/span&gt;();
        b.Hello();
        b.SayHelloToSteve();
        b.SayHelloToSomeone(&quot;Hello Bob!&quot;);
    }
}&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
Notice that SayHelloToSteve() and SayHelloToSomeone(string s) are not member methods of the Bar class, but are invoked as if they were (by virtue of the Method extension defined in Foo that are available to Bar).  This seems like a feature that will be abused to defeat inheritance (by adding reflection to the method extenstions to manipulate internal class memebers without inheriting and changing functionality of the object) that would otherwise look odd and ugly.
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;OUTPUT:&lt;/b&gt;
&lt;pre class=&quot;code&quot;&gt;hello
hello steve
Hello Bob!&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
At this point &lt;b&gt;lambda expressions&lt;/b&gt; dont make much sense to me, but it seems its a way to define more than one method body in an anonymous method.
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Object / collection initialization&lt;/b&gt; makes initialization a lot easier.  I don't know if you've ever wanted to declare a new object inline inside a method, only to realize that you needed to set a few properties before it was used, well this is the answer to that:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;code&quot;&gt;myObject.Blah(new &lt;span class=&quot;class_def&quot;&gt;SomeObject&lt;/span&gt;() { PropertyA = 1, PropertyB = &lt;span class=&quot;string&quot;&gt;&amp;quot;dude&amp;quot;&lt;/span&gt;, PropertyC = &lt;span class=&quot;keyword&quot;&gt;false&lt;/span&gt; });&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
Then there's &lt;b&gt;LINQ&lt;/b&gt;: which feels something like SQL for C#.
&lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;/documents/CSharp3Specification.zip&quot; title=&quot;download the CSharp v3 spec&quot;&gt;&lt;img src=&quot;/images/download.gif&quot; border=&quot;0&quot; /&gt; feel free to download the spec here&lt;/a&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;em&gt;fin&lt;/em&gt;</description>
      <pubDate>Sat, 29 Jul 2006 01:50:13 EDT</pubDate>
      <link>http://sanity-free.org/126/the_csharp_3_0_language_specification.html</link>
    </item>
        <item>
      <title>PHP Webservices and C# / .NET SOAP Clients</title>
      <description>Code Zulu Bind Maker is a project I started working on about 3 years ago. Its a stupid little app that configures game settings for Counter-Strike. When I wrote it, version 1.6 was in beta, and everybody played version 1.5 (which was ever so different).  Anyway I run a website that supports the app, and of course, its written in PHP / MySql (my poison of choice on the web). &lt;br /&gt;
    &lt;br /&gt;
  The reason for the boring and seemingly pointless background is that ever since I wrote the app, I always wanted to integrate the website and application, but never really thought of a &amp;quot;good&amp;quot; way to do it. A couple days ago, I had some spare time and sat down to see how easy it would be to create some PHP webservices that I could easily consume in C#. I had been considering expanding the community aspect of the bind maker website into the application so that they'd work seamlessly together. I also wanted to add a common place that gaming communities could spend their time (which I had previously planned for just the website, but was thinking of the possibilities of bringing that into the app as well), and a PHP webservice combined with a C# windows app seemed like the best option.&lt;br /&gt;
  &lt;br /&gt;
  C# webservices (server side) are about as easy-as-it-gets to write. C#'s SOAP client is completely transparent (or I should say completely generated so you code just like you would use any other objects) and is completely brainless to use. Since I'd never done webservice work in PHP, I started asking Mr. Google how it worked, and what would you now but that I stumbled across the &lt;a href=&quot;http://sourceforge.net/projects/nusoap/&quot; title=&quot;NuSOAP a SOAP library for PHP&quot;&gt;NuSOAP&lt;/a&gt; library for php. It sure beat the heck out of writing all my WSDL files by hand, wrapping and unwrapping stupidly simple SOAP messages, and hacking everything very poorly. &lt;br /&gt;
  &lt;br /&gt;
  My first PHP webservice iteration was a simple &amp;quot;hello &amp;lt;name&amp;gt; &amp;quot; web method.  It used a simple type (xsd:string) as its parameter and returned a simple type (again another xsd:string) as its return value and looked like this:
 &lt;pre class=&quot;code&quot;&gt;
 &lt;span class=&quot;php_comments&quot;&gt;/**
 * ProcessSimpleType method
 * &lt;strong&gt;@param&lt;/strong&gt; string $who name of the person we'll say hello to
 * &lt;strong&gt;@return&lt;/strong&gt; string $helloText the hello &lt;name&gt; string
 */&lt;/span&gt;
&lt;span class=&quot;keyword&quot;&gt;function&lt;/span&gt; ProcessSimpleType(&lt;span class=&quot;php_var&quot;&gt;$who&lt;/span&gt;) {
	return &lt;span class=&quot;php_string&quot;&gt;&quot;Hello &lt;/span&gt;&lt;span class=&quot;php_var&quot;&gt;$who&lt;/span&gt;&lt;span class=&quot;php_string&quot;&gt;&quot;&lt;/span&gt;;
}
&lt;/pre&gt; 
 NuSoap was great.  It took care of all the nitty-gritty work that I had previously done myself.  All it took was about 8 lines of code to set up the SOAP server and register the method and my types.
&lt;br /&gt;
&lt;pre class=&quot;code&quot;&gt;&lt;span class=&quot;keyword&quot;&gt;require_once&lt;/span&gt;(&lt;span class=&quot;php_string&quot;&gt;&quot;lib/nusoap/nusoap.php&quot;&lt;/span&gt;);
&lt;span class=&quot;php_var&quot;&gt;$namespace&lt;/span&gt; = &lt;span class=&quot;php_string&quot;&gt;&quot;http://sanity-free.org/services&quot;&lt;/span&gt;;
&lt;span class=&quot;php_comments&quot;&gt;// create a new soap server&lt;/span&gt;
&lt;span class=&quot;php_var&quot;&gt;$server&lt;/span&gt; = new soap_server();
&lt;span class=&quot;php_comments&quot;&gt;// configure our WSDL&lt;/span&gt;
&lt;span class=&quot;php_var&quot;&gt;$server&lt;/span&gt;-&gt;configureWSDL(&lt;span class=&quot;php_string&quot;&gt;&quot;SimpleService&quot;&lt;/span&gt;);
&lt;span class=&quot;php_comments&quot;&gt;// set our namespace&lt;/span&gt;
&lt;span class=&quot;php_var&quot;&gt;$server&lt;/span&gt;-&gt;wsdl-&gt;schemaTargetNamespace = &lt;span class=&quot;php_var&quot;&gt;$namespace&lt;/span&gt;;
&lt;span class=&quot;php_comments&quot;&gt;// register our WebMethod&lt;/span&gt;
&lt;span class=&quot;php_var&quot;&gt;$server&lt;/span&gt;-&gt;register(
                &lt;span class=&quot;php_comments&quot;&gt;// method name:&lt;/span&gt;
                &lt;span class=&quot;php_string&quot;&gt;'ProcessSimpleType',&lt;/span&gt; 		 
                &lt;span class=&quot;php_comments&quot;&gt;// parameter list:&lt;/span&gt;
                &lt;span class=&quot;keyword&quot;&gt;array&lt;/span&gt;(&lt;span class=&quot;php_string&quot;&gt;'name'&lt;/span&gt;=&gt;&lt;span class=&quot;php_string&quot;&gt;'xsd:string'&lt;/span&gt;), 
                &lt;span class=&quot;php_comments&quot;&gt;// return value(s):&lt;/span&gt;
                &lt;span class=&quot;keyword&quot;&gt;array&lt;/span&gt;(&lt;span class=&quot;php_string&quot;&gt;'return'&lt;/span&gt;=&gt;&lt;span class=&quot;php_string&quot;&gt;'xsd:string'&lt;/span&gt;),
                &lt;span class=&quot;php_comments&quot;&gt;// namespace:&lt;/span&gt;
                &lt;span class=&quot;php_var&quot;&gt;$namespace&lt;/span&gt;,
                &lt;span class=&quot;php_comments&quot;&gt;// soapaction: (use default)&lt;/span&gt;
                &lt;span class=&quot;keyword&quot;&gt;false&lt;/span&gt;,
                &lt;span class=&quot;php_comments&quot;&gt;// style: rpc or document&lt;/span&gt;
                &lt;span class=&quot;php_string&quot;&gt;'rpc'&lt;/span&gt;,
                &lt;span class=&quot;php_comments&quot;&gt;// use: encoded or literal&lt;/span&gt;
                &lt;span class=&quot;php_string&quot;&gt;'encoded'&lt;/span&gt;,
                &lt;span class=&quot;php_comments&quot;&gt;// description: documentation for the method&lt;/span&gt;
                &lt;span class=&quot;php_string&quot;&gt;'A simple Hello World web method'&lt;/span&gt;);
                
&lt;span class=&quot;php_comments&quot;&gt;// Get our posted data if the service is being consumed
// otherwise leave this data blank. &lt;/span&gt;               
&lt;span class=&quot;php_var&quot;&gt;$POST_DATA&lt;/span&gt; = &lt;span class=&quot;keyword&quot;&gt;isset&lt;/span&gt;(&lt;span class=&quot;php_var&quot;&gt;$GLOBALS&lt;/span&gt;[&lt;span class=&quot;php_string&quot;&gt;'HTTP_RAW_POST_DATA'&lt;/span&gt;]) 
                ? &lt;span class=&quot;php_var&quot;&gt;$GLOBALS&lt;/span&gt;[&lt;span class=&quot;php_string&quot;&gt;'HTTP_RAW_POST_DATA'&lt;/span&gt;] : &lt;span class=&quot;php_string&quot;&gt;''&lt;/span&gt;;

&lt;span class=&quot;php_comments&quot;&gt;// pass our posted data (or nothing) to the soap service&lt;/span&gt;                    
&lt;span class=&quot;php_var&quot;&gt;$server&lt;/span&gt;-&gt;service(&lt;span class=&quot;php_var&quot;&gt;$POST_DATA&lt;/span&gt;);                
&lt;span class=&quot;keyword&quot;&gt;exit&lt;/span&gt;();&lt;/pre&gt;
That was it.  From visual studio I was then able to consume the webservice by adding a web reference&lt;br/&gt;
&lt;br /&gt;
&lt;div class=&quot;big_image&quot;&gt;&lt;img src=&quot;/images/addref.gif&quot; border=&quot;0&quot; alt=&quot;Add A Web Reference&quot; /&gt;&lt;/div&gt;
&lt;br /&gt;
This article was first written with visual studio 2005.  Visual studio 2008 and 2010 no longer have the &quot;Add Web Reference&quot;
menu item.  Instead they have an option to add a service reference.  Selecting this option ends up generating a WCF C# client proxy,
but we'll want to use the asmx style web service client proxy, so we'll navigate to the add web reference dialog which is hidden deep
inside Add Service Reference dialog.&lt;br/&gt;&lt;br/&gt;
&lt;fieldset style=&quot;padding:8px; margin-left:10px; margin-right:10px; border: 1px solid #ccc;&quot;&gt;&lt;legend style=&quot;padding: 4px; border: 1px solid #ccc; background-color:#000; color:#fff;&quot;&gt;Visual Studio 2008 and 2010 Only&lt;/legend&gt;&lt;br /&gt;
Right click on the references folder, and select Add Service Reference&lt;br/&gt;
&lt;br/&gt;
&lt;div class=&quot;big_image&quot;&gt;&lt;img src=&quot;/images/svc-ref.png&quot; border=&quot;0&quot; alt=&quot;Add A Service Reference&quot; /&gt;&lt;/div&gt;&lt;br/&gt;&lt;br/&gt;
Click on the advanced button&lt;br/&gt;
&lt;br /&gt;
&lt;div class=&quot;big_image&quot;&gt;&lt;img src=&quot;/images/svc-adv.png&quot; border=&quot;0&quot; alt=&quot;Advanced&quot; /&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;big_image&quot;&gt;&lt;img src=&quot;/images/svc-awr.png&quot; border=&quot;0&quot; alt=&quot;Click Add A Web Reference&quot; /&gt;&lt;/div&gt;
&lt;/fieldset&gt;
&lt;br/&gt;
Finally click the Add Web Reference button&lt;br/&gt;
&lt;br /&gt;
&lt;div class=&quot;big_image&quot;&gt;&lt;img src=&quot;/images/refdiag.gif&quot; border=&quot;0&quot; alt=&quot;Add A Web Reference Dialog&quot; /&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;big_image&quot;&gt;&lt;img src=&quot;/images/webref.gif&quot; border=&quot;0&quot; alt=&quot;Add A Web Reference&quot; /&gt;&lt;/div&gt;
&lt;br /&gt;
With the reference added, I put a button on my form, and added the following button click event handler:
&lt;br /&gt;
&lt;!-- readmore --&gt;
&lt;pre class=&quot;code&quot;&gt;&lt;span class=&quot;keyword&quot;&gt;private void&lt;/span&gt; button1_Click(&lt;span class=&quot;keyword&quot;&gt;object&lt;/span&gt; sender, &lt;span class=&quot;class_def&quot;&gt;EventArgs&lt;/span&gt; e) {
    &lt;span class=&quot;class_def&quot;&gt;SimpleService&lt;/span&gt; svc = &lt;span class=&quot;keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;SimpleService&lt;/span&gt;();
    &lt;span class=&quot;keyword&quot;&gt;string&lt;/span&gt; s = svc.ProcessSimpleType(&lt;span class=&quot;string&quot;&gt;&quot;steve&quot;&lt;/span&gt;);
    &lt;span class=&quot;class_def&quot;&gt;MessageBox&lt;/span&gt;.Show(s);
}&lt;/pre&gt;
Ran my application, pressed the button (which in turn called the webservice), and voil&amp;agrave; I get a message box telling me Hello:
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;small_image&quot;&gt;&lt;img src=&quot;/images/hellosteve.gif&quot; border=&quot;0&quot; alt=&quot;Hello Steve Message Box&quot; /&gt;&lt;/div&gt;
&lt;br /&gt;
That's great!  Completely useless, but great!&lt;br /&gt;
&lt;br /&gt;
Now to get a little more complicated...  I had planned on using complex data types that would be consumed by both PHP and .NET clients (A PHP SOAP client for the website, and .NET client for the windows application). It may sound stupid to have a PHP client when most likely I'd have access to the method directly, but the server that is going to host the webservices is actually different than the &amp;quot;web presence&amp;quot; server I host my website on. Anyway, the test complex types I came up needed to look like simple structs (in C#) which would also be used in an array, and they looked something like this: &lt;br /&gt;
&lt;pre class=&quot;code&quot;&gt;&lt;span class=&quot;keyword&quot;&gt;public struct&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;MySoapObject&lt;/span&gt; {
    &lt;span class=&quot;keyword&quot;&gt;public string&lt;/span&gt; Author;
    &lt;span class=&quot;keyword&quot;&gt;public string&lt;/span&gt; Name;
    &lt;span class=&quot;keyword&quot;&gt;public string &lt;/span&gt;Description;
    &lt;span class=&quot;keyword&quot;&gt;public string&lt;/span&gt; Text;
    &lt;span class=&quot;keyword&quot;&gt;public int&lt;/span&gt; VoteTotal;
    &lt;span class=&quot;keyword&quot;&gt;public int&lt;/span&gt; VoteCount;
}

&lt;span class=&quot;class_def&quot;&gt;MySoapObject&lt;/span&gt;[] soapObjects = &lt;span class=&quot;keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;MySoapObject&lt;/span&gt;[10];&lt;/pre&gt;
so I added the definitions in PHP using NuSOAP like this:
&lt;br /&gt;
&lt;pre class=&quot;code&quot;&gt;&lt;span class=&quot;php_var&quot;&gt;$server&lt;/span&gt;-&gt;wsdl-&gt;addComplexType(
    &lt;span class=&quot;php_string&quot;&gt;'MySoapObject'&lt;/span&gt;,
    &lt;span class=&quot;php_string&quot;&gt;'complexType'&lt;/span&gt;,
    &lt;span class=&quot;php_string&quot;&gt;'struct'&lt;/span&gt;,
    &lt;span class=&quot;php_string&quot;&gt;'all'&lt;/span&gt;,
    &lt;span class=&quot;php_string&quot;&gt;''&lt;/span&gt;,
    &lt;span class=&quot;keyword&quot;&gt;array&lt;/span&gt;(
        &lt;span class=&quot;php_string&quot;&gt;'Author'&lt;/span&gt; =&gt; &lt;span class=&quot;keyword&quot;&gt;array&lt;/span&gt;(&lt;span class=&quot;php_string&quot;&gt;'name'&lt;/span&gt;=&gt;&lt;span class=&quot;php_string&quot;&gt;'Author'&lt;/span&gt;,&lt;span class=&quot;php_string&quot;&gt;'type'&lt;/span&gt;=&gt;&lt;span class=&quot;php_string&quot;&gt;'xsd:string'&lt;/span&gt;),
        &lt;span class=&quot;php_string&quot;&gt;'Name'&lt;/span&gt; =&gt; &lt;span class=&quot;keyword&quot;&gt;array&lt;/span&gt;(&lt;span class=&quot;php_string&quot;&gt;'name'&lt;/span&gt;=&gt;&lt;span class=&quot;php_string&quot;&gt;'Name'&lt;/span&gt;,&lt;span class=&quot;php_string&quot;&gt;'type'&lt;/span&gt;=&gt;&lt;span class=&quot;php_string&quot;&gt;'xsd:string'&lt;/span&gt;),
        &lt;span class=&quot;php_string&quot;&gt;'Description'&lt;/span&gt; =&gt; &lt;span class=&quot;keyword&quot;&gt;array&lt;/span&gt;(&lt;span class=&quot;php_string&quot;&gt;'name'&lt;/span&gt;=&gt;&lt;span class=&quot;php_string&quot;&gt;'Description'&lt;/span&gt;,&lt;span class=&quot;php_string&quot;&gt;'type'&lt;/span&gt;=&gt;&lt;span class=&quot;php_string&quot;&gt;'xsd:string'&lt;/span&gt;),
        &lt;span class=&quot;php_string&quot;&gt;'Text'&lt;/span&gt; =&gt; &lt;span class=&quot;keyword&quot;&gt;array&lt;/span&gt;(&lt;span class=&quot;php_string&quot;&gt;'name'&lt;/span&gt;=&gt;&lt;span class=&quot;php_string&quot;&gt;'Text'&lt;/span&gt;,&lt;span class=&quot;php_string&quot;&gt;'type'&lt;/span&gt;=&gt;&lt;span class=&quot;php_string&quot;&gt;'xsd:string'&lt;/span&gt;),
        &lt;span class=&quot;php_string&quot;&gt;'VoteTotal'&lt;/span&gt; =&gt; &lt;span class=&quot;keyword&quot;&gt;array&lt;/span&gt;(&lt;span class=&quot;php_string&quot;&gt;'name'&lt;/span&gt;=&gt;&lt;span class=&quot;php_string&quot;&gt;'VoteTotal'&lt;/span&gt;,&lt;span class=&quot;php_string&quot;&gt;'type'=&gt;'xsd:int'&lt;/span&gt;),
        &lt;span class=&quot;php_string&quot;&gt;'VoteCount'&lt;/span&gt; =&gt; &lt;span class=&quot;keyword&quot;&gt;array&lt;/span&gt;(&lt;span class=&quot;php_string&quot;&gt;'name'&lt;/span&gt;=&gt;&lt;span class=&quot;php_string&quot;&gt;'VoteCount'&lt;/span&gt;,&lt;span class=&quot;php_string&quot;&gt;'type'&lt;/span&gt;=&gt;&lt;span class=&quot;php_string&quot;&gt;'xsd:int'&lt;/span&gt;)
    )
);

&lt;span class=&quot;php_var&quot;&gt;$server&lt;/span&gt;-&gt;wsdl-&gt;addComplexType(
    &lt;span class=&quot;php_string&quot;&gt;'MySoapObjectArray'&lt;/span&gt;,
    &lt;span class=&quot;php_string&quot;&gt;'complexType'&lt;/span&gt;,
    &lt;span class=&quot;php_string&quot;&gt;'array'&lt;/span&gt;,
    &lt;span class=&quot;php_string&quot;&gt;''&lt;/span&gt;,
    &lt;span class=&quot;php_string&quot;&gt;'SOAP-ENC:Array'&lt;/span&gt;,
    &lt;span class=&quot;keyword&quot;&gt;array&lt;/span&gt;(),
    &lt;span class=&quot;keyword&quot;&gt;array&lt;/span&gt;(&lt;span class=&quot;keyword&quot;&gt;array&lt;/span&gt;(&lt;span class=&quot;php_string&quot;&gt;'ref'&lt;/span&gt;=&gt;&lt;span class=&quot;php_string&quot;&gt;'SOAP-ENC:arrayType'&lt;/span&gt;,&lt;span class=&quot;php_string&quot;&gt;'wsdl:arrayType'&lt;/span&gt;=&gt;&lt;span class=&quot;php_string&quot;&gt;'tns:MySoapObject[]'&lt;/span&gt;)),
    &lt;span class=&quot;php_string&quot;&gt;'tns:MySoapObject'&lt;/span&gt;
);&lt;/pre&gt;
I defined the method in PHP that would take in an array of MySoapObjects, process them, and then return one MySoapObject that it had &amp;quot;processed&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
That method looked like this:&lt;br /&gt;
&lt;pre class=&quot;code&quot;&gt;&lt;span class=&quot;keyword&quot;&gt;function&lt;/span&gt; ProcessMySoapObject(&lt;span class=&quot;php_var&quot;&gt;$mySoapObjects&lt;/span&gt;) {
	&lt;span class=&quot;php_var&quot;&gt;$mso&lt;/span&gt; = &lt;span class=&quot;php_var&quot;&gt;$mySoapObjects&lt;/span&gt;[&lt;span class=&quot;php_red&quot;&gt;3&lt;/span&gt;];
	&lt;span class=&quot;php_var&quot;&gt;$mso&lt;/span&gt;[&lt;span class=&quot;php_string&quot;&gt;'Name'&lt;/span&gt;] = &lt;span class=&quot;php_string&quot;&gt;&quot;|||&quot;&lt;/span&gt;;
	&lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;php_var&quot;&gt;$mso&lt;/span&gt;;
}&lt;/pre&gt;
The SOAP server registration:
&lt;pre class=&quot;code&quot;&gt;&lt;span class=&quot;php_var&quot;&gt;$server&lt;/span&gt;-&gt;register(
    &lt;span class=&quot;php_string&quot;&gt;'ProcessMySoapObject'&lt;/span&gt;,
    &lt;span class=&quot;keyword&quot;&gt;array&lt;/span&gt;(&lt;span class=&quot;php_string&quot;&gt;'soapObjects'&lt;/span&gt;=&gt;&lt;span class=&quot;php_string&quot;&gt;'tns:MySoapObjectArray'&lt;/span&gt;),
    &lt;span class=&quot;keyword&quot;&gt;array&lt;/span&gt;(&lt;span class=&quot;php_string&quot;&gt;'return'&lt;/span&gt;=&gt;&lt;span class=&quot;php_string&quot;&gt;'tns:MySoapObject'&lt;/span&gt;),
    &lt;span class=&quot;php_var&quot;&gt;$ns&lt;/span&gt;,
    &lt;span class=&quot;keyword&quot;&gt;false&lt;/span&gt;,
    &lt;span class=&quot;php_string&quot;&gt;'rpc'&lt;/span&gt;,
    &lt;span class=&quot;keyword&quot;&gt;false&lt;/span&gt;,
    &lt;span class=&quot;php_string&quot;&gt;'Processes an array of MySoapObjects and returns one of them'&lt;/span&gt;);&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
And I did exactly the same as I did before--and added the web reference in visual studio to my project.  This time not only did Visual Studio add the Service class and web method, it added the 2 data types I had defined in PHP on my SOAP server (actually, it just built the MySoapObject, and the array was left out because arrays are an implied data type).&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;big_image&quot;&gt;&lt;img src=&quot;/images/mysoap.gif&quot; border=&quot;0&quot; alt=&quot;The object browser showing the structure of the MySoapObject&quot; /&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;big_image&quot;&gt;&lt;img src=&quot;/images/processmysoap.gif&quot; border=&quot;0&quot; alt=&quot;The object browser showing the method ProcessMySoapObject&quot; /&gt;&lt;/div&gt;
&lt;br /&gt;
I changed my button click event in my windows form and added the following code:
&lt;pre class=&quot;code&quot;&gt;&lt;span class=&quot;keyword&quot;&gt;private void&lt;/span&gt; button1_Click(&lt;span class=&quot;keyword&quot;&gt;object&lt;/span&gt; sender, &lt;span class=&quot;class_def&quot;&gt;EventArgs&lt;/span&gt; e) {
    &lt;span class=&quot;class_def&quot;&gt;Services&lt;/span&gt; s = &lt;span class=&quot;keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;Services&lt;/span&gt;();
    &lt;span class=&quot;class_def&quot;&gt;MySoapObject&lt;/span&gt;[] ms = &lt;span class=&quot;keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;MySoapObject&lt;/span&gt;[10];
    &lt;span class=&quot;keyword&quot;&gt;for&lt;/span&gt;(&lt;span class=&quot;keyword&quot;&gt;int&lt;/span&gt; i = 0; i &lt; ms.Length; i++) {
        ms[i] = &lt;span class=&quot;keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;class_def&quot;&gt;MySoapObject&lt;/span&gt;();
        ms[i].Author = &lt;span class=&quot;string&quot;&gt;&quot;Steve&quot;&lt;/span&gt;;
        ms[i].Description = &lt;span class=&quot;string&quot;&gt;&quot;The One that should be returned&quot;&lt;/span&gt;;
        ms[i].Name = i.ToString();
        ms[i].Text = &lt;span class=&quot;string&quot;&gt;&quot;something something something &quot;&lt;/span&gt; + i.ToString();
        ms[i].VoteCount = i * 2;
        ms[i].VoteTotal = (&lt;span class=&quot;keyword&quot;&gt;int&lt;/span&gt;)&lt;span class=&quot;class_def&quot;&gt;Math&lt;/span&gt;.Pow(ms[i].VoteCount, 2);
    }
    &lt;span class=&quot;class_def&quot;&gt;MySoapObject&lt;/span&gt; retn = s.ProcessMySoapObject(ms);
    &lt;span class=&quot;keyword&quot;&gt;string&lt;/span&gt; output = &lt;span class=&quot;string&quot;&gt;&quot;&quot;&lt;/span&gt;;
    output += retn.Author + &lt;span class=&quot;string&quot;&gt;&quot;\t\t\r\n&quot;&lt;/span&gt;;
    output += retn.Description + &lt;span class=&quot;string&quot;&gt;&quot;\t\t\r\n&quot;&lt;/span&gt;;
    output += retn.Name + &lt;span class=&quot;string&quot;&gt;&quot;\t\t\r\n&quot;&lt;/span&gt;;
    output += retn.Text + &lt;span class=&quot;string&quot;&gt;&quot;\t\t\r\n&quot;&lt;/span&gt;;
    output += retn.VoteCount.ToString() + &lt;span class=&quot;string&quot;&gt;&quot;\t\t\r\n&quot;&lt;/span&gt;;
    output += retn.VoteTotal.ToString() + &lt;span class=&quot;string&quot;&gt;&quot;\t\t\r\n&quot;&lt;/span&gt;;
    &lt;span class=&quot;class_def&quot;&gt;MessageBox&lt;/span&gt;.Show(output);
}&lt;/pre&gt;
Compiled, ran and clicked.&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;big_image&quot;&gt;&lt;img src=&quot;/images/mysoapdiagresults.gif&quot; border=&quot;0&quot; alt=&quot;The image of the message box that was returned when I clicked the button.&quot; /&gt;&lt;/div&gt;
&lt;br /&gt;
Simple, Easy, Can't believe I never tried it before now!&lt;br /&gt;
&lt;br /&gt;
This opened up a whole new world of opportunities in cross-platform distributed computing that I'd never exposed myself to before... what a wonderful thing!
&lt;br /&gt;</description>
      <pubDate>Mon, 03 Jul 2006 02:17:59 EDT</pubDate>
      <link>http://sanity-free.org/125/php_webservices_and_csharp_dotnet_soap_clients.html</link>
    </item>
        <item>
      <title>Dependency Scanner (for .NET 1.1)</title>
      <description>&lt;a href=&quot;/Samples/DependencyResolver.zip&quot; title=&quot;Dependency Resolver project and source code&quot;&gt;&lt;img src=&quot;images/download.gif&quot; alt=&quot;download&quot; border=&quot;0&quot; align=&quot;left&quot; /&gt;&amp;nbsp;Download Project &amp;amp; Source Code&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;div class=&quot;big_image&quot;&gt;&lt;img src=&quot;/images/dependency_scanner.png&quot; alt=&quot;.NET Dependency Resolver for .NET 1.1&quot; /&gt;&lt;/div&gt;
&lt;br /&gt;
Here's a little tool I wrote a while ago.  Right now its only for .NET 1.1, but I'm working on a port for 2.0 (which should look a lot nicer).&lt;br /&gt;
&lt;br /&gt;
When I wrote this, I was dealing with assemblies that had very complex inner-dependencies.  Some assemblies were private, others were shared, and I ended up having to have a ton of ildasm.exe's open to the assembly manafest to try to see which assembly had referenced which assembly and had to verify that the assembly version referenced was correct.  On top of that I needed to verify that assemblies that were referenced--that had policy files--were being redirected to the correct versions.&lt;br /&gt;
&lt;br /&gt;
Basically all this tool does, is load an assembly, display its version, and lists any referenced assemblies, their versions, and whether or not the reference version resolves to any other version.  It doesn't look like much, but its saved me a lot of time and head ache.  It also has a copy / paste feature that can copy selected listview data to the clipboard in a nice column formatted manner.&lt;br /&gt;
&lt;br /&gt;
&lt;!-- readmore --&gt;
Here's the main bulk of whats going on.  Basically what I did, was to load the assembly file, then try to load another assembly based on the partial name from the assembly file that I just loaded.  At first glance, it looks as if I'm creating 2 identical assembly objects.  The difference is, that if one assembly was version 1.1.2 and there was a policy file for version 1.1.2 to redirect to version 1.2.3, the second assembly load would load version 1.2.3 instead of version 1.1.2.  That info would be displayed in the top line of the assembly entry&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;code&quot;&gt;
&lt;span class=&quot;keyword&quot;&gt;private void&lt;/span&gt; ResolveAssemblyImports(&lt;span class=&quot;keyword&quot;&gt;string&lt;/span&gt; file) {
    &lt;span class=&quot;keyword&quot;&gt;try&lt;/span&gt; {
        Assembly asm = Assembly.LoadFile(file);
        Assembly d = Assembly.LoadWithPartialName(asm.FullName);
&lt;/pre&gt;
Then I matched the two assemblies version numbers to see if they matched
&lt;pre class=&quot;code&quot;&gt;
        &lt;span class=&quot;keyword&quot;&gt;string&lt;/span&gt; ver = &lt;span class=&quot;string&quot;&gt;&quot;&quot;&lt;/span&gt;, re = &lt;span class=&quot;string&quot;&gt;&quot;&quot;&lt;/span&gt;;
        &lt;span class=&quot;keyword&quot;&gt;bool&lt;/span&gt; nill = d == &lt;span class=&quot;keyword&quot;&gt;null&lt;/span&gt;;
        &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt;(!nill) {
            re = _r.Match(d.FullName).Groups[&lt;span class=&quot;string&quot;&gt;&quot;version&quot;&lt;/span&gt;].Value;
        } &lt;span class=&quot;keyword&quot;&gt;else&lt;/span&gt; {
            re = &lt;span class=&quot;string&quot;&gt;&quot;NULL&quot;&lt;/span&gt;;
        }
        ver = _r.Match(asm.FullName).Groups[&lt;span class=&quot;string&quot;&gt;&quot;version&quot;&lt;/span&gt;].Value;
        &lt;span class=&quot;keyword&quot;&gt;bool&lt;/span&gt; r_1 = &lt;span class=&quot;keyword&quot;&gt;false&lt;/span&gt;;
        &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt;(ver != re &amp;&amp; re != &lt;span class=&quot;string&quot;&gt;&quot;NULL&quot;&lt;/span&gt;) {
            Version v_1 = new Version(ver);
            Version v_2 = new Version(re);
            &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt;(v_2 &gt;= v_1) r_1 = true;
        }
        AddItem(asm.GetName().Name, &lt;span class=&quot;string&quot;&gt;&quot;----&quot;&lt;/span&gt;, ver, re, &lt;span class=&quot;string&quot;&gt;&quot;True&quot;&lt;/span&gt;, nill ? &lt;span class=&quot;string&quot;&gt;&quot;Failed&quot;&lt;/span&gt; : (r_1 ? &lt;span class=&quot;string&quot;&gt;&quot;True&quot;&lt;/span&gt; : &lt;span class=&quot;string&quot;&gt;&quot;False&quot;&lt;/span&gt;));
&lt;/pre&gt;
Then I iterate through the assembly's referenced assemblies and try to do the same thing (match version numbers &amp;amp; see if they resolve to the same version)
&lt;pre class=&quot;code&quot;&gt;
        &lt;span class=&quot;keyword&quot;&gt;foreach&lt;/span&gt;(AssemblyName asmName &lt;span class=&quot;keyword&quot;&gt;in&lt;/span&gt; asm.GetReferencedAssemblies()) {
            &lt;span class=&quot;keyword&quot;&gt;string&lt;/span&gt; v1 = _r.Match(asmName.FullName).Groups[&lt;span class=&quot;string&quot;&gt;&quot;version&quot;&lt;/span&gt;].Value,
                v2 = v1;
            &lt;span class=&quot;keyword&quot;&gt;bool&lt;/span&gt; exist = false, res = &lt;span class=&quot;keyword&quot;&gt;false&lt;/span&gt;;
            Assembly dep;
            &lt;span class=&quot;keyword&quot;&gt;try&lt;/span&gt; {
                dep = Assembly.LoadWithPartialName(asmName.FullName);
                &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt;(dep != &lt;span class=&quot;keyword&quot;&gt;null&lt;/span&gt;) {
                    &lt;span class=&quot;keyword&quot;&gt;string&lt;/span&gt; temp = _r.Match(dep.FullName).Groups[&lt;span class=&quot;string&quot;&gt;&quot;version&quot;&lt;/span&gt;].Value;
                    exist = &lt;span class=&quot;keyword&quot;&gt;true&lt;/span&gt;;
                    &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt;(v2 != temp) {
                        Version v = new Version(v2);
                        Version t = new Version(temp);
                        &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt;(t &gt;= v) res = &lt;span class=&quot;keyword&quot;&gt;true&lt;/span&gt;;
                    }
                }
            } &lt;span class=&quot;keyword&quot;&gt;finally&lt;/span&gt; {
                AddItem(&lt;span class=&quot;string&quot;&gt;&quot;    +----&quot;&lt;/span&gt;, asmName.Name, v1, v2, exist.ToString(), res.ToString());
            }
        }
    } &lt;span class=&quot;keyword&quot;&gt;catch&lt;/span&gt; {
    }
}
&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
Then I finally print out the results of my findings to the listview.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
I had forgotten about this app until today when I couldn't understand why I kept getting an error for a method that had been updated in the assembly I was referencing.  I pulled this tool out and saw that it indeed referenced the correct assembly but because another assembly being used referenced an older version of that assembly, the local copy was using the newer version but the older version was in the GAC so my code was using the older assembly that didnt have the current code that I needed (confused... I am and I was there).&lt;br /&gt;
&lt;br /&gt;
Anyway, &lt;a href=&quot;Samples/DependencyResolver.zip&quot; title=&quot;Dependency Resolver project and source code&quot;&gt;here's the project / source code&lt;/a&gt; for the app.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Update 7/16/2009&lt;/b&gt; I've converted this app over to .NET 3.5 and complied app binaries that can be downloaded &lt;a href=&quot;Samples/DependencyResolver-binary-.net3.5.zip&quot;&gt;here&lt;/a&gt;</description>
      <pubDate>Thu, 23 Mar 2006 03:12:29 EST</pubDate>
      <link>http://sanity-free.org/122/dependency_scanner_for_dotnet_1_1.html</link>
    </item>
        <item>
      <title>Visual Studio .NET 2005 Troubleshooting Tool</title>
      <description>I was having some troubles with the final release visual studio Team System.  I've been using VS.NET 2005 on my home computer since the first previews were available on MSDN.  I had installed and uninstalled each release of the IDE.  I was experiencing problems viewing some of the docking window contents (entire panels loosing their contents, and were not able to be closed).  I found &lt;a href=&quot;http://astebner.sts.winisp.net/Tools/ttool.zip&quot; title=&quot;Visual Studio .NET 2005 Troubleshooting Tool&quot; target=&quot;_blank&quot;&gt;A trouble shooting tool&lt;/a&gt; on &lt;a href=&quot;http://blogs.msdn.com/astebner/archive/2005/11/18/494735.aspx&quot; title=&quot;Aaron Stebner's blog describing some problems that this tool will fix&quot; target=&quot;_blank&quot;&gt;Aaron Stebner's blog&lt;/a&gt;.  I ran it, it found an orphaned beta assembly that was causing all the problems, and now I'm up and running without any problems...&lt;br&gt;
&lt;br&gt;
Very Nice!</description>
      <pubDate>Fri, 02 Dec 2005 01:16:30 EST</pubDate>
      <link>http://sanity-free.org/118/visual_studio_dotnet_2005_troubleshooting_tool.html</link>
    </item>
        <item>
      <title>A Battlefield 2 Rank Avatar Script in PHP</title>
      <description>This is a little departure from my typical topic.&lt;br&gt;
&lt;br&gt;
To cut a rather tedious and boring story short, I play &lt;a href=&quot;http://www.eagames.com/official/battlefield/battlefield2/us/home.jsp&quot; target=&quot;_blank&quot;&gt;Battlefield 2&lt;/a&gt;, which has a cool stats service that folks have been hacking with outside of the game (the game has a stats board built in, which stats board uses some pseudo web-services to obtain game statistic info).&lt;br&gt;
&lt;br&gt;
There are a multitude of server hosts out there that generate dynamic signature images with your game stats in them, so that you can look cool and show off how much time you actually waste online gaming, on every forum you frequent.&lt;br&gt;
&lt;br&gt;
I decided to join the masses and write a script that pulls my users rank and score from the stats servers and returns an image that can be used as an avatar on most bulletin board systems out there. I had a size restriction on the server I wanted it for, so the images produced are 80x80 and less than 6k in size.&lt;br&gt;
&lt;br&gt;
Using the avatar script is pretty easy, you direct the image source url to the server where you install the script (or you can just use the one here)&lt;br&gt;
&lt;br&gt;
&lt;pre class=&quot;code&quot;&gt;
&lt;b&gt;html&lt;/b&gt;
&amp;lt;img src=&amp;quot;http://www.sanity-free.org/bf2rank/?pid=45647367&amp;quot;&amp;gt;

&lt;b&gt;bbCode&lt;/b&gt;
[img]http://www.sanity-free.org/bf2rank/?nick=[OinK]_MadHatter[/img]

&lt;/pre&gt;&lt;br&gt;
&lt;br&gt;
most bulliten board systems have places for you to link in a remote avatar, and one would add this same url w/ a name just as you would a normal image.&lt;br&gt;
&lt;br&gt;
One annoying thing about most bbcode systems is that they require a valid image extension (.png, .jpg, .gif ... ) on the url inside the [img][/img] blocks (otherwise it just shows text inside the img blocks).  To get your rank image to show up if you're having troubles is to simply add an &amp;amp;avatar.jpg to the end of the url.  The php code will ignore it, and the forum will think its a valid image and will show it correctly.  Another way is to use the simple url described in the forums &lt;a href=&quot;http://www.sanity-free.org/forum/viewtopic.php?id=6&quot;&gt;here&lt;/a&gt;.&lt;br&gt;
&lt;br&gt;
The resulting image looks like this:&lt;br&gt;
&lt;br&gt;
&lt;img src=&quot;http://www.sanity-free.org/bf2rank/?pid=45647367&quot; alt=&quot;MadHatter's Battlefield 2 Rank Avatar&quot;&gt;&lt;br&gt;
&lt;br&gt;

You can also specify a couple of options.  
&lt;ul&gt;
&lt;li&gt; adding &lt;b&gt;&amp;amp;score&lt;/b&gt; will display your global score at the top of the image. &lt;br&gt; ex: &lt;img src=&quot;http://www.sanity-free.org/bf2rank/?nick=[OinK]_MadHatter&amp;score&quot; alt=&quot;&amp;amp;score Example&quot;&gt;&lt;/li&gt;
&lt;li&gt; adding &lt;b&gt;&amp;amp;no_text&lt;/b&gt; to the end of the url will keep the text from 
being written on the image. &lt;br&gt;ex:&lt;img src=&quot;http://www.sanity-free.org/bf2rank/?nick=[OinK]_MadHatter&amp;no_text&quot; alt=&quot;&amp;amp;no_text Example&quot;&gt;&lt;/li&gt;
&lt;li&gt; adding &lt;b&gt;&amp;amp;delta&lt;/b&gt; to the end will show the difference in your progress between your last rank and your next one. &lt;br&gt;ex: &lt;img src=&quot;http://www.sanity-free.org/bf2rank/?nick=[OinK]_MadHatter&amp;delta&quot; alt=&quot;&amp;amp;delta example&quot;&gt;&lt;/li&gt;
&lt;li&gt; you can also combine any combination of them as well (this one is using &amp;amp;delta, &amp;amp;no_text, and &amp;amp;score options).&lt;br&gt; ex: &lt;img src=&quot;http://www.sanity-free.org/bf2rank/?pid=45647367&amp;delta&amp;no_text&amp;score&quot; alt=&quot;&amp;amp;delta and &amp;amp;no_text and &amp;ampscore options&quot;&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;b&gt;EDIT Feb. 15 2006: since the last patch, I havent updated this article source code.  &lt;a href=&quot;/bf2rank/code.txt&quot;&gt;HERE'S&lt;/a&gt; the most recent version of the script. you can download the package and copy that over the script provided.&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;EDIT March 20 2006: I updated the code to include nick name resolution (using searchforplayers.aspx). the link to the code above has that change in it. so you can use your player nick again instead of trying to find out your player ID.&lt;/b&gt;&lt;br/&gt;
&lt;br/&gt;
&lt;b&gt;EDIT June 30 2006: I removed the source code from the article and repacked the download with everything you need.&lt;/b&gt;&lt;br /&gt;
&lt;br&gt;
&lt;b&gt;EDIT July 18 2006: I've added &quot;easy url's&quot; for the rank avatars.  for more info on that--and if you'd like to add them to your server--take a look at the thread in the forums &lt;a href=&quot;http://www.sanity-free.org/forum/viewtopic.php?id=6&quot;&gt;here&lt;/a&gt;.&lt;/b&gt;
&lt;br&gt;
&lt;br&gt;
Here's the code for the script (the &lt;a href=&quot;http://www.sanity-free.org/binary/bf2rank.zip&quot;&gt;download&lt;/a&gt; has a true type font that needs to be located in the script dir w/ all the image files which are also referenced, so make sure to download the script pack):&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;a href=&quot;http://www.sanity-free.org/binary/bf2rank.zip&quot; target=&quot;_blank&quot; title=&quot;Battlefield 2 Avatar Script&quot;&gt;&lt;img src=&quot;images/download.gif&quot; border=&quot;0&quot; align=&quot;left&quot; /&gt; Download source and resources here.&lt;/a&gt;  &lt;br&gt;
&lt;br /&gt;</description>
      <pubDate>Wed, 21 Sep 2005 01:29:34 EDT</pubDate>
      <link>http://sanity-free.org/116/battlefield_rank_avatar_script_in_php.html</link>
    </item>
        <item>
      <title>Stupid .NET 1.1 Service Pack 1</title>
      <description>Service pack 1 strikes again.&lt;br&gt;
&lt;br /&gt;
&lt;pre class=&quot;code&quot;&gt;
    &lt;span class=&quot;keyword&quot;&gt;using&lt;/span&gt; System;
    &lt;span class=&quot;keyword&quot;&gt;using&lt;/span&gt; System.Drawing;
    &lt;span class=&quot;keyword&quot;&gt;using&lt;/span&gt; System.IO;
    &lt;span class=&quot;keyword&quot;&gt;using&lt;/span&gt; System.Reflection;
    &lt;span class=&quot;keyword&quot;&gt;public class&lt;/span&gt; Resources {
        private&lt;/span&gt; Resources() {}
        &lt;span class=&quot;keyword&quot;&gt;public static&lt;/span&gt; Image LoadImageFromResource(&lt;span class=&quot;keyword&quot;&gt;string&lt;/span&gt; path) {
            Image i = &lt;span class=&quot;keyword&quot;&gt;null&lt;/span&gt;;
            &lt;span class=&quot;keyword&quot;&gt;using&lt;/span&gt; ( Stream s = 
                      Assembly.GetExecutingAssembly().GetManifestResourceStream(path) ) {
                &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt;(s != &lt;span class=&quot;keyword&quot;&gt;null&lt;/span&gt;) i = Image.FromStream(s, &lt;span class=&quot;keyword&quot;&gt;true&lt;/span&gt;, &lt;span class=&quot;keyword&quot;&gt;true&lt;/span&gt;);
            }
            &lt;span class=&quot;keyword&quot;&gt;return&lt;/span&gt; i;
        }
    }
&lt;/pre&gt;
This is part of a general use utility that I frequently use.  An odd bug that came out of this is that the overloaded method&lt;br /&gt;
&lt;br /&gt;
Image.FromStream(Stream, Boolean, Boolean)&lt;br /&gt;
&lt;br /&gt;
was added in Service Pack 1 for .net v1.1 &lt;b&gt;BUT NEVER DOCUMENTED!!!&lt;/b&gt;.  Well, ok, that's not entirely true, It is documented... in v2.0 documentation...  So if  you wrote this in 1.1sp1 and deploy it to a machine with out sp1 you'll get a method not found exception when trying to load the image from stream using the above overload--non sp1 only has FromStream(Stream, Boolean)&lt;br /&gt;
&lt;br /&gt;
Nothing like being incompatible with the same version of the framework eh?</description>
      <pubDate>Tue, 06 Sep 2005 19:24:32 EDT</pubDate>
      <link>http://sanity-free.org/115/stupid_dotnet_1_1_service_pack.html</link>
    </item>
      </channel>
</rss>