A customer had the requirement to develop his solution with .NET 3.0 framework.
I was already used to the new and nice features of .NET 3.5 framework, especially the class extension feature.
A simple example for a class extension would be like this:
In order to use the same feature under .NET 3.0 you can use the following trick:
First create an empty class file (e.g. AttributeExtension.cs) and add this class to your project.
Secondly put the following code in that class:
Visual Studio 2008 will recognise the extension and there you go. From now on class extensions are available for .NET 3.0 applications.
I was already used to the new and nice features of .NET 3.5 framework, especially the class extension feature.
A simple example for a class extension would be like this:
public class ReverseString
{
public static void Main()
{
string s = "Hello World!";
string r = s.Reverse();
}
}
static class StringHelpers
{
public static string Reverse(this string s)
{
char[] c = s.ToCharArray();
Array.Reverse(c);
return new string(c);
}
}
In order to use the same feature under .NET 3.0 you can use the following trick:
First create an empty class file (e.g. AttributeExtension.cs) and add this class to your project.
Secondly put the following code in that class:
using System;
namespace System.Runtime.CompilerServices
{
/// <summary>
/// Indicates that a method is an extension method,
/// or that a class or assembly contains extension methods.
/// </summary>
[AttributeUsage(AttributeTargets.Method)]
public sealed class ExtensionAttribute : Attribute
{
/// <summary>
/// Initializes a new instance of the <see cref="ExtensionAttribute"/> class.
/// </summary>
public ExtensionAttribute() { }
}
}
Visual Studio 2008 will recognise the extension and there you go. From now on class extensions are available for .NET 3.0 applications.
Comments
Post a Comment