Framework Tips
All .NET delegates inherit (indirectly) from System.Delegate class (and directly from System.MulticastDelegate) which has a static CreateDelegate method. The method has two powerful characteristics that are not widely known. All delegate types are implemented by the runtime. What do I mean by that? Let’s have a look at how any delegate type looks at the IL level: All methods are runtime managed, meaning that, in a similar fashion you provide implementation for interface methods, runtime provides implementation for delegate methods. Take a look at the constructor. Regardless of delegate type it always has two arguments:...
As a follow up to my previous post, here’s the simplified – Console based version of the code. Disclaimer. This is a solution. Not the best one, not recommended by anyone, just the one that happened to solve my problem. So take it with a (big) grain of salt, and if you know a better one, use the Leave your comment function below. using System;
using System.ComponentModel.Composition;
using System.Reflection;
[assembly:AllowNonPublicComposition]
namespace ConsoleApplication1
{
class Program
...
Part of a project I’m working on, has strong extensibility requirements, so for last two days I’ve been working on a proof of concept prototype for it, using MEF. Not only do we need plug-ins, but also there will be at least two client applications using them (one in WPF, and the other one in Silverlight), so we decided to decouple the view of the plug-in from the logic behind it using something along the lines of MVP (Presenter First would be the closest). That is where I hit the wall. I decorated my classes and interfaces with...
Lately while going through the code of latest MEF release, I stumbled upon a piece of code that used a very little known feature of .NET Just take a look: public struct Tuple<TFirst, TSecond>
{
public Tuple(TFirst first, TSecond second)
{
this = new Tuple<TFirst, TSecond>();//looks strange?
this.First = first;
this.Second = second;
...
Yes, I know that previous Framework Tips had number VIII, but I didn’t notice that I already had VIII, so this makes this part 10th in the row. Each enum, is a named collection of flags, that mask a numeric type. For example: public enum ErrorCodes
{
FileNotFound = 0x12,
OutOfDiskSpace = 0x14
}
You could use this enum in an error handling method like:
public void HandleError(ErrorCodes errorCode)
{
switch(errorCode)
...
I have somewhat mixed feelings towards enums in C#. On the one hand, they can greatly improve readability of your code, but on the other hand, they are not much more than textual masks on numeric values. You can't inherit from them, you can't use enum as generic constraint (for which I see no good reason), and you can't extend them... Or can you? With the addition of Extension Methods in C# 3.0 you finally have the tools to put some life in them. Consider you have an enum like this:public enum Mp3Player
{
IPodClassic,
...
In .NET < 3.5 the only collections you could initialize inline were arrays. So this was legal: public class CollectionTest{ public static readonly ICollection<string> _list = new string[]{"one","two","three"};}
However if you wanted to have List instead of array, you had to use a trick and pass array as constructor parameter:
public static readonly ICollection<string> _list = new List<string>(new string[]{"one","two","three"});
Not the most elegant piece of code, but at least it works. So far so good. What if you wanted to...
Joe Developer once had to read options from configuration file, where each line looked basically like this: option: first;second;third;;fifth; Each line consisted of option name, followed by few spaces, and its parameters delimited by semicolons. Now the question is: how would Joe get to those parameters? The most elegant way would be using Regular Expressions, but Joe has strong allergy to them. For this simple example String.Split method will do. Joe rolled up his sleeves and crafted that beautiful masterpiece of code: 1: ...
Regular Expressions are very powerful and useful, but one thing that stops them from being widely used, is their complexity. They're commonly referred to as write-only language. Even my colleague, who is very proficient at crafting complex regular expressions often deletes the whole thing and starts from the scratch. For easy tasks however, like matching a literal piece of text, you can use two static methods of Regex class: Escape and Unescape. Escape escapes all characters that have special meaning in regex language, and Unescape, does the opposite thing. ...
You can call extension methods on null elements. It's obvious when you think about it: its a normal static method where you specify its first parameter with this. using System;namespace ExtensionMethods2{ class Program { static void Main(string[] args) { string isNull = null; Console.WriteLine(isNull.IsNullOrEmpty()); string isNotNull =...
StringBuilder is a class that allows you to manipulate strings in mutable manner. It has many methods allowing you to Append, Insert and Replace, portions of the string. However it does not contain Clear() method, that would allow you to clear the content of a StringBuilder.There is Remove(int,int) method, that allows you to do this, but it requires you to pass two parameters to achieve this. int startIndex = 0; stringBuilder.Remove(startIndex, stringBuilder.Length);
There is however easier, and more elegant way to do this. Other than in case of most classes in the framework, StringBuilder's Length...