<rss version="0.92">
  <channel>
    <title>Sanity Free  dot   org</title>
    <link>http://www.sanity-free.org/</link>
    <description>Madness in template form</description>
    <language>en</language>
        <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;

By having a named mutex allows us to stack synchronization across 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 message to every window (that I had previously 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;) 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>Fun at the office</title>
      <description>The windows in our office building (the windows I sit right in front of) look directly out over Campbell road in Richardson.  Its a fairly busy road (three lanes on each side).  The company relocated to this office space around March of last year.  Since that time we've seen people run into light poles, get in a few finder binders, and come really close to smashing each other to bits, but today took the cake.&lt;br /&gt;
&lt;br /&gt;
Around 5:30 or 6:00 PM people are quietly working away trying to wrap things up, or playing a round of pinball at the office pinball machine, when all of the sudden you hear a really loud BOOM out on the street.  There was no screeching tires, no horns honking, just a loud thunderous crash sound.  Our office is one big room, everybody sits at tables looking at everyone else, and we all popped up to take a look outside.&lt;br /&gt;
&lt;br /&gt;
All three lanes headed east are bumper to bumber.  There seems to be four or five cars seemingly fused together. There's a large SUV (I'm guessing a ford explorer) missing most of its &amp;quot;what used to be a hood / engine area of the car&amp;quot;, the guy inside is hidden behind a wall of air bags. Four cars seemingly fused together (at first it looked like much more because of all the traffic) end to end.&lt;br /&gt;
&lt;br /&gt;The poor sod at the back was the red SUV.  He had plowed into the back end of a huge suburban, who had then managed to get up under the bumper of the buick sedan in front of her, who then ate the back end of a nissan.&lt;br /&gt;
&lt;br /&gt;The gal in front faired the best with what looked like a banged up rear bumper.&lt;br /&gt;
&lt;br /&gt;The buick sedan who was next was firmly supplanted on the hood of the suburban looked to be done in.&lt;br /&gt;
&lt;br /&gt;The suburban looked pretty much unscathed except for the transmission fluid which was now starting to leak from the car at an alarming rate.  It had taken the brunt of the (assumingly) 45 MPH impact of the SUV.  I can only imagine what would have happened to that poor buick had the suburban not been between them.  The force of the impact was so strong that it had smashed the Suburban's transmission causing what looked like a large pool of blood (transmission fluid is red) to seep from beneath it.&lt;br /&gt;
&lt;br /&gt;
The people involved started to get out of their vehicles (except the guy in the SUV) and try to figure out what the heck just happened.  It was a while before anyone made any calls on their mobile phones (like to call the police), but the Richardson police have a pretty heavy presence on that road and it wasn't long before there were teams of cops blocking off traffic and getting things sorted.&lt;br /&gt;
&lt;br /&gt;
That pretty much killed the rest of our work day, and provided us with some (albeit morbid) entertainment.&lt;br /&gt;
&lt;br /&gt;
I snapped a photo with my phone (which totally doesn't do it justice, but its a crappy RAZR and the shots where I zoomed in, ended up actually just being cropped pics of this one).&lt;br /&gt;
&lt;br /&gt;
&lt;img src=&quot;/misc/wreck.jpg&quot; alt=&quot;4 car pileup outside the office&quot; class=&quot;big_image&quot;/&gt;&lt;br /&gt;
&lt;br /&gt;</description>
      <pubDate>Fri, 20 Apr 2007 00:24:49 EDT</pubDate>
      <link>http://sanity-free.org/142/fun_at_the_office.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;(temp == null) &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;(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;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;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;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>Vista</title>
      <description>&lt;img src=&quot;images/vista_icon.png&quot; align=&quot;left&quot; style=&quot;padding-right: 8px;padding-bottom:8px;&quot; /&gt;I've needed a new monitor for a while now, but have been putting it off... My current monitor is getting so hard to read so I decided it was finally time to break down and get a new one.  With that purchase I decided I may as well pick up vista, so I did.  We're making the upgrade at work later this week too, so I figured I'd push it at home as well.&lt;br /&gt;
&lt;br /&gt;
Tonight I was looking around at the vista section of microsoft.com, and I noticed a chart that described the upgrade path for various operating systems migrating to vista.  Wouldn't you know it, xp x64's (the OS I run here at home) upgrade path is a &lt;a href=&quot;http://www.microsoft.com/windows/products/windowsvista/buyorupgrade/upgradepaths.mspx&quot; target=&quot;_blank&quot;&gt;&amp;quot;clean install&amp;quot;&lt;/a&gt;&lt;br/&gt;
&lt;br/&gt;
Man I just love microsoft... I may as well &amp;quot;upgrade&amp;quot; to a mac!  What a bunch of crap!</description>
      <pubDate>Wed, 21 Feb 2007 01:57:54 EST</pubDate>
      <link>http://sanity-free.org/139/vista.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>my new years resolution</title>
      <description>Well, for the longest time I resolved every new year, never to make a new years resolution.  That was very successful for quite some time, so I have a nearly perfect record.  This year, I decided I was getting way to inactive, so I decided to buy a membership to a gym.&lt;br /&gt;
&lt;br /&gt;
When I was in high school, I was on the swim and diving team.  I'd typically warm up by swimming 500 meters, then do various types of strokes / kicks for about 2000 meters, then warm down with another 500 meters.  I signed up at a particular gym because there was a facility near my work and home, and they both had pools.  I figured I could swim at lunch from work, and after work near home.&lt;br /&gt;
&lt;br /&gt;
Last night I swam for about an hour (probably closer to 45 minutes) and after 100 meters, my arms felt like they were (jelly, and) about to fall off.  It took me the rest of that hours time to finish swimming something close to a 500 meters... It was after the 100 meters when my arms lost all strength that I realized how far I'd really gone.  All the pictures of me looking like a whale had somehow gotten past my &quot;fat&quot; radar, but my first 100 meters in the pool caused a huge blip.  That night when I got home and had to do two breathing treatments for my asthma because it was such a shock to my system.  I got home around 8 pm and it wasn't till about 10 that my heart rate got back to normal.&lt;br /&gt;
&lt;br /&gt;
This afternoon I went at it again.  This time I was able to swim much longer without folding up.  Minutes after I got out of the pool my heart rate went back to normal, and my asthma didn't act up.  So hopefully it gets easier as time goes on.&lt;br /&gt;
&lt;br /&gt;
</description>
      <pubDate>Wed, 17 Jan 2007 17:07:32 EST</pubDate>
      <link>http://sanity-free.org/136/my_new_years_resolution.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 new byte&lt;/span&gt;[] { (&lt;span class=&quot;keyword&quot;&gt;byte&lt;/span&gt;)(crc &amp;gt;&amp;gt; 8), (&lt;span class=&quot;keyword&quot;&gt;byte&lt;/span&gt;)(crc &amp;amp; 0x00ff) };
    }

    &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;http://www.sanity-free.org/133/crc_16_ccitt_in_csharp.html&quot;&gt;CRC16-CCITT&lt;/a&gt;, &lt;a href=&quot;http://www.sanity-free.org/12/crc32_implementation_in_csharp.html&quot;&gt;CRC32&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 new byte&lt;/span&gt;[] { (&lt;span class=&quot;keyword&quot;&gt;byte&lt;/span&gt;)(crc &amp;gt;&amp;gt; 8), (&lt;span class=&quot;keyword&quot;&gt;byte&lt;/span&gt;)(crc &amp;amp; 0x00ff) };
    }

    &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;http://www.sanity-free.org/12/crc32_implementation_in_csharp.html&quot;&gt;CRC32&lt;/a&gt;, &lt;a href=&quot;http://www.sanity-free.org/134/standard_crc_16_in_csharp.html&quot;&gt;CRC16&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>battlefield 2142</title>
      <description>I took the dive (got a kitchen pass from the wife) and pre-ordered Battlefield 2142 yesterday!  I cant wait, 19 some odd days left (from when I wrote this).  I guess since I'll download it and play it the moment its released, I should just go ahead and take the day off to play it all day... (I can always dream anyway).&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;big_image&quot;&gt;&lt;img src=&quot;/misc/bf2142.png&quot; class=&quot;big_image&quot; border=&quot;0&quot; alt=&quot;Screen shot of my EA Downloader account&quot; /&gt;&lt;/div&gt;</description>
      <pubDate>Wed, 20 Sep 2006 15:03:23 EDT</pubDate>
      <link>http://sanity-free.org/130/battlefield_2142.html</link>
    </item>
        <item>
      <title>Surreal!</title>
      <description>We've been in the middle of the biggest drought Dallas has seen in a long time. Every day has over 100&amp;deg;F and my asthma's been killing me. My water bill has been only a little bit less expensive my mortgage payment lately, and every time it rains (uhm, I mean both times it's rained, [this summer]), the rain seems to hover about 2 feet off the ground just before it runs off down the gutter.  It's so dry my boss recently lost a golf ball down a crack in the ground at the golf course (how would you score that btw)...  I think you get the point.&lt;br /&gt;
&lt;br /&gt;
This afternoon my son wanted to go over and play at my wife's parents house (who live directly behind us), so he and my wife took the long trek across the alley to go play.  Just after they left, it started &quot;coming up a flood&quot; as they say here in Texas (which loosley translates to a &quot;torrential downpour&quot;).  After a while I went over with an umbrella so they could come home with out getting soaked.  While we were over talking w/ my father in law, it thundered (and lightininged how ever you'd say that, at the same time) the loudest I've ever heard it.  We laughed and said how close that one was because there was no pause between the flash and the boom. &lt;br /&gt;
&lt;br /&gt;
As we walked across the alley back to our house, we smelled wood burning (this seemed odd for such a hot afternoon).  Looking down the alleyway we could actually see smoke low to the ground like fog in the early morning.  My wife took our son back in our house as I ran down our street to see if I could see what was burning.  As I walked down the street, the smoke grew thicker and thicker.  Some of my neighbors were out, and we all started running towards the smoke (joking about how much help we would all be: wearing our rain soaked &quot;flip-flops&quot;).&lt;br /&gt;
&lt;br /&gt;
That lightning we'd heard had actually struck our neighbors house!  There was dark smoke billowing out of their attic vent, and a few people were standing out in front of the house across the street from the one burning.  The fire department was just pulling up, and you could see fire starting to peek out of the roof of their house.  It didn't seem like much at first.  The firemen quickly (though it seemed like forever) got hoses hooked up and went into the house.  All of the sudden the vent where we first started seeing smoke pouring out, erupted into a big ball of flames.  The fireball was literally inches from the neighbor's house.&lt;br /&gt;
&lt;br /&gt;
Minutes later what was a tiny hole in the roof of the house had turned into a big black void where the roof of the attic used to be.  The entire roof had flames shooting up 15 to 20 feet from the top of what used to be the crest of the house.  The whole scene was surreal.&lt;br /&gt;
&lt;br /&gt;
As I watched--in horror--my neighbors house burn, I couldn't help but think that this could very well be my house disintegrating before my eyes.  We do, after all, have the tallest house on the block.  At one point the ceiling in the master bedroom fell through, and you could see the fire burning uncontrolled through the bedroom window like the glass of a fireplace shielding the raging fire that you typically build to keep warm in the dead of winter.  I felt so sad for these people though I didn't know them from Adam.  I can't imagine what it must have felt like to so helplessly watch everything in your life go up in flames.  They had escaped from their home with their lives... and nothing else.  That's a pretty harsh reality to come to terms with.  I still find it very difficult to go to sleep thinking about it.  Everything smells of smoke (the damp night air has a lot to do with it I'm sure), and serves as a constant reminder of just how fortunate we were today.  Fifteen minutes was all it took for that house to completely burn to the ground.  &lt;br /&gt;
&lt;br /&gt;
It was the longest fifteen minutes of my life.&lt;br /&gt;
&lt;br /&gt;
God help us all.&lt;br /&gt;
&lt;br /&gt;</description>
      <pubDate>Mon, 28 Aug 2006 01:43:11 EDT</pubDate>
      <link>http://sanity-free.org/129/surreal.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;sp