<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Shut Up and Hack</title>
	<atom:link href="http://www.aaronmbrown.net/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.aaronmbrown.net/blog</link>
	<description></description>
	<lastBuildDate>Tue, 27 Jul 2010 21:52:12 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Protip</title>
		<link>http://www.aaronmbrown.net/blog/2010/07/protip/</link>
		<comments>http://www.aaronmbrown.net/blog/2010/07/protip/#comments</comments>
		<pubDate>Tue, 27 Jul 2010 21:52:12 +0000</pubDate>
		<dc:creator>AMB</dc:creator>
				<category><![CDATA[theory]]></category>

		<guid isPermaLink="false">http://www.aaronmbrown.net/blog/?p=46</guid>
		<description><![CDATA[Never require the presence of a variable which isn&#8217;t used in your code.
I would think this would go without saying, but I just lost most of a day trying to figure out why a web service wasn&#8217;t working.  One of the things I&#8217;d done was cleaned up some old code, including removing a hardcoded ]]></description>
			<content:encoded><![CDATA[<p><strong>Never require the presence of a variable which isn&#8217;t used in your code.</strong></p>
<p>I would think this would go without saying, but I just lost most of a day trying to figure out why a web service wasn&#8217;t working.  One of the things I&#8217;d done was cleaned up some old code, including removing a hardcoded HTTP header value.  The header it referenced wasn&#8217;t consumed anywhere in my code, and a quick smoke test indicated that, no matter what the value was set to, the client services behaved nicely.  So I assumed (correctly, as it turns out) that the variable wasn&#8217;t used in locally or in any system in which we integrate, so I cut the reference out.</p>
<p>Rolled up the build, sent it off, moved on to other things.</p>
<p>QA failed the build, because one of the integration use cases was broken.</p>
<p>Turns out that unless that header is present, one of the methods in a client web service dies.  It doesn&#8217;t matter what the value is, but it has to be there, or the web method chokes.</p>
<p>So, moral of the story: make sure that all your variables are really needed.  Make sure they they mean something and cut out any variables which aren&#8217;t required.  This is especially true with integration situations like those found in web methods.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.aaronmbrown.net/blog/2010/07/protip/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Eclipse can&#8217;t find aapt.exe in Android SDK tools folder</title>
		<link>http://www.aaronmbrown.net/blog/2010/07/eclipse-cant-find-aapt-exe-in-android-sdk-tools-folder/</link>
		<comments>http://www.aaronmbrown.net/blog/2010/07/eclipse-cant-find-aapt-exe-in-android-sdk-tools-folder/#comments</comments>
		<pubDate>Mon, 26 Jul 2010 04:27:31 +0000</pubDate>
		<dc:creator>AMB</dc:creator>
				<category><![CDATA[android]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://www.aaronmbrown.net/blog/?p=45</guid>
		<description><![CDATA[So I&#8217;ve lost the original error message, but I ran into an issue setting up the Android SDK in Eclipse.  The error message was something to the effect that Eclipse couldn&#8217;t find &#8220;aapt.exe&#8221; in the version-specific SDK tools folder.  
I browsed to the folder specified and confirmed that the executable was there.  ]]></description>
			<content:encoded><![CDATA[<p>So I&#8217;ve lost the original error message, but I ran into an issue setting up the Android SDK in Eclipse.  The error message was something to the effect that Eclipse couldn&#8217;t find &#8220;aapt.exe&#8221; in the version-specific SDK tools folder.  </p>
<p>I browsed to the folder specified and confirmed that the executable was there.  I checked my path to the Android SDK and it was correct.</p>
<p>After futzing around for a little while I instructed Eclipse to clean all projects in the solution.  As soon as I did that, my firewall (COMODO) popped up a warning that Eclipse was trying to access aapt.exe, which was an unknown executable.  I selected &#8220;allow&#8221; and, after Eclipse cleaned and rebuilt the project, the errors went away.</p>
<p>So, if you run COMODO and you get funky errors from Eclipse about not finding SDK components, clean your solution.  If that fails, check your firewall settings to make sure that those executables aren&#8217;t being blocked.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.aaronmbrown.net/blog/2010/07/eclipse-cant-find-aapt-exe-in-android-sdk-tools-folder/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Intro to Android Hacking</title>
		<link>http://www.aaronmbrown.net/blog/2010/07/intro-to-android-hacking/</link>
		<comments>http://www.aaronmbrown.net/blog/2010/07/intro-to-android-hacking/#comments</comments>
		<pubDate>Sun, 18 Jul 2010 02:29:32 +0000</pubDate>
		<dc:creator>AMB</dc:creator>
				<category><![CDATA[hackety hack]]></category>

		<guid isPermaLink="false">http://www.aaronmbrown.net/blog/?p=44</guid>
		<description><![CDATA[The excellent Hack A Day is running a series of articles on programming for the Android OS.  The first two sections (an intro to the series and a &#8220;Hello, World!&#8221; walk through) are up already, with more on the way.  They are very easy to follow, and give you everything you need to ]]></description>
			<content:encoded><![CDATA[<p>The excellent <a href="http://hackaday.com/">Hack A Day</a> is running a series of articles on programming for the Android OS.  The first two sections (an intro to the series and a &#8220;Hello, World!&#8221; walk through) are up already, with more on the way.  They are very easy to follow, and give you everything you need to get started with Android development.</p>
<p>It&#8217;s definitely shaping up to be a great resource.</p>
<p><a href="http://hackaday.com/2010/07/12/android-development-101-%E2%80%93-a-tutorial-series/">Article 0 &#8211; Intro</a></p>
<p><a href="http://hackaday.com/2010/07/15/android-dev-101-%E2%80%93-part-1hello-world/">Article 1 &#8211; Hello, World</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.aaronmbrown.net/blog/2010/07/intro-to-android-hacking/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sugru for You!</title>
		<link>http://www.aaronmbrown.net/blog/2010/07/sugru-for-you/</link>
		<comments>http://www.aaronmbrown.net/blog/2010/07/sugru-for-you/#comments</comments>
		<pubDate>Wed, 14 Jul 2010 06:54:57 +0000</pubDate>
		<dc:creator>AMB</dc:creator>
				<category><![CDATA[hackety hack]]></category>
		<category><![CDATA[ooh shiny]]></category>

		<guid isPermaLink="false">http://www.aaronmbrown.net/blog/?p=43</guid>
		<description><![CDATA[Cross-posted from my non-code blog.
Hey, just wanted to post a quick follow up to my post on Sugru.  Pete, in comments, points to the fact that it&#8217;s now on sale again.  I will definitely be getting some.  Hackers of all kinds and stripes should hie themselves to the Sugru site and do ]]></description>
			<content:encoded><![CDATA[<p><b>Cross-posted from <a href="http://thetarquin.com/BlagSwitch/">my non-code blog</a>.</b></p>
<p>Hey, just wanted to post a quick follow up to <a href="http://www.aaronmbrown.net/blog/2010/05/sugru/">my post on Sugru</a>.  Pete, in comments, points to the fact that it&#8217;s now on sale again.  I will definitely be getting some.  Hackers of all kinds and stripes should hie themselves to the <a href="http://sugru.com/">Sugru site</a> and do similarly.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.aaronmbrown.net/blog/2010/07/sugru-for-you/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Programmers, Flow, and Productivity</title>
		<link>http://www.aaronmbrown.net/blog/2010/07/programmers-flow-and-productivity/</link>
		<comments>http://www.aaronmbrown.net/blog/2010/07/programmers-flow-and-productivity/#comments</comments>
		<pubDate>Mon, 12 Jul 2010 05:31:19 +0000</pubDate>
		<dc:creator>AMB</dc:creator>
				<category><![CDATA[hackety hack]]></category>

		<guid isPermaLink="false">http://www.aaronmbrown.net/blog/?p=42</guid>
		<description><![CDATA[Giles Bowkett has a new video up over at his blog about programmers and flow.  I first read Flow when I took a class on the Philosophy of Happiness.  It&#8217;s interesting stuff and it basically reinforces the notion that people need some sort of challenge in order to be happy.  (See also ]]></description>
			<content:encoded><![CDATA[<p>Giles Bowkett has <a href="http://gilesbowkett.blogspot.com/2010/07/secrets-of-superstar-programmer.html">a new video</a> up over at <a href="http://gilesbowkett.blogspot.com/">his blog</a> about programmers and flow.  I first read <i>Flow</i> when I took a class on the Philosophy of Happiness.  It&#8217;s interesting stuff and it basically reinforces the notion that people need some sort of challenge in order to be happy.  (See also Viktor Frankl&#8217;s work on meaning.)</p>
<p>The video&#8217;s good, but it takes as given one thing that programmers all take for granted, but that many other people don&#8217;t understand: a 5-minute interruption costs us way more than 5-minutes worth of productivity.  It&#8217;s not simply that talking to someone for 5-minutes is 5-minutes less that we&#8217;re working, it&#8217;s that (and Giles mentioned this in passing at one point), it completely wrecks our train of thought, our focus, our motivation, our strategy, and our tactic.  They all get completely obliterated.  Building these back up takes time; often hours of it.</p>
<p>One other thing that he might have done well to mention is the fact that many of our interruptions (or at least mine) are interruptions that I generate myself.  I am habitually self-distracting, especially if the work that need to get done is boring.</p>
<p>The video is excellent, though, and highly recommended.  It&#8217;s a much-watch for anyone who is a programmer, works with programmers, manages programmers, etc.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.aaronmbrown.net/blog/2010/07/programmers-flow-and-productivity/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sugru</title>
		<link>http://www.aaronmbrown.net/blog/2010/05/sugru/</link>
		<comments>http://www.aaronmbrown.net/blog/2010/05/sugru/#comments</comments>
		<pubDate>Thu, 13 May 2010 04:33:33 +0000</pubDate>
		<dc:creator>AMB</dc:creator>
				<category><![CDATA[hackety hack]]></category>

		<guid isPermaLink="false">http://www.aaronmbrown.net/blog/?p=41</guid>
		<description><![CDATA[Cross-posted from my non-code blog.
This stuff looks crazy awesome.  It&#8217;s called Sugru and it&#8217;s a moldable, putty-like substance that dries into a high-strength silicone.  I love the idea, and I love that it&#8217;s being marketed precisely to people who like to hack things.  Alas, it&#8217;s sold out at the moment, or I&#8217;d ]]></description>
			<content:encoded><![CDATA[<p><b>Cross-posted from <a href="http://thetarquin.com/BlagSwitch/">my non-code blog</a>.</b></p>
<p><a href="http://sugru.com/">This stuff</a> looks crazy awesome.  It&#8217;s called Sugru and it&#8217;s a moldable, putty-like substance that dries into a high-strength silicone.  I love the idea, and I love that it&#8217;s being marketed precisely to people who like to hack things.  Alas, it&#8217;s sold out at the moment, or I&#8217;d be buying some right now.  I can think of at least a half dozen projects for which this would be perfect.  (E.g. custom-fitted, cushy silicone recoil pad!)</p>
<p>Can&#8217;t wait for them to get more in stock.</p>
<p>I love that this product explicitly combines two of the things that I think are poised to help revolutionize our culture and our world: the hacker ethos and material science.  Now maybe (read: definitely) this is starry-eyed utopianism on my part, but the more people who internalize the idea that they can and should hack their stuff, their world, and themselves, the better.  I think that being a hacker speaks to one of the primal elements that makes us human.  I think that a far better descriptor for us than &#8220;man the wise&#8221; (<i>homo sapiens</i>) is &#8220;man the hacker&#8221; (<i>homo textor</i>? <i>Homo abetor</i>?)</p>
<p>So now combine that hacker spirit with the fact that our fundamental understanding of how materials really work is just starting bear some really cool fruit, and the future begins to look pretty damned awesome.  See, we, as a species, moved from the stage of using ambient materials we found around us (flint), to reproducing materials that we first created by serendipity (bronze), to fine-tuning those serendipitous materials (high-carbon steel).  But it&#8217;s only recently that we&#8217;ve gotten to the point where we can actually <em>design</em> materials to achieve the properties we want.  Sugru is one such designed material.</p>
<p>So human-designed materials that are explicitly targeted to the hacker soul in each of us.  How awesome is that?  Rhetorical question.  The answer is, of course, &#8220;insanely&#8221;.  Better (non-rhetorical) question: when can I get my hands on some?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.aaronmbrown.net/blog/2010/05/sugru/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Fixing the Motorola Droid Battery Cover</title>
		<link>http://www.aaronmbrown.net/blog/2010/04/fixing-the-motorola-droid-battery-cover-2/</link>
		<comments>http://www.aaronmbrown.net/blog/2010/04/fixing-the-motorola-droid-battery-cover-2/#comments</comments>
		<pubDate>Tue, 20 Apr 2010 04:53:02 +0000</pubDate>
		<dc:creator>AMB</dc:creator>
				<category><![CDATA[gadgets]]></category>
		<category><![CDATA[hackety hack]]></category>

		<guid isPermaLink="false">http://www.aaronmbrown.net/blog/?p=38</guid>
		<description><![CDATA[Step 0: Own a Motorola Droid.
Step 1: Get kind of annoyed by the battery cover slipping off at inconvenient times.  Be sure not to get so annoyed that you actually take steps to fix the issue.
Step 2: Go drinking with some friends at your local on an idle Thursday night.
Step 3: During a heated ]]></description>
			<content:encoded><![CDATA[<p>Step 0: Own a Motorola Droid.<br />
Step 1: Get kind of annoyed by the battery cover slipping off at inconvenient times.  Be sure not to get so annoyed that you actually take steps to fix the issue.<br />
Step 2: Go drinking with some friends at your local on an idle Thursday night.<br />
Step 3: During a heated game of shuffleboard, accidentally slosh your beer on the pocket where you carry your phone.<br />
Step 4: Swear.<br />
Step 5: Wipe the phone off with a paper towel.<br />
Step 6: Swear some more.<br />
Step 7: Go back to playing shuffleboard.<br />
Step 8: Wake up the next morning, carefully clean the Droid&#8217;s screen and keyboard.<br />
Step 9: Notice after a few days that the battery cover is stuck shut with dried beer.</p>
<p>N.B.: I performed the above steps with (I believe) a porter.  Not sure what kind.  Not sure if the variety of beer matters.</p>
<p>Also, this post is just one man&#8217;s method.  My phone weathered it fine, but yours might not.  This will probably void your warranty.  It might destroy your phone.  Hell, it might give you scabies or an unhealthy obsession with the Beta Band.</p>
<p>Whatever happens, you undertake the above steps at your own risk.  I make no promises, guarantees, avowals, or assertions as to the safety and efficacy of the above method.  If you mess up your phone, it&#8217;s on your head.</p>
<p>I will, however, say that my battery cover still stays in place quite nicely.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.aaronmbrown.net/blog/2010/04/fixing-the-motorola-droid-battery-cover-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>yield;</title>
		<link>http://www.aaronmbrown.net/blog/2010/04/yield/</link>
		<comments>http://www.aaronmbrown.net/blog/2010/04/yield/#comments</comments>
		<pubDate>Wed, 14 Apr 2010 05:14:05 +0000</pubDate>
		<dc:creator>AMB</dc:creator>
				<category><![CDATA[theory]]></category>

		<guid isPermaLink="false">http://www.aaronmbrown.net/blog/?p=36</guid>
		<description><![CDATA[Posting&#8217;s going to be abnormally slow here the next two weeks.  I&#8217;m currently ramping up into a new project at work while another one is just winding down.  While there&#8217;s overlap, I&#8217;ll probably be pretty busy.  There&#8217;s also some Good Things in the works about which I&#8217;m reticent.  I&#8217;ll post more ]]></description>
			<content:encoded><![CDATA[<p>Posting&#8217;s going to be abnormally slow here the next two weeks.  I&#8217;m currently ramping up into a new project at work while another one is just winding down.  While there&#8217;s overlap, I&#8217;ll probably be pretty busy.  There&#8217;s also some Good Things in the works about which I&#8217;m reticent.  I&#8217;ll post more about them if they pan out.</p>
<p>I do, however, have a few posts in the works about a variety of things; Quines and the CLR and hacker drama, oh my!</p>
<p>So stay tuned.  But while you&#8217;re staying tuned, here&#8217;s an intersting quandary:</p>
<p>So the game Flood-It (to which I&#8217;m fair addicted) has recently <a href="http://arxiv.org/abs/1001.4420">been proved NP-Hard</a>.  The game is fairly simple.  From the abstract of the linked article:</p>
<blockquote><p>&#8220;In this game the player is given an n by n board of tiles where each tile is allocated one of c colours. The goal is to make the colours of all tiles equal via the shortest possible sequence of flooding operations. In the standard version, a flooding operation consists of the player choosing a colour k, which then changes the colour of all the tiles in the monochromatic region connected to the top left tile to k. After this operation has been performed, neighbouring regions which are already of the chosen colour k will then also become connected, thereby extending the monochromatic region of the board.&#8221;</p></blockquote>
<p>So my question: what constraint of the above problem, other than finding the shortest sequence, could one relax or eliminate to move this problem from NP into P?  I strongly suspect that, were a player allowed to choose their starting square after having seen the initial board coloring, that all board configurations would be solvable in P.  I haven&#8217;t proved this yet, though, so I may very well be wrong.</p>
<p>Thoughts?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.aaronmbrown.net/blog/2010/04/yield/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Not Quite Constant</title>
		<link>http://www.aaronmbrown.net/blog/2010/04/not-quite-constant/</link>
		<comments>http://www.aaronmbrown.net/blog/2010/04/not-quite-constant/#comments</comments>
		<pubDate>Mon, 05 Apr 2010 03:51:38 +0000</pubDate>
		<dc:creator>AMB</dc:creator>
				<category><![CDATA[theory]]></category>

		<guid isPermaLink="false">http://www.aaronmbrown.net/blog/?p=35</guid>
		<description><![CDATA[Interesting tidbit I found while brushing up on Big-O analysis &#8211; apparently some methods are O(a(m,n)) where a(m,n) is the inverse Ackerman function.  It&#8217;s also sometimes written as a(n), if the two inputs are equal.  This function does grow (so it&#8217;s worse than constant time) but it grows so slowly that it can ]]></description>
			<content:encoded><![CDATA[<p>Interesting tidbit I found while brushing up on Big-O analysis &#8211; apparently some methods are O(a(m,n)) where a(m,n) is the <a href="http://en.wikipedia.org/wiki/Ackermann_function#Inverse">inverse Ackerman function</a>.  It&#8217;s also sometimes written as a(n), if the two inputs are equal.  This function does grow (so it&#8217;s worse than constant time) but it grows so slowly that it can almost be ignored.</p>
<p>a(n) will not grow larger than 5 for any number which can be written in the physical universe.</p>
<p>An example of an algorithm that demonstrates this is <a href="http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.88.8975&amp;rep=rep1&amp;type=pdf">Chazelle&#8217;s algorithm for minimum spanning trees</a> [pdf], which is O(e*a(e,v)) where e is the number of edges in the graph and v is the number of vertices.  So it&#8217;s <em>functionally</em> O(e), but not quite.</p>
<p>Interesting, if useless.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.aaronmbrown.net/blog/2010/04/not-quite-constant/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Stupid C# Tricks: Ghetto Eval</title>
		<link>http://www.aaronmbrown.net/blog/2010/03/stupid-c-tricks-ghetto-eval/</link>
		<comments>http://www.aaronmbrown.net/blog/2010/03/stupid-c-tricks-ghetto-eval/#comments</comments>
		<pubDate>Mon, 29 Mar 2010 05:35:19 +0000</pubDate>
		<dc:creator>AMB</dc:creator>
				<category><![CDATA[c#]]></category>
		<category><![CDATA[hackety hack]]></category>

		<guid isPermaLink="false">http://www.aaronmbrown.net/blog/?p=28</guid>
		<description><![CDATA[Alternate Title: An Incredibly Simple Introduction to C# Metaprogramming
The eval method is a staple of scripting languages.  It takes a string and executes it as code.  This is useful when, e.g., you need to modify the value of one of several text fields on a page.  The name of the text field can be dynamically ]]></description>
			<content:encoded><![CDATA[<p><strong>Alternate Title: An Incredibly Simple Introduction to C# Metaprogramming</strong></p>
<p>The eval method is a staple of scripting languages.  It takes a string and executes it as code.  This is useful when, e.g., you need to modify the value of one of several text fields on a page.  The name of the text field can be dynamically passed into the eval argument.</p>
<p>So C#, being a compiled language, doesn&#8217;t have an eval method.  What it does have, however, is great support for programmatic compilation of code.  This can be used to hack together something that&#8217;s kind of like an eval.  Take, for instance, the following method:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;color: #FCFFBA;background-color: #222222;">        <span style="color: #0600FF;color: #B83A24;">public</span> <span style="color: #0600FF;color: #B83A24;">static</span> <span style="color: #0600FF;color: #B83A24;">void</span> FauxEval<span style="color: #000000;color: #DDD;">&#40;</span><span style="color: #FF0000;color: #343832;">string</span> codeToEval<span style="color: #000000;color: #DDD;">&#41;</span>
        <span style="color: #000000;color: #DDD;">&#123;</span>
            <span style="color: #008080; font-style: italic;color: #CDC;">//Create compiler objects</span>
            CSharpCodeProvider Compiler <span style="color: #008000;color: #DDD;">=</span> <span style="color: #008000;color: #8FB394;">new</span> CSharpCodeProvider<span style="color: #000000;color: #DDD;">&#40;</span><span style="color: #000000;color: #DDD;">&#41;</span><span style="color: #008000;color: #DDD;">;</span>
            CompilerParameters <span style="color: #0600FF;color: #B83A24;">Params</span> <span style="color: #008000;color: #DDD;">=</span> <span style="color: #008000;color: #8FB394;">new</span> CompilerParameters<span style="color: #000000;color: #DDD;">&#40;</span><span style="color: #000000;color: #DDD;">&#41;</span><span style="color: #008000;color: #DDD;">;</span>
            CompilerResults Results<span style="color: #008000;color: #DDD;">;</span>
&nbsp;
            <span style="color: #0600FF;color: #B83A24;">Params</span>.<span style="color: #0000FF;">GenerateExecutable</span> <span style="color: #008000;color: #DDD;">=</span> true<span style="color: #008000;color: #DDD;">;</span>
            <span style="color: #0600FF;color: #B83A24;">Params</span>.<span style="color: #0000FF;">OutputAssembly</span> <span style="color: #008000;color: #DDD;">=</span> <span style="color: #666666;color: #666666;">&quot;eval.exe&quot;</span><span style="color: #008000;color: #DDD;">;</span>
&nbsp;
            <span style="color: #008080; font-style: italic;color: #CDC;">//We could check this Results object, but this is naught but an example</span>
            Results <span style="color: #008000;color: #DDD;">=</span> Compiler.<span style="color: #0000FF;">CompileAssemblyFromSource</span><span style="color: #000000;color: #DDD;">&#40;</span><span style="color: #0600FF;color: #B83A24;">Params</span>, codeToEval<span style="color: #000000;color: #DDD;">&#41;</span><span style="color: #008000;color: #DDD;">;</span>
&nbsp;
            <span style="color: #008080; font-style: italic;color: #CDC;">//Execute the eval-ed code</span>
            Process evalledExecutable <span style="color: #008000;color: #DDD;">=</span> <span style="color: #008000;color: #8FB394;">new</span> Process<span style="color: #000000;color: #DDD;">&#40;</span><span style="color: #000000;color: #DDD;">&#41;</span><span style="color: #008000;color: #DDD;">;</span>
            evalledExecutable.<span style="color: #0000FF;">StartInfo</span>.<span style="color: #0000FF;">FileName</span> <span style="color: #008000;color: #DDD;">=</span> <span style="color: #666666;color: #666666;">&quot;eval.exe&quot;</span><span style="color: #008000;color: #DDD;">;</span>
            evalledExecutable.<span style="color: #0000FF;">Start</span><span style="color: #000000;color: #DDD;">&#40;</span><span style="color: #000000;color: #DDD;">&#41;</span><span style="color: #008000;color: #DDD;">;</span>
            evalledExecutable.<span style="color: #0000FF;">WaitForExit</span><span style="color: #000000;color: #DDD;">&#40;</span><span style="color: #000000;color: #DDD;">&#41;</span><span style="color: #008000;color: #DDD;">;</span>
        <span style="color: #000000;color: #DDD;">&#125;</span></pre></td></tr></table></div>

<p>This method takes the C# source of an executable as its argument.  It compiles the source and executes it.  Now this is kind of like an eval.  It&#8217;s more structured than that found in a scripting language, since it can only eval code that compiles into a valid C# program.  The heavy lifting is done by the <a href="http://msdn.microsoft.com/en-us/library/microsoft.csharp.csharpcodeprovider.aspx">CSharpCodeProvider class</a>, which handles the compilation of the source.  This class also contains a lot of handy methods to generate code from various CodeDOM objects, making it your one-stop shop for C# metaprogramming.  (N.B. There&#8217;s a comparable class for Visual Basic called <a href="http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.vbcodeprovider.aspx">VBCodeProvider</a>.  This is exceptionally handy if, for whatever reason, you want to use C# to emit Visual Basic code.)</p>
<p>So the above FauxEval method can be used simply by assembling a string that will compile into an executable.  The strength of eval-like methods, of course, is that this string can be constructed dynamically at run time.  As a completely trivial example, here&#8217;s a program that uses the above eval to generate a Hello World-like program:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;color: #FCFFBA;background-color: #222222;">&nbsp;
        <span style="color: #0600FF;color: #B83A24;">static</span> <span style="color: #0600FF;color: #B83A24;">void</span> Main<span style="color: #000000;color: #DDD;">&#40;</span><span style="color: #FF0000;color: #343832;">string</span><span style="color: #000000;color: #DDD;">&#91;</span><span style="color: #000000;color: #DDD;">&#93;</span> args<span style="color: #000000;color: #DDD;">&#41;</span>
        <span style="color: #000000;color: #DDD;">&#123;</span>
            <span style="color: #FF0000;color: #343832;">string</span> programMessage <span style="color: #008000;color: #DDD;">=</span> <span style="color: #666666;color: #666666;">&quot;Hello, world!&quot;</span><span style="color: #008000;color: #DDD;">;</span>
            <span style="color: #0600FF;color: #B83A24;">if</span> <span style="color: #000000;color: #DDD;">&#40;</span>args.<span style="color: #0000FF;">Length</span> <span style="color: #008000;color: #DDD;">&amp;</span>gt<span style="color: #008000;color: #DDD;">;</span> <span style="color: #FF0000;color: #DDD;">0</span><span style="color: #000000;color: #DDD;">&#41;</span>
            <span style="color: #000000;color: #DDD;">&#123;</span>
                programMessage <span style="color: #008000;color: #DDD;">=</span> args<span style="color: #000000;color: #DDD;">&#91;</span><span style="color: #FF0000;color: #DDD;">0</span><span style="color: #000000;color: #DDD;">&#93;</span><span style="color: #008000;color: #DDD;">;</span>
            <span style="color: #000000;color: #DDD;">&#125;</span>
            <span style="color: #FF0000;color: #343832;">string</span> programCode <span style="color: #008000;color: #DDD;">=</span> <span style="color: #666666;color: #666666;">&quot;&quot;</span><span style="color: #008000;color: #DDD;">;</span>
            programCode <span style="color: #008000;color: #DDD;">+=</span> <span style="color: #666666;color: #666666;">&quot;using System;&quot;</span><span style="color: #008000;color: #DDD;">;</span>
            programCode <span style="color: #008000;color: #DDD;">+=</span> <span style="color: #666666;color: #666666;">&quot;using System.Text;&quot;</span><span style="color: #008000;color: #DDD;">;</span>
            programCode <span style="color: #008000;color: #DDD;">+=</span> <span style="color: #666666;color: #666666;">&quot;namespace GeneratedCode&quot;</span><span style="color: #008000;color: #DDD;">;</span>
            programCode <span style="color: #008000;color: #DDD;">+=</span> <span style="color: #666666;color: #666666;">&quot;{&quot;</span><span style="color: #008000;color: #DDD;">;</span>
            programCode <span style="color: #008000;color: #DDD;">+=</span> <span style="color: #666666;color: #666666;">&quot;class GeneratedProgram&quot;</span><span style="color: #008000;color: #DDD;">;</span>
            programCode <span style="color: #008000;color: #DDD;">+=</span> <span style="color: #666666;color: #666666;">&quot;{&quot;</span><span style="color: #008000;color: #DDD;">;</span>
            programCode <span style="color: #008000;color: #DDD;">+=</span> <span style="color: #666666;color: #666666;">&quot;static void Main(string[] args)&quot;</span><span style="color: #008000;color: #DDD;">;</span>
            programCode <span style="color: #008000;color: #DDD;">+=</span> <span style="color: #666666;color: #666666;">&quot;{&quot;</span><span style="color: #008000;color: #DDD;">;</span>
            programCode <span style="color: #008000;color: #DDD;">+=</span> <span style="color: #666666;color: #666666;">&quot;Console.WriteLine(<span style="color: #008080; font-weight: bold;">\&quot;</span>&quot;</span> <span style="color: #008000;color: #DDD;">+</span> programMessage <span style="color: #008000;color: #DDD;">+</span> <span style="color: #666666;color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\&quot;</span>);&quot;</span><span style="color: #008000;color: #DDD;">;</span>
            programCode <span style="color: #008000;color: #DDD;">+=</span> <span style="color: #666666;color: #666666;">&quot;Console.WriteLine(<span style="color: #008080; font-weight: bold;">\&quot;</span>Press any key to continue...<span style="color: #008080; font-weight: bold;">\&quot;</span>);&quot;</span><span style="color: #008000;color: #DDD;">;</span>
            programCode <span style="color: #008000;color: #DDD;">+=</span> <span style="color: #666666;color: #666666;">&quot;Console.ReadKey(true);&quot;</span><span style="color: #008000;color: #DDD;">;</span>
            programCode <span style="color: #008000;color: #DDD;">+=</span> <span style="color: #666666;color: #666666;">&quot;}&quot;</span><span style="color: #008000;color: #DDD;">;</span>
            programCode <span style="color: #008000;color: #DDD;">+=</span> <span style="color: #666666;color: #666666;">&quot;}&quot;</span><span style="color: #008000;color: #DDD;">;</span>
            programCode <span style="color: #008000;color: #DDD;">+=</span> <span style="color: #666666;color: #666666;">&quot;}&quot;</span><span style="color: #008000;color: #DDD;">;</span>
&nbsp;
            FauxEval<span style="color: #000000;color: #DDD;">&#40;</span>programCode<span style="color: #000000;color: #DDD;">&#41;</span><span style="color: #008000;color: #DDD;">;</span>
        <span style="color: #000000;color: #DDD;">&#125;</span></pre></td></tr></table></div>

<p>The generated program will simply print the first command line argument to the console.  If no argument is provided, the executable is built to be a default &#8220;Hello, World&#8221;.</p>
<p>The results:</p>
<p><a href="http://www.aaronmbrown.net/blog/wp-content/uploads/2010/03/FauxEvalCapture.jpg"><img src="http://www.aaronmbrown.net/blog/wp-content/uploads/2010/03/FauxEvalCapture.jpg" alt="" width="450" height="99" /></a><br />
(Click to embiggen)</p>
<p>So that&#8217;s a super-simple C# eval.  This provides the ability to execute code that&#8217;s been dynamically created at run time, provided the code can be compiled into an executable.  There&#8217;s a lot of room to expand on the method and make it more flexible and general, but there really is only so much you can do with this strategy of code evaluation, since the code always has to be able to be compiled into an assembly.  Still, it&#8217;s a damn sight better than nothing.</p>
<p><strong>Update 2010.3.30:</strong> I removed some random debugging code from the Eval method above.  It was a classic case of YAGNI.  I thought I might want to dump eval string contents to a file.  I ended up not needing to, but then forgot to pull the debugging code.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.aaronmbrown.net/blog/2010/03/stupid-c-tricks-ghetto-eval/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
