I'd like to cause compiler errors local to methods that need to be changed once I make the transition to .NET Framework 4.0. See below:
Oops, while this is just an example, this code totally does not work, something I just realized. (a corrected version is available at the end, if you ever need this kind of functionality)
// ASP.NET 3.5 does not contain a global accessor for routing data
// this workaround will be used until we transition to .NET 4.0
// this field still exists in .NET 4.0, however, it is never used
// GetRouteData will always return null after the transition to .NET 4.0
static object _requestDataKey = typeof(UrlRoutingModule)
.GetField("_requestDataKey", BindingFlags.NonPublic | BindingFlags.Instance)
.GetValue(null)
;
public static RouteData GetRouteData(this HttpContext context)
{
if (context != null)
{
return context.Items[_requestDataKey] as RouteData;
}
// .NET 4.0 equivalent
//if ((context != null) && (context.Request != null))
//{
// return context.Request.RequestContext.RouteData;
//}
return null;
}
Does anyone know of a trick that results in a compiler error once the transition is made?
This code actually does what the original version intended. This code is also not specific to the 3.5 version of the runtime, however, there are simpler yet ways of getting at the route data in 4.0.
if (context != null)
{
var routeData = context.Items["RouteData"] as RouteData;
if (routeData == null
&& !context.Items.Contains("RouteData"))
{
context.Items["RouteData"] = routeData = RouteTable.Routes.GetRouteData(new HttpContextWrapper(context));
}
return routeData;
}