Extension Methods in .NET Framework 2.0 Apps
Extension methods are not doing anything in the underlying IL code that could not be done using Visual Basic 2005; they are simply making a shared method call. What has changed is the compiler's ability to identify specific methods and allow the change in the calling method syntax, which gives the appearance that the type being used has new functionality and makes the extensions work just like the existing instance methods.
Another new feature in Visual Basic 2008 is multi-targeting, which lets you use Visual Basic 2008 to write code that targets a specific version of the Framework. Since the same compiler is used for multi-targeting, it is possible to use extension methods for applications targeted, for example, to version 2.0 of the Framework. This does take a little workaround, as the IDE prevents adding the required references to a .NET Framework 2.0 targeted application.
If you create a 3.5 application and change the target Framework to version 2.0 (using Project Properties | Compiler | Advanced | Target Framework), you'll see some of the references highlighted as missing, including those containing most of the out-of-the-box LINQ functionality. Trying to add these references to a 2.0 project results in a dialog informing you that they require a different version of the targeted Framework (see Figure 1).
References for Changed Target to the .NET Framework 2.0
Extension methods can be created and used for 2.0 applications after you have created your own System.Runtime.CompilerServices.Extension attribute. You must first create a new project targeting version 2.0. The default references will not include System.Core, which contains the extension attribute. In the Applications Project Properties, go to the Application Tab, clear the Root Namespace, and then create your own extension attribute in the correct System.Runtime.CompilerServices namespace. The code in Figure 2 shows the creation of the extension attribute and then using this to declare and use extension methods. Once you compile the application, you can call the extension methods.
Extension methods can be created and used for 2.0 applications after you have created your own System.Runtime.CompilerServices.Extension attribute. You must first create a new project targeting version 2.0. The default references will not include System.Core, which contains the extension attribute. In the Applications Project Properties, go to the Application Tab, clear the Root Namespace, and then create your own extension attribute in the correct System.Runtime.CompilerServices namespace. The code in Figure 2 shows the creation of the extension attribute and then using this to declare and use extension methods. Once you compile the application, you can call the extension methods.
Here are some important considerations regarding 2.0-targeted applications. First, type inference is off by default. To turn it back on, use Option Infer On. You'll probably want to do when you migrate .NET Framework 2.0 applications to the version 3.5.
Second, to use extension methods, you need to create your own extension attribute that mimics the one in System.Core. The IDE will prevent you from using the System.Core reference on a 2.0-targeted project.
Finally, this technique will not work for ASP.NET applications targeting the .NET Framework 2.0 because they have a runtime dependency on the 2.0 command-line compiler. When those apps are deployed to Web servers that only have the .NET Framework 2.0 installed, they will not work because the 2.0 CSC.EXE command-line compiler does not understand extension methods.
Even if you don't want to start writing applications for the .NET Framework 3.5 or you have existing applications in version 2.0 that you have to maintain, you can still benefit from upgrading to Visual Basic 2008. The extension method functionality is built into the compiler and can be used for any version of the Framework that the Visual Basic 2008 compiler is able to target.
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Runtime.CompilerServices;
static class Module1
{
public static void Main()
{
int i_x = 1;
string i_s = "Test";
dynamic x = i_x.IntegerExtension;
Interaction.MsgBox(x);
dynamic y = i_s.StringExtension;
Interaction.MsgBox(y.ToString());
}
}
//CREATE A USER-DEFINED ATTRIBUTE CALLED EXTENSION IN THE CORRECT NS
namespace System.Runtime.CompilerServices
{
[AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
class ExtensionAttribute : Attribute
{
}
}
//DEFINED EXTENSION METHODS WITH USER-DEFINED ATTRIBUTE ON THEM
static class ExtMethods
{
[Extension()]
public static string StringExtension(string a)
{
return "Success";
}
[Extension()]
public static int IntegerExtension(int a)
{
return 100;
}
}
No comments:
Post a Comment