Category Archives: C#

Quick reminder about the workings of Type.IsAssignableFrom

Here is a quick reminder about the workings of Type.IsAssignableFrom: class Fruit {} class Banana : Fruit {} [Test] public void CanAssignBananaToFruit() { var fruit = typeof (Fruit); var banana = typeof (Banana); Assert.IsTrue(fruit.IsAssignableFrom(banana)); } [Test] public void CanNotAssignFruitToBanana() { … Continue reading

Posted in C# | Leave a comment

Removing Dead Tracks (Duplicates that don’t exist) from iTunes using C#

Last week i noticed the following post from Scott Hanselman: Removing Dead Tracks (Duplicates that don’t exist) from iTunes using C#. As a good boy scout i noticed that these days iTunesLib.IITTrackCollection inherits from IEnumerable so i rewrote the code … Continue reading

Posted in C# | Leave a comment

Sometimes you can write it better than Resharper

Here is a real-life example of when people are much better expressing intent than a tool: Consider the following code from a typical Silverlight Navigation application: foreach (UIElement child in LinksStackPanel.Children) { var hb = child as HyperlinkButton; if (hb … Continue reading

Posted in C# | Leave a comment

WCF REST: generate correct Content-Length header for HEAD request

The point of a HEAD request is to return a Content-Length header, but with an empty body. The WCF transport stack has the annoying ‘feature’ that it ‘corrects’ the Content-Length header based on the stream that is returned. With the … Continue reading

Posted in C# | Leave a comment

Support both GET and HEAD requests on the same method with WCF REST

A while ago i had to modify an existing WCF REST service which was being consumed by BITS. Apparently the implementation has changed in Windows7 in such a way that the BITS client first makes a HEAD request to discover … Continue reading

Posted in C# | Leave a comment

Enumerating SpecialFolders

Environment.SpecialFolder is a value-type that i always seem to forget about. Let’s try to do something about that by posting about it here foreach (var name in Enum.GetNames(typeof(Environment.SpecialFolder))) { var specialFolder = (Environment.SpecialFolder)Enum.Parse(typeof(Environment.SpecialFolder), name); Console.WriteLine("{0,25} => {1}", name, Environment.GetFolderPath(specialFolder)); } … Continue reading

Posted in C# | Leave a comment

Get root directory for IsolatedStorageFiles

Sometimes you want to know the absolute path of a file that is persisted with IsolatedStorageFile. Apparently there is an internal property RootDirectory which contains this information: public static class IsolatedStorageFileExtensions { public static string GetRootDirectory(this IsolatedStorageFile isf) { var … Continue reading

Posted in C# | Leave a comment

Exploring System.Interactive

A couple of weeks ago i was working on an application that would transfer data through a couple of components as a List<object>. In essence, all we were doing over and over again was the following: interface IMapper<TEntity> { TEntity … Continue reading

Posted in C# | Leave a comment

Learned something from Resharper: Enumerable.OfType<TResult>

A couple of weeks ago i discovered Enumerable.OfType<TResult> when i let Resharper rewrite my code as a Linq statement. Here is the original code: var selectedPersons = new List<PersonSelectItem>(); foreach(var selectedItem in selectedItems) { var selectedPerson = selectedItem as PersonSelectItem; … Continue reading

Posted in C# | Leave a comment

Calculate EndpointAddress for Silverlight client

Because Silverlight checks the origin it considers http://localhost and http://127.0.0.1 as different locations. In case you want your visitors to be able to use both addresses you can recalculate the address as following: EndpointAddress GetEndpointAddress(EndpointAddress endpointAddress) { var scheme = … Continue reading

Posted in C#, Silverlight | Leave a comment