2016-03-25 14 views
9

Jak korzystać z funkcji C# 6.0 bez Visual Studio?C# 6.0 bez Visual Studio

Poniższy C# 6.0 Code nie działa:

using static System.Console; 

class Program 
{ 
    static void Main() 
    { 
     WriteLine("Hello world!"); 
    } 
} 

Oto Błąd:

>csc CS6.cs 
Microsoft (R) Visual C# Compiler version 4.6.1055.0 
for Microsoft (R) .NET Framework 4.5 
Copyright (C) Microsoft Corporation. All rights reserved. 

CS6.cs(1,7): error CS1041: Identifier expected; 'static' is a keyword 
CS6.cs(1,14): error CS1518: Expected class, delegate, enum, interface, or struct 

Oto C# kod do determine which .NET Framework versions are installed:

using System; 
using Microsoft.Win32; 

class RegistryVersion 
{ 
    private static void GetVersionFromRegistry() 
    { 
     // Opens the registry key for the .NET Framework entry. 
      using (RegistryKey ndpKey = 
       RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, ""). 
       OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP\")) 
      { 
       // As an alternative, if you know the computers you will query are running .NET Framework 4.5 
       // or later, you can use: 
       // using (RegistryKey ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, 
       // RegistryView.Registry32).OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP\")) 
      foreach (string versionKeyName in ndpKey.GetSubKeyNames()) 
      { 
       if (versionKeyName.StartsWith("v")) 
       { 

        RegistryKey versionKey = ndpKey.OpenSubKey(versionKeyName); 
        string name = (string)versionKey.GetValue("Version", ""); 
        string sp = versionKey.GetValue("SP", "").ToString(); 
        string install = versionKey.GetValue("Install", "").ToString(); 
        if (install == "") //no install info, must be later. 
         Console.WriteLine(versionKeyName + " " + name); 
        else 
        { 
         if (sp != "" && install == "1") 
         { 
          Console.WriteLine(versionKeyName + " " + name + " SP" + sp); 
         } 

        } 
        if (name != "") 
        { 
         continue; 
        } 
        foreach (string subKeyName in versionKey.GetSubKeyNames()) 
        { 
         RegistryKey subKey = versionKey.OpenSubKey(subKeyName); 
         name = (string)subKey.GetValue("Version", ""); 
         if (name != "") 
          sp = subKey.GetValue("SP", "").ToString(); 
         install = subKey.GetValue("Install", "").ToString(); 
         if (install == "") //no install info, must be later. 
          Console.WriteLine(versionKeyName + " " + name); 
         else 
         { 
          if (sp != "" && install == "1") 
          { 
           Console.WriteLine(" " + subKeyName + " " + name + " SP" + sp); 
          } 
          else if (install == "1") 
          { 
           Console.WriteLine(" " + subKeyName + " " + name); 
          } 
         } 
        } 
       } 
      } 
     } 
    } 

    static void Main() 
    { 
     GetVersionFromRegistry(); 
    } 
} 

Oto wyjściowa dla C# kod aby ustalić, które wersje .NET Framework są zainstalowane:

>csc FrameworkVersion.cs 
Microsoft (R) Visual C# Compiler version 4.6.1055.0 
for Microsoft (R) .NET Framework 4.5 
Copyright (C) Microsoft Corporation. All rights reserved. 

>FrameworkVersion.exe 
v2.0.50727 2.0.50727.4927 SP2 
v3.0 3.0.30729.4926 SP2 
v3.5 3.5.30729.4926 SP1 
v4 
    Client 4.6.01055 
    Full 4.6.01055 
v4.0 
    Client 4.0.0.0 

Należy pamiętać, że to pytanie jest o C# 6.0 bez Visual Studio IDE/.

+0

Jak to skompilować bez Visual Studio? Czy możesz dołączyć polecenie budowania? –

+0

Spójrz na to pytanie: http://stackoverflow.com/questions/31875825/how-to-run-roslyn-instead-csc-exe-from-command-line To prawdopodobnie to samo. Najprawdopodobniej masz uruchomioną złą wersję (z błędnej ścieżki) csc.exe. –

+0

Należy pamiętać, że używa się wiersza poleceń i .NET Framework 4.6 (NDP461-KB3102436-x86-x64-AllOS-ENU.exe), a nie Visual Studio . – Impavid

Odpowiedz

4

C# 6 nie jest zbudowany w kierunku konkretnej wersji ramowej, wszystko zależy od kompilatora. Jeśli korzystasz z kompilatora C# 6, możesz skompilować ten fragment.

Upewnij się, że masz zainstalowaną najnowszą wersję MSBuild na komputerze kompilowanym (przenieśli kompilator z folderu frameworka). Ta instalacja zawiera najnowszy kompilator. (Klienci nie muszą uruchamiać najnowszej wersji frameworka, jeśli twój projekt jest przeznaczony do budowania dla niższej wersji framework).

Możesz również zainstalować pakiet NuGet kompilatora za pomocą wiersza poleceń NuGet.

Przydatne zasoby:

3

Twój problem jest z kompilatora

>csc CS6.cs 
Microsoft (R) Visual C# Compiler version -->4.6.1055.0<-- 
for Microsoft (R) .NET Framework 4.5 

Używasz 4.6 kompilatora, kiedy trzeba się 6 kompilatora.

C# 6 kompilator jest instalowany z Visual Studio, jednak wygląda na to, że ktoś zdołał go zainstalować niezależnie od IDE

https://social.msdn.microsoft.com/Forums/vstudio/en-US/9959d4c1-16fe-45bc-9535-7b0775d26e9a/please-confirm-c-60-compiler-is-not-installed-with-net-framework-46-if-so-location?forum=csharpgeneral

http://www.microsoft.com/en-us/download/details.aspx?id=48159 Microsoft Build Tools 2015 It says it only depends on .NET Framework 4.5... I guess the Roslyn-based C# 6.0 compiler can target .NET Framework 4.5? What is this thing's relationship to the .NET Framework 4.6 Targeting pack? Will that install this? Or are both needed? And like I said, can I compile using C# 6 language features in VS2013?
2

próbować używać kompilatorów z tych miejsc:

%ProgramFiles(x86)%\MSBuild\12.0\Bin\ 
%ProgramFiles(x86)%\MSBuild\12.0\Bin\amd64\ 
%ProgramFiles(x86)%\MSBuild\14.0\Bin\ 
%ProgramFiles(x86)%\MSBuild\14.0\Bin\amd64\ 
Powiązane problemy