0

How I can get c# version from my code on run time? I use .net core. For example I can get framework version with this code:

FrameworkName = Assembly
    .GetEntryAssembly()?
    .GetCustomAttribute<TargetFrameworkAttribute>()?
    .FrameworkName

And I need something similar for C# version.

stepozer
  • 1,143
  • 1
  • 10
  • 22

1 Answers1

5

There is no such thing. The language version is completely lost in the compilation process, as the end result is just compiled IL code.

The fact that you used C# 6 or C# 7 or Visual Basic.NET is not retained in any form in the runtime program, and is only used to emit code.

Alejandro
  • 7,290
  • 4
  • 34
  • 59
  • Thank you for the answer. In this case, exist any way to get c# version on CLI? Something like `dotnet --language_version`? – stepozer Mar 17 '20 at 19:58
  • @stepozer That only lives on the project file, that's used to launch the compiler with the right language version. Once compiled, CLI or code or anything, C# doesn't exists anymore. – Alejandro Mar 17 '20 at 21:44
  • So, that i asked, where to get it? – stepozer Mar 18 '20 at 03:52
  • @stepozer You can look at the `LangVersion` tag of the csproj file, that's the only place where the language version is held. Of course, that file lives only on the development working copy and isn't deployed at all, so at runtime it won't be available, unless you take actions to ship it. – Alejandro Mar 18 '20 at 14:47