How to make my code break when migrating to .NET Framework 4.0

I want local compiler errors to apply to methods that need to be changed after upgrading to the .NET Framework 4.0. See below:

Oops, although this is just an example, this code does not fully work, which I just realized. (A patched version is available at the end if you ever need this 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 leads to a compiler error after the transition?

This code actually does what the original version intended. This code also does not apply to version 3.5 at run time, however there are even simpler ways to get 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; } 
+4
source share
5 answers

I think there is no inline preprocessor character, but you can roll it:

 public static RouteData GetRouteData(this HttpContext context) { if (context != null) { return context.Items[_requestDataKey] as RouteData; } #if NET_4 #error Review code for .NET #endif // .NET 4.0 equivalent //if ((context != null) && (context.Request != null)) //{ // return context.Request.RequestContext.RouteData; //} return null; } 

Then, when you compile your projects for .NET 4, add the "NET_4" symbol to the compiler symbols (via the properties of the VS.NET project or directly into the .csproj file).

Of course, this requires that you know the places of interest (that is, through code verification). I think there is no way to automatically do this.

+4
source

Use Environment.Version .

Returns a Version object that describes the major, minor, string, and revision numbers of the common language runtime.

You can use this in your method to print errors, throw exceptions, etc.

+2
source

You can use the Obsolete attribute for the method in question.

 [Obsolete("Upgrade Method to .NET 4.0", true)] 

A boolean at the end indicates that the compiler should generate an error if this method is used. Therefore, for your reason, you can pass false here and force it to generate a warning so that your program is still functioning, but provides the feedback that is needed to update the method.

+1
source

You can use conditional compilation to achieve the result, but not the method you need. If you see this answer , it describes the setting of conditional values. Define .NET 3.5 for targeting and wrap the old code in it. Wrap the new code in IFNDEF or something similar. I myself did not use conditional compilation, but the angle that I first thought about.

+1
source

Well, runtime errors are easy to create, but compile-time errors are a bit more complicated. First, do you want to discover a new version of the .NET Framework or a new version of the compiler? I do not know how to determine the difference in runtime at compile time.

However, it is possible to cause an error when compiling with the Visual Studio 2010 compiler (as opposed to the 2008 version). Let's look at a list of C # 2010 change changes at http://msdn.microsoft.com/en-us/library/ee855831(VS.100).aspx .

The difference in the action of the zero-coalescing operator with respect to uninitialized local variables seems to be a good candidate for this.

 #pragma warning disable 0219 bool? i; bool? j; // TODO: Update this method for .NET 4 and remove this section. bool? createCompilerErrorOnVS2010 = (i = true) ?? j; #pragma warning restore 0219 

The above code will compile with the Visual Studio 2008 compiler, but not with the Visual Studio 2010 compiler.

+1
source

Source: https://habr.com/ru/post/1334907/


All Articles