Compiler Version vs. NET Framework Version - Scena

2019-06-22 00:09发布

问题:

Scenario:

I have VS 2010 (C# 4 compiler) targeting 3.5 on my client machine.

I am developing ASP.NET applications. I use optional parameters (C# 4 supported) in a class file and compile the code everything appears to work fine.

Later on an issue that is discovered at Runtime where an old (classic I believe) ASPX is using the function. No function accepts only x arguments where x is one less than the optional parameter is the runtime error.

Does this mean normal classes and such use the C# compiler of the client, while the views (aspx) and such use the compiler on the server - thus causing issues if C# 4 is used in view/form files?

回答1:

I believe this has to do with the fact that aspx pages are (re?)compiled on first load by IIS rather than being compiled in Visual Studio. This allows them to be updated on the fly without recompiling, however since they're compiled by IIS it brings the complications you're seeing.

I'm not aware of a way to have IIS use the C#4 compiler but compile to .NET 3.5 so it seems like your only options are:

  1. Update to .NET 4
  2. Don't use optional parameters
  3. Don't call code that uses optional parameters in your .aspx files. I'm guessing if you move the calls to the codebehind file it should work fine, but I haven't tried it.