Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
250 views
in Technique[技术] by (71.8m points)

asp.net - C# 7 local function not working as expected and no errors being displayed

I have an Asp.Net MVC App running with framework version .NET 4.5 and I'm using VS2017 pro version. Users can upload attachments including but not limited to:

  • Excel
  • Word
  • PowerPoint
  • pdf
  • jpeg
  • png

So I have a private function which is as follows:

private string ImageExtension(string path)
{
  string noImagePath = HttpContext.Current.Server.MapPath("~/Content/images/Template/No-Image-Available-100x100.jpg");
  string fileExtension = System.IO.Path.GetExtension(path);
  switch (fileExtension)
  {
    case ".jpg":
    case ".jpeg":
    case ".gif":
    case ".png":
       return path;
    default:
       return noImagePath;
   }
}

As you can see it's very simple and does not do anything fancy. As I'm only using this in one place I thought about making the use of new C# 7 feature of local function. So I've gone ahead and created my main function as follows and added ImageExtension(string path) inside it.

public void BugInfo(HttpPostedFileBase file)
{
  if(file != null && file.ContentLength > 0)
  {
    string fileName = file.FileName;
    string directoryPath = "directory path";

     //save path of 
     string savePath = System.IO.Path.Combine(directoryPath, fileName);
     string testString = ImageExtension(savePath);

     string ImageExtension(string path)
     {
        string noImagePath = HttpContext.Current.Server.MapPath("~/Content/images/Template/No-Image-Available-100x100.jpg");
        string fileExtension = System.IO.Path.GetExtension(path);
        switch (fileExtension)
        {
          case ".jpg":
          case ".jpeg":
          case ".gif":
          case ".png":
             return path;
          default:
             return noImagePath;
         }
      }
    }
  //save values to db here
 }

With the above code my project builds without any errors. As soon as I hit F5 or Ctrl + F5 I get the following error screen

enter image description here

If I check in the ErrorList to see if there are any errors I get none at all as you can see below.

enter image description here

Can someone tell me where I'm going wrong please. Do I have to change any settings or need to include any additional DLLs to make use of C# 7 features.

Looking at this answer it seems like all of the C# 7 features should work on .NET 4.5

question from:https://stackoverflow.com/questions/43179862/c-sharp-7-local-function-not-working-as-expected-and-no-errors-being-displayed

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need to update nuget package named "Microsoft.Net.Compilers" to the latest version. Most likely you have version 1.3.2 installed in your project but you need 2.0.1 to use C# 7 features. Alternatively - you can remove this package at all (together with packages that depend on it) - then it will also work, because it will use your installed compiler, but I don't recommend doing that.

As this package description says:

.Net Compilers package. Referencing this package will cause the project to be built using the specific version of the C# and Visual Basic compilers contained in the package, as opposed to any system installed version.

So that is why it uses C# 6 compiler for you now.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...