Smoothfriction.nl http://blog.smoothfriction.nl/Default.aspx en-US Erik van Brakel Subtext Version 2.6.0.0 Smoothfriction.nl http://blog.smoothfriction.nl/images/RSS2Image.gif http://blog.smoothfriction.nl/Default.aspx 77 60 Silverlight BusyIndicator in Caliburn.Micro 2 MVVM Silverlight Caliburn.Micro http://blog.smoothfriction.nl/archive/2012/05/07/silverlight-busyindicator-in-caliburn-micro-pt-2-again.aspx <p>In my <a href="http://blog.smoothfriction.nl/archive/2012/04/20/silverlight-busyindicator-in-caliburn-micro.aspx">previous post</a> about the subject, I presented one approach to wiring up the BusyIndicator control from the Silverlight Toolkit using some fancy Caliburn.Micro wizardry… Ok, maybe it was not wizardry. It was actually simply diving into the extensibility model that comes with the framework. <br />I also said I already had my next solution figured out, and I would present that in this post. Well… I lied. I didn’t have it all figured out, and it took some experiments to get it right. I’m not completely satisfied yet, but I think the solution I am about to show you will offer just a little bit more flexibility than the previous one. I do warn you, It’s there’s a good wall of text incoming. I will put the code on Github soon(ish), which I will announce in a future post. The project will contain both approaches (and possibly new ones if I come up with something else). But for now, just read through this ;-).</p> <p>First, we’ll start off with a stock Caliburn.Micro Silverlight application. Just like last time, we’ll add a button to the view and wire it up to the view model. For completeness sake, here’s the code for the view and the viewmodel:</p> <p>View:</p> <pre class="brush: xml;">&lt;UserControl x:Class="Approach2.ShellView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit"&gt; &lt;toolkit:BusyIndicator&gt; &lt;Grid Background="White"&gt; &lt;Button x:Name="DoSomething" Content="Do something" Height="20" Width="100" /&gt; &lt;/Grid&gt; &lt;/toolkit:BusyIndicator&gt; &lt;/UserControl&gt;</pre> <p>ViewModel:</p> <pre class="brush: csharp;">using System.Collections.Generic; using System.ComponentModel.Composition; using Caliburn.Micro; namespace Approach2 { [Export(typeof(IShell))] public class ShellViewModel : IShell { public IEnumerator&lt;IResult&gt; DoSomething() { yield return new SleepRoutine(1000); yield return new SleepRoutine(1000); yield return new SleepRoutine2(1000); yield return new SleepRoutine2(1000); yield return new SleepRoutine(3000); } } }</pre> <p>Note that the BusyIndicator control in the view no longer has any bindings. It doesn’t even need a name! The view model contains a method which returns a bunch of co-routines, which are basically the same except for one minor detail:</p> <pre class="brush: csharp;">public class SleepRoutineBase : IResult { private readonly int _milliseconds; public SleepRoutineBase(int milliseconds) { _milliseconds = milliseconds; } public void Execute(ActionExecutionContext context) { Task.Factory.StartNew(() =&gt; Thread.Sleep(_milliseconds)) .ContinueWith(x =&gt; Completed(this, new ResultCompletionEventArgs())); } public event EventHandler&lt;ResultCompletionEventArgs&gt; Completed; } [BusyMessage("Sleeping...")] public class SleepRoutine : SleepRoutineBase { public SleepRoutine(int milliseconds) : base(milliseconds) { } } public class SleepRoutine2 : SleepRoutineBase { public SleepRoutine2(int milliseconds) : base(milliseconds) { } }</pre> <p>I’ve extracted the functionality in a base class (just for the example, this is obviously not a REAL scenario). The only difference is that this time, we’ve annotated the co-routine with the BusyMessage attribute instead of the method on the view model. This means that instead of having a single status message for the whole sequence, we can show a message for each step in our action. However, hooking this up is a bit different from the previous approach.</p> <p>First, a bit of explanation is required. What actually happens when you trigger an action, which fires off a message which returns an IEnumerator of IResult? Well, to put it simply, the framework takes the IEnumerator, passes it into a function called CreateParentEnumerator on the Coroutine class, and executes that. Internally, the SequentialResult class will make sure that all co-routines in the IEnumerator will execute sequentially. Luckily, the Caliburn.Micro framework allows us to easily change or extend this functionality by exposing the function as a field, which allows us to do something like this:</p> <pre class="brush: csharp;">Coroutine.CreateParentEnumerator = e =&gt; new BusyIndicatorResult(e);</pre> <p>This means that instead of using a SequentialResult, the framework will now use my own IResult instead. This IResult also simply executes a sequential result, but not before doing some transformations here and there. Every co-routine contained in the IEnumerator will be wrapped in a decorator which will activate the BusyIndicator when needed, and it resets the BusyIndicator at the end. The code is not that complicated, although we do need to do a lot through the Dispatcher because there’s no guarantee that the co-routines will all run on the UI thread:</p> <pre class="brush: csharp;">class BusyIndicatorResult : IResult { private BusyIndicator _busyIndicator; private SequentialResult _sequentialResult; private static IEnumerator&lt;IResult&gt; Decorate(IEnumerator&lt;IResult&gt; source) { while (source.MoveNext()) { yield return new BusyIndicatorDecorator(source.Current); } } public BusyIndicatorResult(IEnumerator&lt;IResult&gt; enumerator) { _sequentialResult = new SequentialResult(Decorate(enumerator)); } public void Execute(ActionExecutionContext context) { _busyIndicator = context.Source.GetAncestor&lt;BusyIndicator&gt;(); _sequentialResult.Completed += ResetBusyIndicator; _sequentialResult.Execute(context); } public event EventHandler&lt;ResultCompletionEventArgs&gt; Completed; private void ResetBusyIndicator(object sender, ResultCompletionEventArgs e) { (sender as IResult).Completed -= ResetBusyIndicator; if (_busyIndicator != null) { Deployment.Current.Dispatcher.BeginInvoke(() =&gt; _busyIndicator.IsBusy = false); } Completed(this, e); } }</pre> <p>And the decorator class:</p> <pre class="brush: csharp;">class BusyIndicatorDecorator : IResult { private readonly IResult _innerResult; private BusyIndicator _busyIndicator; public BusyIndicatorDecorator(IResult innerResult) { _innerResult = innerResult; } public void Execute(ActionExecutionContext context) { var statusMessage = _innerResult.GetType().GetAttributes&lt;BusyMessageAttribute&gt;(true).FirstOrDefault(); SetBusyIndicator(statusMessage, context); _innerResult.Completed += OnComplete; _innerResult.Execute(context); } private void SetBusyIndicator(BusyMessageAttribute statusMessage, ActionExecutionContext context) { Deployment.Current.Dispatcher.BeginInvoke(() =&gt; { _busyIndicator = context.Source.GetAncestor&lt;BusyIndicator&gt;(); if (_busyIndicator != null) { _busyIndicator.IsBusy = statusMessage != null; _busyIndicator.BusyContent = statusMessage == null ? "" : statusMessage.Message; } }); }</pre> <p>And finally, a little helper class I use to find the BusyIndicator control somewhere upstream in the visual tree, starting at the element which triggered the action:</p> <pre class="brush: csharp;">internal static class VisualTreeExtensions { public static T GetAncestor&lt;T&gt;(this FrameworkElement source) where T : class { var current = source; while(current != null) { if (current is T) return current as T; current = current.Parent as FrameworkElement; } return null; } }</pre> <p>This will now show a BusyIndicator for each co-routine class which is annotated with the BusyMessageAttribute. When that attribute is not present, the BusyIndicator will be hidden. Whenever the SequentialResult completes, regardless of the reason (ran to completion, cancelled, exception), the indicator will be hidden again.</p> <p>Another neat side-effect from this approach is that it very easily scales to having multiple distinct sets of BusyIndicator containers with their own controls triggering them, because the control triggering the action will only trigger showing the first BusyIndicator upstream in the visual tree. </p> <p>As stated in the introductory paragraph I will publish the code on Github sometime in the near future. If I forget, and you really, really want to play around with it, just drop me a line here on on twitter, either on <a href="http://www.twitter.com/smoothfriction">@smoothfriction</a> or <a href="http://www.twitter.com/erikvanbrakel">@erikvanbrakel</a>.</p><img src="http://blog.smoothfriction.nl/aggbug/59.aspx" width="1" height="1" /> Erik van Brakel http://blog.smoothfriction.nl/archive/2012/05/07/silverlight-busyindicator-in-caliburn-micro-pt-2-again.aspx Mon, 07 May 2012 20:35:58 GMT http://blog.smoothfriction.nl/archive/2012/05/07/silverlight-busyindicator-in-caliburn-micro-pt-2-again.aspx#feedback http://blog.smoothfriction.nl/comments/commentRss/59.aspx Pro-tip: .gitignore file for Visual Studio development git visual studio http://blog.smoothfriction.nl/archive/2012/05/04/pro-tip-gitignore-file-for-visual-studio-development.aspx <p>When using git, or any source control system for that matter, you want to be very careful that you only submit the files that matter to your repository. The main reasons being that a clean repository is easier to navigate, and if you’re really pushing it it might help you clone a fresh instance to a new development machine just that little bit quicker.</p> <p>The community at StackOverflow have a very nice thread running <a href="http://stackoverflow.com/questions/2143956/gitignore-for-visual-studio-projects-and-solutions">with a very good list of suggested ignore rules</a>. I’ve taken the liberty to put the list into a gist, so everyone can easily clone the list and make personal adaptions. </p> <p><a href="https://gist.github.com/2589150">The Gist is available here</a>.</p> <p><em>Note: there is also an <a href="https://github.com/github/gitignore/">official repository from the github team with all sorts of gitignore files</a>. The ignore file in my gist is what I use, and it’s only slightly different from the one for CSharp in the repository. Obviously, everyone is free to choose whichever approach they want to take ;-)</em></p><img src="http://blog.smoothfriction.nl/aggbug/58.aspx" width="1" height="1" /> Erik van Brakel http://blog.smoothfriction.nl/archive/2012/05/04/pro-tip-gitignore-file-for-visual-studio-development.aspx Fri, 04 May 2012 08:00:00 GMT http://blog.smoothfriction.nl/archive/2012/05/04/pro-tip-gitignore-file-for-visual-studio-development.aspx#feedback http://blog.smoothfriction.nl/comments/commentRss/58.aspx Silverlight BusyIndicator in Caliburn.Micro Caliburn.Micro MVVM Silverlight http://blog.smoothfriction.nl/archive/2012/04/20/silverlight-busyindicator-in-caliburn-micro.aspx <p><em>This article will assume that you’re familiar with Caliburn.Micro and know how to set up a silverlight application using this (awesome) MVVM framework. The steps for doing this will be skipped in the explanation. I am also going to assume that you know and use NuGet for installing packages.</em></p> <p>If you’re working with Silverlight, the <a href="http://silverlight.codeplex.com/">Silverlight Toolkit</a> offers all sorts of controls to spice up your application. I urge you to check out <a href="http://silverlight.net/content/samples/sl4/toolkitcontrolsamples/run/default.html">the demo</a> for a lot of samples to get a good feeling of what’s available. <br />One of the controls I really like is the BusyIndicator. It offers an easy way to speed up your <em>perceived performance</em>, or to put it differently, it gives your end-users the feeling that the application is responsive and getting the best performance they can get. Obviously this is just a façade, because you’re still doing the same amount of work. You’re just letting your users know what is happening instead of leaving them in the dark while waiting for something to happen.</p> <p>Anyway, words don’t really convey the essence of the control. However, a picture does. So, this is what your users will see when you use the busy indicator to simply show that you’re doing something:</p> <p><a href="http://blog.smoothfriction.nl/PostImages/BusyIndicator-Caliburn.Micro_ADFA/image.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto; padding-top: 0px" title="image" border="0" alt="image" src="http://blog.smoothfriction.nl/PostImages/BusyIndicator-Caliburn.Micro_ADFA/image_thumb.png" width="304" height="161" /></a></p> <p>The challenge is how to use this semantically in your MVVM solution, while not having to litter your code with value assignments to make the control active/inactive. A naïve implementation would more or less put a property on your view model, which you set/unset at the start/end of your action method. However, this has a few disadvantages:</p> <ol> <li>It’s error prone. What if your method doesn’t complete? Especially if you’re using the co-routines functionality available in Caliburn Micro (which I think is awesome, and you should really use this), you can assume this will be a common scenario. </li> <li>It means you have at the very least two extra lines of code in your action methods, which clutters your code. </li> <li>You’re repeating a lot of code. This goes against the DRY principle and makes change very difficult. </li> </ol> <p>I’ve come up with several approaches to solve this problem, all valid depending on your preferences and/or use-case within the application. I will showcase the first one here, and (hopefully) follow up later with the other approaches and explanation.</p> <h3>A solution</h3> <p>I am a big fan of the co-routine functionality within Caliburn.Micro. I tend to implement all my action methods as a co-routine, so I can quickly compose my actions from a predefined set of routines I have in my toolkit. Among these routines I have things like a confirmation dialog and a simple IResult implementation which takes an instance of System.Action as a constructor parameter. This allows me to take ANY code I want and stick it in a co-routine, essentially making it part of the execution pipeline of the framework. I’ll use that co-routine to adapt it a little bit to work with a busy indicator.</p> <p>Starting with a working Caliburn.Micro application, we have to add the busy indicator control to the project. So do this, simply add a package reference to <strong>SilverlightToolkit-Core</strong>. That’s where the control lives. The control itself works as a container, putting a semi-opaque (or semi-transparent if you like) layer over the contents, and putting a little progress dialog over the top, like you can see in the image at the start of the article. So we’ll take the stock ShellView.xaml and wrap everything in a BusyIndicator control. We should also add a button to activate the busy state:</p> <pre class="brush: xml;">&lt;UserControl x:Class="BusyIndicatorDemo.ShellView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit"&gt; &lt;toolkit:BusyIndicator IsBusy="{Binding IsBusy}" BusyContent="{Binding BusyMessage}"&gt; &lt;Grid Background="White"&gt; &lt;Button x:Name="DoSomething" Content="Do something" Height="20" Width="100" /&gt; &lt;/Grid&gt; &lt;/toolkit:BusyIndicator&gt; &lt;/UserControl&gt;</pre> <p>The BusyIndicator has a property called IsBusy which toggles the activity/inactivity of the control. simply put, when IsBusy equals true, the containing controls are disabled and the progress dialog is shown. What we’ll want to do is implement a method named ‘DoSomething’ on our view model which returns an IEnumerator&lt;IResult&gt;, which we can use to showcase the behavior. The method will just return a few instances of our co-routine to simulate a few long-running processes. In between each step we will set the status message to be displayed on the dialog. </p> <p>The view model:</p> <pre class="brush: csharp;">using System.Collections.Generic; using Caliburn.Micro; namespace BusyIndicatorDemo { using System.ComponentModel.Composition; [Export(typeof(IShell))] public class ShellViewModel : Screen, IShell, IHasBusyIndicator { public IEnumerator&lt;IResult&gt; DoSomething() { BusyMessage = "Step1"; yield return new SleepRoutine(1000); BusyMessage = "Step2"; yield return new SleepRoutine(4000); BusyMessage = "Step3"; yield return new SleepRoutine(500); BusyMessage = "Step4"; yield return new SleepRoutine(50); } public string BusyMessage { get { return _busyMessage; } set { _busyMessage = value; NotifyOfPropertyChange(() =&gt; BusyMessage); } } private string _busyMessage; public bool IsBusy { get { return _isBusy; } set { _isBusy = value; NotifyOfPropertyChange(() =&gt; IsBusy); } } private bool _isBusy; } }</pre> <p>And the co-routine:</p> <pre class="brush: csharp;">using System; using System.Threading; using System.Threading.Tasks; using Caliburn.Micro; namespace BusyIndicatorDemo { public class SleepRoutine : IResult { private readonly int _milliseconds; public string BusyMessage { get; set; } public SleepRoutine(int milliseconds) { _milliseconds = milliseconds; } public void Execute(ActionExecutionContext context) { Task.Factory.StartNew(() =&gt; Thread.Sleep(_milliseconds)) .ContinueWith(x =&gt; Completed(this, new ResultCompletionEventArgs())); } public event EventHandler&lt;ResultCompletionEventArgs&gt; Completed; } }</pre> <p>You might have noticed the IHasBusyIndicator interface on the view model. This is what we’ll use to determine if we should do something to show the status when invoking an action. In the Caliburn.Micro framework you can basically replace anything you want. We’re going to look at wrapping the code which executes a co-routine with our little busy indicator activator. Using the standard AppBootstrapper implementation, place the following code in your Configure method:</p> <pre class="brush: csharp;">var invokeAction = ActionMessage.InvokeAction; ActionMessage.InvokeAction = context =&gt; { var target = context.Target as IHasBusyIndicator; if (target != null) { target.IsBusy = true; EventHandler&lt;ResultCompletionEventArgs&gt; coroutineOnCompleted = (sender, args) =&gt; { }; coroutineOnCompleted = (sender, args) =&gt; { target.IsBusy = false; Coroutine.Completed -= coroutineOnCompleted; }; Coroutine.Completed += coroutineOnCompleted; } invokeAction(context); };</pre> <p>This code will basically check if the view model on which we are executing the action has a busy indicator property, and if so, set it when starting and unset it when done. If the view model does not implement the interface the code only runs the regular invoke action.</p> <p>However, this is not the most ideal approach. You still have to set the BusyContent property for every step (unless you simply want a general message), and the InvokeAction implementation seems a bit brittle. I’ve got a few different options to explore, which are a bit more involved. A small hint: the Dispatcher will come into play, and we’ll move the code responsible for showing the busy indicator around a little bit.</p> <strong>Update: I forgot putting in the code for the IHasBusyIndicator interface. Here it is:</strong> <pre class="brush:csharp">public interface IHasBusyIndicator { bool IsBusy { get; set; } }</pre> <img src="http://blog.smoothfriction.nl/aggbug/56.aspx" width="1" height="1" /> Erik van Brakel http://blog.smoothfriction.nl/archive/2012/04/20/silverlight-busyindicator-in-caliburn-micro.aspx Fri, 20 Apr 2012 14:33:04 GMT http://blog.smoothfriction.nl/archive/2012/04/20/silverlight-busyindicator-in-caliburn-micro.aspx#feedback http://blog.smoothfriction.nl/comments/commentRss/56.aspx Spring is in the air, time to update the template! general http://blog.smoothfriction.nl/archive/2012/04/17/spring-is-in-the-air-time-to-update-the-template.aspx <p>It’s cliché, but it’s the time of the year again for a good spring cleaning. I’ve updated my template to be sort of metro-ish, as a preparation for Windows 8 coming to us in the summer.</p> <p>The next few posts I will write will be a continuation of my previous post about the Joel Test. I got a good comment on that, saying that while the test has some valid points, it’s not an absolute measure of the quality of a company. While I agree with that statement, I also feel that there’s no harm in striving for perfection. Obviously the points which either require you to have plenty of human resources (god, I hate that word) or money, there’s a lot you can do with a little bit of creative interpretation and good use of the (free) tools at hand.</p> <p>I’ve also set out to dive into Windows Phone and WinRT development. I’m getting quite good at XAML based programming, doing Silverlight and WPF programming at my day job and all, so I figured the next step is to dive into the platforms which could use my help to increase the amount of quality apps available, while also making me filthy rich (note: insert sarcasm here). So expect some posts on how to get started in the not-so-near future.</p> <p>Another note: I changed my reluctantly changed my feedburner ID, because I couldn’t remember my details and honestly, the amount of subscribers was generally below the amount of fingers available on my both hands combined. So if you want to subscribe, use the button on the right, and I will try to keep a steady flow of braindumps coming your way!</p><img src="http://blog.smoothfriction.nl/aggbug/55.aspx" width="1" height="1" /> Erik van Brakel http://blog.smoothfriction.nl/archive/2012/04/17/spring-is-in-the-air-time-to-update-the-template.aspx Tue, 17 Apr 2012 21:44:18 GMT http://blog.smoothfriction.nl/archive/2012/04/17/spring-is-in-the-air-time-to-update-the-template.aspx#feedback http://blog.smoothfriction.nl/comments/commentRss/55.aspx Taking your startup to the next level: meet the Joel Test http://blog.smoothfriction.nl/archive/2012/01/02/taking-your-startup-to-the-next-level-meet-the-joel.aspx <p>Tomorrow, I will start my second year at my current employer. The company is fairly small, when I started there were two developers and the managing director. Effectively it was just the two of us though. Additionally, the managing director actually has a technical background, and trusts our judgment. This left us with a lot of room to make our own decisions on what’s important. There’s around 6 of us now, and things are going nice and steady.</p> <p>As one of my responsibilities I’ve decided on making the whole development process as painless as possible. I could of course try to think of all sorts of things to do myself, but luckily, there’s already a pretty good list out there already. <a href="http://www.joelonsoftware.com/">Joel Spolsky</a>, one of the people behind <a href="http://stackoverflow.com/">Stack Overflow</a> (the Q&amp;A site for and by software developers) wrote an article about this back in August 2000. Ages ago in software development terms. However, the list is still valid, and my aim was going to be to meet as many of the requirements I could.</p> <ol> <li>Do you use source control? </li> <li>Can you make a build in one step? </li> <li>Do you make daily builds? </li> <li>Do you have a bug database? </li> <li>Do you fix bugs before writing new code? </li> <li>Do you have an up-to-date schedule? </li> <li>Do you have a spec? </li> <li>Do programmers have quiet working conditions? </li> <li>Do you use the best tools money can buy? </li> <li>Do you have testers? </li> <li>Do new candidates write code during their interview? </li> <li>Do you do hallway usability testing? </li> </ol> <p><a href="http://www.joelonsoftware.com/articles/fog0000000043.html">The Joel Test</a> has 12 requirements. According to Joel, a proper software development team should score at least a 10 out of 12. Being a startup company, it’s probably expected that you wouldn’t be there right away. Additionally, doing mainly green-field development, some of these are not directly applicable. However, with some creativity it’s quite possible to find a solution for each one.</p> <p>For completeness sake, here’s the scoring before we started working on it:</p> <p>Do you use source control? <br /><em>Yes. We have a subversion server up and running, and we use it actively.</em></p> <p>Can you make build in one step? <br /><em>No.</em></p> <p>Do you make daily builds? <br /><em>No.</em></p> <p>Do you have a bug database? <br /><em>Yes. We use Mantis for that, and keep it as up-to-date as possible.</em></p> <p>Do you fix bugs before writing new code? <br /><em>No. Not by rule.</em></p> <p>Do you have an up-to-date schedule? <br /><em>No. We only have the main project deadlines to go by.</em></p> <p>Do you have a spec? <br /><em>No. We just wing it.</em></p> <p>Do programmers have quiet working conditions? <br /><em>No. We share an office with a consulting company. Lots of phone calls.</em></p> <p>Do you use the best tools money can buy? <br /><em>Yes. As far as money allows.</em></p> <p>Do you have testers? <br /><em>No. We test our code ourselves.</em></p> <p>Do new candidates write code during their interview? <br /><em>Yes. Or at least show code from pet projects and explain it.</em></p> <p>Do you do hallway usability testing? <br /><em>No. We simply don’t have people to do that between the two of us.</em></p> <p>Adding everything up this gives us an appalling 4 out of 12. Plenty of room for improvement I’d say!</p> <p>For my next posts I will explain how we’ve gone from a 4 out of 12 to somewhere between 8 and 10 out of 12.</p><img src="http://blog.smoothfriction.nl/aggbug/54.aspx" width="1" height="1" /> Erik van Brakel http://blog.smoothfriction.nl/archive/2012/01/02/taking-your-startup-to-the-next-level-meet-the-joel.aspx Mon, 02 Jan 2012 00:13:13 GMT http://blog.smoothfriction.nl/archive/2012/01/02/taking-your-startup-to-the-next-level-meet-the-joel.aspx#feedback http://blog.smoothfriction.nl/comments/commentRss/54.aspx Building your own Reflector with Mono.Cecil reflector c# http://blog.smoothfriction.nl/archive/2011/02/12/building-your-own-reflector-with-mono-cecil.aspx <p>After writing my previous post, <a href="http://blog.smoothfriction.nl/archive/2011/02/07/building-your-own-reflector-with-mono-cecil-and-the-codedom.aspx">Building your own Reflector with Mono.Cecil and the CodeDom</a>, I decided to find other initiatives to writing a ‘new Reflector’. I’ve now joined the Monoflector effort, working on filling the gaps in the current decompiler. It seems a good fit, because I already knew a few things about it after my small spike.</p> <p>We’re working with the decompiler which is part of the Cecil project. However, there are quite a few bugs or just lacking features which makes reading the generated code quite frustrating. We’ve forked Cecil and are working on adding the features we need. </p> <p>If you want to check out the project, I invite you to go to our github page at <a title="https://github.com/jcdickinson/Monoflector" href="http://github.com/jcdickinson/Monoflector">http://github.com/jcdickinson/Monoflector</a>. Feel free to fork the project and get your changes in, but keep in mind that at the moment of writing, the GUI is under quite heavy construction. It might not be a good idea to work on that, because it will be quite hard to integrate your changes into the main branch.</p><img src="http://blog.smoothfriction.nl/aggbug/50.aspx" width="1" height="1" /> Erik van Brakel http://blog.smoothfriction.nl/archive/2011/02/12/building-your-own-reflector-with-mono-cecil.aspx Sat, 12 Feb 2011 22:04:32 GMT http://blog.smoothfriction.nl/archive/2011/02/12/building-your-own-reflector-with-mono-cecil.aspx#feedback http://blog.smoothfriction.nl/comments/commentRss/50.aspx Building your own Reflector with Mono.Cecil and the CodeDom c# reflector http://blog.smoothfriction.nl/archive/2011/02/07/building-your-own-reflector-with-mono-cecil-and-the-codedom.aspx <p>Sparked by the news that Red Gate’s Reflector will not be available for free anymore starting early March (<a href="http://www.red-gate.com/products/dotnet-development/reflector/announcement">see announcement on the redgate site</a>), I decided to try how hard it would be to build your own reflector. Turns out, not too hard if you use the tools at hand. There’s actually already a nice effort going on based on the Mono.Cecil.Decompiler, but I only found out after I started my trials.</p> <p>What I basically did was get Mono.Cecil to read an assembly, hand me the MSIL statements and simply build an AST (Abstract Syntax Tree) out of that using the .NET CodeDom available in System.CodeDom.Compiler. Combined with the CSharp or VB code generators, available in Microsoft.CSharp and Microsoft.VisualBasic respectively, I managed to more or less decompile System.Object into valid C# code.</p> <p>The code very rough, and not tested more than just reading and converting the MSIL by hand and using that to check. However, I did find that for most things, a decompiler like this is not very hard to build. Just very time consuming. You can check out the code at my github repo, <a href="http://github.com/smoothfriction/SmoothReflections">here</a>. Most of the work is in the ILToCSharpConverter.cs file, which simply converts most of the MSIL from the System.Object class into an AST, which then gets fed through the code generator.</p> <p>Note: I excluded the GetFieldInfo method, because I ran into an infinite loop and didn’t want to try to fix that before publishing. Any other classes are NOT tested, and therefore will probably break. Also, the code generated is hardly complete or optimal, but it should more or less convey the meaning!</p> <p>If you want, have fun hacking away. Might actually be fun to get this off of the ground, no?</p><img src="http://blog.smoothfriction.nl/aggbug/49.aspx" width="1" height="1" /> Erik van Brakel http://blog.smoothfriction.nl/archive/2011/02/07/building-your-own-reflector-with-mono-cecil-and-the-codedom.aspx Mon, 07 Feb 2011 03:23:05 GMT http://blog.smoothfriction.nl/archive/2011/02/07/building-your-own-reflector-with-mono-cecil-and-the-codedom.aspx#feedback http://blog.smoothfriction.nl/comments/commentRss/49.aspx PowerShell fun: Project Euler #3 - The primary factors are... Powershell projecteuler math http://blog.smoothfriction.nl/archive/2011/01/30/powershell-fun-project-euler-3-the-primary-factors-arehellip.aspx <p>Onwards, to Project Euler problem #3. The problem is defined as below:</p> <blockquote> <p>The prime factors of 13195 are 5, 7, 13 and 29.</p> <p>What is the largest prime factor of the number 600851475143 ?</p> </blockquote> <p>So, basically, we need to find out which factors make up a certain number. The easiest way to do this is simply by trial division. A very naïve implementation would be something like this:</p> <pre class="brush: ps">function Get-PrimeFactors ([long]$number) { while ($number -gt 1) { for($i = 2; $i -le $number; $i++) { if($number % $i -eq 0) { $number = $number / $i; $i; break; } } } }</pre> <p> </p> <p>To be honest, for a number of this magnitude even the simplest of solutions will work. I’ve been looking at different integer factorization algorithms (using my trusty pal <a href="http://en.wikipedia.org/wiki/Integer_factorization">Wikipedia</a>), but none of them is simple enough to actually fit the problem. Most of them are actually really suitable for the problem, if the number was much, much bigger. </p> <p>However, there are a few optimizations to be made. For instance, we can assume that we only have to check for divisors up to the square root of the number in question, because anything higher than that has a pairing divisor which is lower than the root. For instance, let’s look at 42. $\sqrt{42} \approx 6.5$, so we only have to check up to (and including) 6. If we look at all possible solutions for $a \times b = 42$, we’ll see that none of them have two factors above 6:</p> <ul> <li>$2 \times 21$ </li> <li>$3 \times 14$ </li> <li>$7 \times 6$ </li> <li>$6 \times 7$ </li> <li>$14 \times 3$ </li> <li>$21 \times 2$ </li> </ul> <p>Besides that, we need to only look for prime factors. We know that all primes (except for the first one, being ‘2’) are odd. Adding all of these optimizations in, we’ll get something like this:</p> <pre class="brush: ps">function Get-PrimeFactors ([long]$number) { while($number % 2 -eq 0) { 2; $number /= 2; } for($i = 3; $i -le [Math]::Sqrt($number); $i+= 2) { while($number % $i -eq 0) { $number /= $i; $i; } } $number }</pre> <p> </p> <p>This is more or less a good enough solution for this problem. Using the number from the problem description, it will run take around 50ms to get all factors of that integer.</p> <p>As I’ve said, I’ve looked into a bunch of factorization algorithms, but they were all pretty much overkill for the problem at hand. As you see, the solution is found in 50ms. Even bigger numbers like 600851475143123, which is the product of two quite substantial primes, takes less than 30 seconds. I think this function might be useful for later Project Euler problems, so I’ll keep it in the library for now.</p><img src="http://blog.smoothfriction.nl/aggbug/48.aspx" width="1" height="1" /> Erik van Brakel http://blog.smoothfriction.nl/archive/2011/01/30/powershell-fun-project-euler-3-the-primary-factors-arehellip.aspx Sun, 30 Jan 2011 18:52:11 GMT http://blog.smoothfriction.nl/archive/2011/01/30/powershell-fun-project-euler-3-the-primary-factors-arehellip.aspx#feedback http://blog.smoothfriction.nl/comments/commentRss/48.aspx PowerShell fun: Project Euler #2 - Fibonacci summed up projecteuler Powershell math http://blog.smoothfriction.nl/archive/2011/01/25/powershell-fun-project-euler-2-fibonacci-summed-up.aspx <p>The first few Project Euler problems are actually pretty trivial to brute force, but sometimes they have a smart solution. All in all, coming up with a semi-smart title for the post is usually harder than the problem itself. But I don’t want to leave anything out, so I’ll just continue on with problem #2:</p> <blockquote> <p>Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:</p> <p>1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...</p> <p>By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.</p> </blockquote> <p>The Fibonacci sequence is a quite classic integer sequence, named after Leonardo of Pisa, aka Fibonacci. Apparently they’re used in the analysis of financial markets and much more, but I have never used it in the wild so far. </p> <p>Besides that, because this solves so blazingly fast (even for numbers up to 10<sup>20</sup>). This is simply because the Fibonacci sequence gets quite big, quite fast. So I just solve this using a very simple bit of PowerShell code.</p> <pre class="brush: ps">function Get-Fibonacci ($max) { $current = $previous = 1 $previous; while($current -lt $max) { $current; $current, $previous = ($current + $previous), $current } } $measure = Get-Fibonacci 4000000 | where {$_ % 2 -eq 0} | measure-object -sum Write-Host $measure.Sum</pre> <p> </p> <p>One thing I did find was that a recursive solution doesn’t really work, at least not in PowerShell. The default call depth is set to 100 recursive calls at most, which you will hit eventually. Not in this case though, but when testing for higher numbers it would fail. However, I think that while a calculating a Fibonacci sequence is a nice exercise to learn about recursion, it isn’t a good example of when to use recursion in the first place.</p><img src="http://blog.smoothfriction.nl/aggbug/47.aspx" width="1" height="1" /> Erik van Brakel http://blog.smoothfriction.nl/archive/2011/01/25/powershell-fun-project-euler-2-fibonacci-summed-up.aspx Tue, 25 Jan 2011 01:37:45 GMT http://blog.smoothfriction.nl/archive/2011/01/25/powershell-fun-project-euler-2-fibonacci-summed-up.aspx#feedback http://blog.smoothfriction.nl/comments/commentRss/47.aspx PowerShell fun: Project Euler #1 - Gauss being smart Powershell projecteuler http://blog.smoothfriction.nl/archive/2011/01/23/powershell-fun-project-euler-1-gauss-being-smart.aspx <p>I am not at all sure how far I will get with this, but I am going to try. So far I have solved the first 7 problems from <a href="http://projecteuler.net">Project Euler</a>, using PowerShell. I will, whenever I solve a problem, take my time to write about my solution and what I’ve learned about math and PowerShell. It should be a good read for anyone not particularly a wizard at math, because I don’t have a math major either. Everything I write here should be in reasonably understandable terms, because I need to understand it too!</p> <p>Either way, let’s start with the first problem, defined as below.</p> <blockquote> <p>If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.</p> <p>Find the sum of all the multiples of 3 or 5 below 1000.</p> </blockquote> <p>This is easily solvable using a simple formula, but first, a naïve script to calculate this:</p> <pre class="brush: ps">(1..999) | where {$_ % 3 -eq 0 -or $_ % 5 -eq 0} | measure -sum | select Sum</pre> <p> </p> <p>So basically, what I do here is create an array ranging from 1 to 999, select all items which are divisible by 3 or 5, and add them together. Then, for good measure, I select the ‘Sum’ property of the measure info object.</p> <p>What I learned here is basically how to create a range (similar to Enumerable.Range in C#), and how to measure a variable in PowerShell. </p> <p>This is not the optimal solution. This problem is actually best solved simply by using a simple formula for calculating the sum of a range of numbers:</p> <blockquote> <p>$$\sum_{i=1}^n i = {n^2 + n \over 2}$$</p> </blockquote> <p>Basically, for any range of numbers with a regular interval you can find the sum with this formula. Something even more interesting is that it is possible to also find the sum for a sequence of $i^2$, $i^3$, and so on. This will be useful for one of the next problems, so I will elaborate on that when I get there.</p> <p>It’s easy to prove how this formula works. First, we take the sequence and put the reverse sequence below it, just for visualization. For the example I’ve taken {1,2,3,4,5}, it’s a bit shorter:</p> <pre>1 2 3 4 5 5 4 3 2 1</pre> <p> <br />What’s so special about this? Well, every pair of the top and bottom row is equal to exactly 6, or (n+1). How many pairs are there? Well, that’s easy. In this case, 5, but in a general case, we could say there are ‘n’ pairs. So the sum of these pairs is equal to: $(n+1)n$ which can be reduced to $n^2 + n$. However, because we’ve duplicated the sequence, our sum is now twice the amount we’re looking for. Simply divide by two (= remove the duplicate sequence) and voila: we have our formula!</p> <p>However, we’re not looking for {1,2,3,..,n}. We’re looking for all terms divisible by 3 or 5, or {3,6,9,..,n} and {5,10,15,..,n}. Luckily, we can just multiply the sequence for {1,2,3,..n} by 3 and 5 to get those. We just need to find the upper bound for both, to make sure the $3n$ and $5n$ won’t be greater than 1000. In other words, less than or equal to 999. It’s not much trouble calculating this:</p> <p>999/5 = 199.8. Round down to 199. <br />999/3 = 333. No rounding needed.</p> <p>But wait! we also need to make sure we’re not adding values twice. Every time a value is divisible by 3 and 5, we’ll add it twice if we simply add the two sequences. So we’ll need to make sure that the values divisible by 15 (= 3 * 5) are removed from the sequence once. Again, let’s find the upper limit:</p> <p>999 / 15 = 66.6, round down to 66.</p> <p>Adding all these values gives us the following equations:</p> <p>$$\sum_{i=1}^{333} 3i + \sum_{i=1}^{199} 5i - \sum_{i=1}^{66} 15i = 3{{333^2 + 333} \over 2} + 5{{199^2 + 199} \over 2} - 15{{66^2 + 66} \over 2}$$</p> <p>Problem solved, with and without programming! As for the title, I’ll leave it as an exercise for you as a reader to look up the story about little Carl Friedrich Gauss, and how it relates to this problem and it’s solution.</p><img src="http://blog.smoothfriction.nl/aggbug/46.aspx" width="1" height="1" /> Erik van Brakel http://blog.smoothfriction.nl/archive/2011/01/23/powershell-fun-project-euler-1-gauss-being-smart.aspx Sun, 23 Jan 2011 23:50:44 GMT http://blog.smoothfriction.nl/archive/2011/01/23/powershell-fun-project-euler-1-gauss-being-smart.aspx#feedback http://blog.smoothfriction.nl/comments/commentRss/46.aspx