<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-5374463297582559341</id><updated>2011-04-22T02:48:15.846+02:00</updated><category term='nant'/><category term='C#'/><category term='design'/><category term='msbuild'/><category term='build'/><category term='BuildLib'/><category term='.NET'/><title type='text'>CodeFocus</title><subtitle type='html'>My thoughts on code, design and architecture. Mainly focused on the .NET platform and related tools.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://codefocus.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5374463297582559341/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://codefocus.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Andreas Böckert</name><uri>http://www.blogger.com/profile/02408546627211273599</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='21' height='32' src='http://4.bp.blogspot.com/_cjYCdxncpOE/SNn7mtL2Q-I/AAAAAAAAAAU/Xf9LR8DLV6Q/S220/Portr%C3%A4tt_cropped.jpg'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>4</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-5374463297582559341.post-7361513176275297440</id><published>2009-02-10T20:10:00.009+01:00</published><updated>2009-02-16T20:59:57.940+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='.NET'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><title type='text'>ReSharper Recipe: Move Method</title><content type='html'>&lt;p&gt;Its a shame that ReSharper does not support moving a method directly. It's not hard to do in sequence but it does take a few extra seconds. &lt;/p&gt;  &lt;h5&gt;Move Method&lt;/h5&gt;  &lt;p&gt;The fastest way to move a method is to use “Make Method Static” followed by “Make Method Non-Static”. But first, some sample code to work with...&lt;/p&gt;  &lt;p&gt;The Car class:&lt;/p&gt;  &lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Car&lt;br /&gt;{&lt;br /&gt;   &lt;span class="kwrd"&gt;public&lt;/span&gt; CarEngine Engine;&lt;br /&gt;&lt;br /&gt;   &lt;span class="kwrd"&gt;public&lt;/span&gt; Car()&lt;br /&gt;   {&lt;br /&gt;       Engine = &lt;span class="kwrd"&gt;new&lt;/span&gt; CarEngine(4, 0.5f);&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;float&lt;/span&gt; ComputeEngineVolume()&lt;br /&gt;   {&lt;br /&gt;       &lt;span class="kwrd"&gt;return&lt;/span&gt;&lt;br /&gt;           Engine.LitersPerCylinder *&lt;br /&gt;           Engine.NumCylinders;&lt;br /&gt;   }&lt;br /&gt;}&lt;/pre&gt;&lt;p&gt;The CarEngine class:&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; CarEngine&lt;br /&gt;{&lt;br /&gt;   &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;float&lt;/span&gt; LitersPerCylinder;&lt;br /&gt;   &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; NumCylinders;&lt;br /&gt;&lt;br /&gt;   &lt;span class="kwrd"&gt;public&lt;/span&gt; CarEngine(&lt;span class="kwrd"&gt;int&lt;/span&gt; numCylinders,&lt;br /&gt;                    &lt;span class="kwrd"&gt;float&lt;/span&gt; litersPerCylinder)&lt;br /&gt;   {&lt;br /&gt;       NumCylinders = numCylinders;&lt;br /&gt;       LitersPerCylinder = litersPerCylinder;&lt;br /&gt;   }&lt;br /&gt;}&lt;/pre&gt;&lt;p&gt;Calling code:&lt;/p&gt;&lt;pre class="csharpcode"&gt;Car c = &lt;span class="kwrd"&gt;new&lt;/span&gt; Car();&lt;br /&gt;&lt;span class="kwrd"&gt;float&lt;/span&gt; volume = c.ComputeEngineVolume();&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p&gt;Clearly, the ComputeEngineVolume method belong to the CarEngine class and not to the Car class. To move the method we’ll use two steps, first we'll use the "Make Method Static" refactoring on the ComputeEngineVolume method. You should make sure that you get the target object as a input parameter to the static method. CarEngine in this case. The resulting code is:&lt;br /&gt;&lt;br /&gt;Car class:&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Car&lt;br /&gt;{&lt;br /&gt;   ...&lt;/pre&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;float&lt;/span&gt; ComputeEngineVolume(&lt;br /&gt;       CarEngine engine)&lt;br /&gt;   {&lt;br /&gt;       &lt;span class="kwrd"&gt;return&lt;/span&gt;&lt;br /&gt;           engine.LitersPerCylinder *&lt;br /&gt;           engine.NumCylinders;&lt;br /&gt;   }&lt;br /&gt;}&lt;/pre&gt;&lt;p&gt;Calling code&lt;/p&gt;&lt;pre class="csharpcode"&gt;Car c = &lt;span class="kwrd"&gt;new&lt;/span&gt; Car();&lt;br /&gt;&lt;span class="kwrd"&gt;float&lt;/span&gt; volume = Car.ComputeEngineVolume(c.Engine);&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;The next step is to use the "Make Method Non-Static" refactoring to move the method to its new home, the CarEngine class. In the dialog, select the appropriate target class and let ReSharper do its work.&lt;br /&gt;&lt;br /&gt;The Car class:&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Car&lt;br /&gt;{&lt;br /&gt;   &lt;span class="kwrd"&gt;public&lt;/span&gt; CarEngine Engine;&lt;br /&gt;&lt;br /&gt;   &lt;span class="kwrd"&gt;public&lt;/span&gt; Car()&lt;br /&gt;   {&lt;br /&gt;       Engine = &lt;span class="kwrd"&gt;new&lt;/span&gt; CarEngine(4, 0.5f);&lt;br /&gt;   }&lt;br /&gt;}&lt;/pre&gt;&lt;p&gt;The CarEngine class:&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; CarEngine&lt;br /&gt;{&lt;br /&gt;   ...&lt;br /&gt;&lt;br /&gt;   &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;float&lt;/span&gt; ComputeEngineVolume()&lt;br /&gt;   {&lt;br /&gt;       &lt;span class="kwrd"&gt;return&lt;/span&gt;&lt;br /&gt;           LitersPerCylinder *&lt;br /&gt;           NumCylinders;&lt;br /&gt;   }&lt;br /&gt;}&lt;/pre&gt;&lt;p&gt;And the calling code:&lt;/p&gt;&lt;pre class="csharpcode"&gt;Car c = &lt;span class="kwrd"&gt;new&lt;/span&gt; Car();&lt;br /&gt;&lt;span class="kwrd"&gt;float&lt;/span&gt; volume = c.Engine.ComputeEngineVolume();&lt;/pre&gt;&lt;br /&gt;&lt;p&gt;A final touch would probably be to refactor the ComputeEngineVolume method into a Volume property but we’ll leave it for now.&lt;/p&gt;&lt;h5&gt;Tweaks&lt;/h5&gt;&lt;p&gt;If you want to have a bit more control over the process you could initiate it by using a “Introduce Parameter” refactoring to hand pick the target class. This is useful if you have a more complex scenario then the simple property used here.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5374463297582559341-7361513176275297440?l=codefocus.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://codefocus.blogspot.com/feeds/7361513176275297440/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5374463297582559341&amp;postID=7361513176275297440' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5374463297582559341/posts/default/7361513176275297440'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5374463297582559341/posts/default/7361513176275297440'/><link rel='alternate' type='text/html' href='http://codefocus.blogspot.com/2009/02/resharper-recipe-move-method.html' title='ReSharper Recipe: Move Method'/><author><name>Andreas Böckert</name><uri>http://www.blogger.com/profile/02408546627211273599</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='21' height='32' src='http://4.bp.blogspot.com/_cjYCdxncpOE/SNn7mtL2Q-I/AAAAAAAAAAU/Xf9LR8DLV6Q/S220/Portr%C3%A4tt_cropped.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5374463297582559341.post-6667774619495421598</id><published>2008-12-03T11:12:00.003+01:00</published><updated>2008-12-03T15:31:52.516+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='.NET'/><category scheme='http://www.blogger.com/atom/ns#' term='design'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><title type='text'>null is not a boolean</title><content type='html'>One coding practice I have a problem with is returning null to signify "no value here". This leads to null-check-littered code ans consequently decreases readability. Davy Brion has elaborated on this subject &lt;a href="http://elegantcode.com/2008/12/02/do-not-litter-your-code-with-null-checks/"&gt;here&lt;/a&gt; and I can only agree wholeheartedly.&lt;br /&gt;&lt;br /&gt;So, how do we deal with data that may or may not be present?&lt;br /&gt;&lt;br /&gt;If possible, the best approach is to use the &lt;a href="http://en.wikipedia.org/wiki/Null_Object_pattern"&gt;null object pattern&lt;/a&gt;. This allows the calling code to completely ignore the fact that the object is undefined. I'll not write anything more about it, check the web for more information.&lt;br /&gt;&lt;br /&gt;There are however cases where the caller actually need to know if there is a value present or not. Consider the following code:&lt;br /&gt;&lt;pre style="border: 1px dashed rgb(153, 153, 153); padding: 5px; overflow: auto; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238); font-size: 12px; line-height: 14px; width: 100%;"&gt;&lt;code&gt;class Person&lt;br /&gt;{&lt;br /&gt;  public Animal Pet { get; }&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;In this example, a person may or may not have a pet. If we use null to signify the absence of a pet then code that accesses a Person object would have to write code like:&lt;br /&gt;&lt;pre style="border: 1px dashed rgb(153, 153, 153); padding: 5px; overflow: auto; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238); font-size: 12px; line-height: 14px; width: 100%;"&gt;&lt;code&gt;void WritePet(Person person)&lt;br /&gt;{&lt;br /&gt;  if (person.Pet != null)&lt;br /&gt;  {&lt;br /&gt;      Console.WriteLine("Pet name is: " + person.Pet.Name);&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;The first issue with this code is the "person.Pet != null" check. We're using the Pet reference do the extra work of keeping track of if there is a value present or not. The Pet property should be used to access the Pet, not to determine wither there is a value there or not.&lt;br /&gt;&lt;br /&gt;The second issue is that nothing in this code indicates that null is a valid value. You could add this information in a comment but relying on comments should be avoided when possible.&lt;br /&gt;&lt;br /&gt;I prefer the following construction:&lt;br /&gt;&lt;pre style="border: 1px dashed rgb(153, 153, 153); padding: 5px; overflow: auto; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238); font-size: 12px; line-height: 14px; width: 100%;"&gt;&lt;code&gt;class Person&lt;br /&gt;{&lt;br /&gt;   private Animal pet = null;&lt;br /&gt;   public bool PetIsValid { get { return pet != null; } }&lt;br /&gt;   public Animal Pet { get { return pet; } }&lt;br /&gt;}&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;Yes, this adds a bit of code to the class. And yes, I'm using the pet field as a boolean myself. The difference though is that I only perform the pet != null check internally in the class. It is not unreasonable for the class to know that the pet value may be null.&lt;br /&gt;&lt;br /&gt;We can now rewrite the calling code as:&lt;br /&gt;&lt;pre style="border: 1px dashed rgb(153, 153, 153); padding: 5px; overflow: auto; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238); font-size: 12px; line-height: 14px; width: 100%;"&gt;&lt;code&gt;void WritePet(Person person)&lt;br /&gt;{&lt;br /&gt;   if (person.PetIsValid)&lt;br /&gt;   {&lt;br /&gt;       Console.WriteLine("Pet name is: " + person.Pet.Name);&lt;br /&gt;   }&lt;br /&gt;}&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;The first benefit of this pattern is that the calling code is simply more readable. (Yes, that is subjective.)&lt;br /&gt;&lt;br /&gt;A second and more important benefit of applying this pattern consistently in a code base is that when you find a property that does not have an associated XyzIsValid property you can be sure that it will not be null!&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;(A small note on the naming: I think that HasPet would be a better name than PetIsValid. The drawback is that it would be harder to discover. Using the PetIsValid naming convention this method will appear in the intellisense list when you type person.Pet, hinting that you should probably check validity before proceeding).&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5374463297582559341-6667774619495421598?l=codefocus.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://codefocus.blogspot.com/feeds/6667774619495421598/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5374463297582559341&amp;postID=6667774619495421598' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5374463297582559341/posts/default/6667774619495421598'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5374463297582559341/posts/default/6667774619495421598'/><link rel='alternate' type='text/html' href='http://codefocus.blogspot.com/2008/12/null-is-not-boolean.html' title='null is not a boolean'/><author><name>Andreas Böckert</name><uri>http://www.blogger.com/profile/02408546627211273599</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='21' height='32' src='http://4.bp.blogspot.com/_cjYCdxncpOE/SNn7mtL2Q-I/AAAAAAAAAAU/Xf9LR8DLV6Q/S220/Portr%C3%A4tt_cropped.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5374463297582559341.post-1961604338109545088</id><published>2008-10-09T07:17:00.009+02:00</published><updated>2008-10-13T18:51:13.301+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='.NET'/><category scheme='http://www.blogger.com/atom/ns#' term='build'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><category scheme='http://www.blogger.com/atom/ns#' term='msbuild'/><title type='text'>Running MSBuild tasks programmatically</title><content type='html'>I tried scouring the web for a description on how to run MSBuild tasks from your own code but came up with slim results. After some investigations this is one way that managed to run some tasks at least.&lt;br /&gt;&lt;br /&gt;For this example, we'll compile a .cs file in the simplest way possible. To achieve this we need to use the Csc task.&lt;br /&gt;&lt;br /&gt;There is one property on all tasks that deserves a special mention. It is Task.BuildEngine. The property is of type IBuildEngine and as far as I can tell the only implementation of this interface is an internal class called EngineProxy in the Microsoft.Build.BuildEngine namespace. Since it's internal we cannot use it.&lt;br /&gt;&lt;br /&gt;We need to create our own IBuildEngine implementation. It turns out that this is pretty straightforward:&lt;br /&gt;&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; MyBuildEngine : IBuildEngine&lt;br /&gt;{&lt;br /&gt;   &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;bool&lt;/span&gt; BuildProjectFile(&lt;span class="kwrd"&gt;string&lt;/span&gt; projectFileName, &lt;span class="kwrd"&gt;string&lt;/span&gt;[] targetNames, &lt;br /&gt;                                IDictionary globalProperties, &lt;br /&gt;                                IDictionary targetOutputs)&lt;br /&gt;   {&lt;br /&gt;      &lt;span class="kwrd"&gt;throw&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; NotImplementedException();&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; ColumnNumberOfTaskNode&lt;br /&gt;   {&lt;br /&gt;      get { &lt;span class="kwrd"&gt;return&lt;/span&gt; 0; }&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;bool&lt;/span&gt; ContinueOnError&lt;br /&gt;   {&lt;br /&gt;      get { &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;false&lt;/span&gt;; }&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; LineNumberOfTaskNode&lt;br /&gt;   {&lt;br /&gt;      get { &lt;span class="kwrd"&gt;return&lt;/span&gt; 0; }&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; ProjectFileOfTaskNode&lt;br /&gt;   {&lt;br /&gt;      get { &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="str"&gt;""&lt;/span&gt;; }&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; LogCustomEvent(CustomBuildEventArgs e)&lt;br /&gt;   {&lt;br /&gt;      Console.WriteLine(&lt;span class="str"&gt;"Custom: {0}"&lt;/span&gt;, e.Message);&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; LogErrorEvent(BuildErrorEventArgs e)&lt;br /&gt;   {&lt;br /&gt;      Console.WriteLine(&lt;span class="str"&gt;"Error: {0}"&lt;/span&gt;, e.Message);&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; LogMessageEvent(BuildMessageEventArgs e)&lt;br /&gt;   {&lt;br /&gt;      Console.WriteLine(&lt;span class="str"&gt;"Message: {0}"&lt;/span&gt;, e.Message);&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; LogWarningEvent(BuildWarningEventArgs e)&lt;br /&gt;   {&lt;br /&gt;      Console.WriteLine(&lt;span class="str"&gt;"Warning: {0}"&lt;/span&gt;, e.Message);&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;This implementation is very simple.&lt;br /&gt;&lt;ul&gt;&lt;li&gt;We'll simply ignore BuildProjectFile since we're running tasks manually here.&lt;/li&gt;&lt;li&gt;The *TaskNode methods are also almost ignored. I haven't investigated them but it looks like they're used for reporting errors in project files.&lt;/li&gt;&lt;li&gt;The Log* methods simply dump the message to the console.&lt;/li&gt;&lt;/ul&gt;Now we're ready to run the Csc task:&lt;br /&gt;&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;br /&gt;Csc cscTask = &lt;span class="kwrd"&gt;new&lt;/span&gt; Csc();&lt;br /&gt;cscTask.BuildEngine = &lt;span class="kwrd"&gt;new&lt;/span&gt; MyBuildEngine();&lt;br /&gt;cscTask.Sources = &lt;span class="kwrd"&gt;new&lt;/span&gt; TaskItem[]{&lt;br /&gt;   &lt;span class="kwrd"&gt;new&lt;/span&gt; TaskItem(&lt;span class="str"&gt;@"C:\Code\Projects\MSBuildTest\HelloWorld\HelloWorld.cs"&lt;/span&gt;)&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;if&lt;/span&gt; (cscTask.Execute())&lt;br /&gt;{&lt;br /&gt;   Console.WriteLine(&lt;span class="str"&gt;"Task executed ok. Resulting assembly: {0}"&lt;/span&gt;,&lt;br /&gt;      cscTask.OutputAssembly&lt;br /&gt;   );&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;This will produce an output file HelloWorld.exe in the working directory.&lt;br /&gt;&lt;br /&gt;Obviously, there are lots of other properties available on the Csc task but this was just the simplest example possible.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5374463297582559341-1961604338109545088?l=codefocus.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://codefocus.blogspot.com/feeds/1961604338109545088/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5374463297582559341&amp;postID=1961604338109545088' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5374463297582559341/posts/default/1961604338109545088'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5374463297582559341/posts/default/1961604338109545088'/><link rel='alternate' type='text/html' href='http://codefocus.blogspot.com/2008/10/running-msbuild-tasks-programatically.html' title='Running MSBuild tasks programmatically'/><author><name>Andreas Böckert</name><uri>http://www.blogger.com/profile/02408546627211273599</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='21' height='32' src='http://4.bp.blogspot.com/_cjYCdxncpOE/SNn7mtL2Q-I/AAAAAAAAAAU/Xf9LR8DLV6Q/S220/Portr%C3%A4tt_cropped.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5374463297582559341.post-6430417578787620585</id><published>2008-10-06T19:03:00.013+02:00</published><updated>2008-10-07T20:50:57.973+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='BuildLib'/><category scheme='http://www.blogger.com/atom/ns#' term='.NET'/><category scheme='http://www.blogger.com/atom/ns#' term='build'/><category scheme='http://www.blogger.com/atom/ns#' term='nant'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><category scheme='http://www.blogger.com/atom/ns#' term='msbuild'/><title type='text'>BuildLib</title><content type='html'>I initiated yet another side project this weekend. It's an idea that has been growing on me for a while.&lt;br /&gt;&lt;br /&gt;A build engine that uses C# as a scripting language.&lt;br /&gt;&lt;br /&gt;The recipe for a build frequently includes a number of tools. A few utility .exes here, some .bat files there. A bit of scripting in NAnt or MSBuild. Maybe even some custom extensions to the build engine. Finally, a little continuous integration scripting to finish it all off.&lt;br /&gt;&lt;br /&gt;Wouldn't it be neater to unify this into one tool?&lt;br /&gt;Why should I need to learn yet another syntax (i.e. Nant or MSBuild)?&lt;br /&gt;Why create clumsy custom language using XML when C# is more powerful then msbuild will ever be?&lt;br /&gt;&lt;br /&gt;Imagine that you want to have a file that contains a simple timestamp. Something along the lines of:&lt;br /&gt;&lt;pre style="border: 1px dashed rgb(153, 153, 153); padding: 5px; overflow: auto; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238); font-size: 12px; line-height: 14px; width: 100%;"&gt;&lt;code&gt;namespace MyProject {&lt;br /&gt;  public class BuildInfo {&lt;br /&gt;      public string BuildDate = "_BUILDDATE_PLACEHOLDER_";&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;And you want to transform this into:&lt;br /&gt;&lt;pre style="border: 1px dashed rgb(153, 153, 153); padding: 5px; overflow: auto; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238); font-size: 12px; line-height: 14px; width: 100%;"&gt;&lt;code&gt;namespace MyProject {&lt;br /&gt;  public class BuildInfo {&lt;br /&gt;      public string BuildDate = "2008-10-07";&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;How would you update this using msbuild? My best bet is that you would need to obtain and include &lt;a href="http://msbuildtasks.tigris.org/"&gt;MSBuild community tasks&lt;/a&gt; and use FileUpdate task. I have no idea how to obtain a properly formatted date...&lt;br /&gt;&lt;br /&gt;Compare this to:&lt;br /&gt;&lt;pre style="border: 1px dashed rgb(153, 153, 153); padding: 5px; overflow: auto; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238); font-size: 12px; line-height: 14px; width: 100%;"&gt;&lt;code&gt;string templateText = File.ReadAllText("BuildDate.cs.template");&lt;br /&gt;string buildDate = DateTime.Now.ToShortDateString();&lt;br /&gt;string updatedText = templateText.Replace("_BUILDDATE_PLACEHOLDER_", buildDate );&lt;br /&gt;File.WriteAllText("BuildDate.cs", updatedText);&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;To solve some of these issues I initiated a project with the boring yet appropriate name BuildLib.&lt;br /&gt;&lt;br /&gt;So, what is the current state of BuildLib?&lt;br /&gt;&lt;ul&gt;&lt;li&gt;It has a name! That took me a good two hours.  &lt;/li&gt;&lt;li&gt;It has &lt;a href="http://code.google.com/p/buildlib/"&gt;project page&lt;/a&gt; on google code.&lt;br /&gt;&lt;/li&gt;&lt;li&gt;It has stub projects and a directory structure (heavliy influenced by JP Boodhoos &lt;a href="http://blog.jpboodhoo.com/DirectoryStructureForProjects.aspx"&gt;post&lt;/a&gt;)&lt;/li&gt;&lt;/ul&gt;I'll try to use this blog as a design diary for BuildLib. We'll see where the project heads. Perhaps it will turn out to be a fluent interface over MSBuild, a full fledged build engine or (perhaps most likely) yet another abandoned side project that got 60% done.&lt;br /&gt;&lt;br /&gt;Since I have a 8 week old girl at home I don't expect any abundance of free time. On the other hand, she normally wakes me up at 6 in the morning so I do have an hour or two before work...&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5374463297582559341-6430417578787620585?l=codefocus.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://codefocus.blogspot.com/feeds/6430417578787620585/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5374463297582559341&amp;postID=6430417578787620585' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5374463297582559341/posts/default/6430417578787620585'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5374463297582559341/posts/default/6430417578787620585'/><link rel='alternate' type='text/html' href='http://codefocus.blogspot.com/2008/10/buildlib.html' title='BuildLib'/><author><name>Andreas Böckert</name><uri>http://www.blogger.com/profile/02408546627211273599</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='21' height='32' src='http://4.bp.blogspot.com/_cjYCdxncpOE/SNn7mtL2Q-I/AAAAAAAAAAU/Xf9LR8DLV6Q/S220/Portr%C3%A4tt_cropped.jpg'/></author><thr:total>0</thr:total></entry></feed>
