Skip to content

added conditional processing for C# language features based on languageVersion / framework #3454

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,36 @@
"description": "If specified, skips the automatic restore of the project on create.",
"defaultValue": "false",
"displayName": "Skip restore"
},
"csharp10orLater": {
"type": "generated",
"generator": "regexMatch",
"datatype": "bool",
"parameters": {
"pattern": "^(|10\\.0|10|preview|latest|default|latestMajor)$",
"source": "langVersion"
}
},
"csharp8orLater": {
"type": "generated",
"generator": "regexMatch",
"datatype": "bool",
"parameters": {
"pattern": "^(|8|8\\.0|9|9\\.0|10\\.0|10|preview|latest|default|latestMajor)$",
"source": "langVersion"
}
},
"csharpFeature_ImplicitUsings": {
"type": "computed",
"value": "Framework == \"net6.0\" && csharp10orLater == \"true\""
},
"csharpFeature_FileScopedNamespaces": {
"type": "computed",
"value": "(Framework == \"net6.0\" || langVersion != \"\") && csharp10orLater == \"true\""
},
"csharpFeature_Nullable": {
"type": "computed",
"value": "(Framework != \"netstandard2.0\" || langVersion != \"\") && csharp8orLater == \"true\""
}
},
"primaryOutputs": [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
namespace Company.ClassLibrary1
#if (!csharpFeature_ImplicitUsings)
using System;

#endif
namespace Company.ClassLibrary1
{
public class Class1
{

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
<TargetFramework Condition="'$(TargetFrameworkOverride)' != ''">TargetFrameworkOverride</TargetFramework>
<RootNamespace Condition="'$(name)' != '$(name{-VALUE-FORMS-}safe_namespace)'">Company.ClassLibrary1</RootNamespace>
<LangVersion Condition="'$(langVersion)' != ''">$(ProjectLanguageVersion)</LangVersion>
<Nullable Condition="('$(Framework)' != 'netstandard2.0')">enable</Nullable>
<DisableImplicitNamespaceImports Condition="'$(csharpFeature_ImplicitUsings)' != 'true'">true</DisableImplicitNamespaceImports>
<Nullable Condition="'$(csharpFeature_Nullable)' == 'true'">enable</Nullable>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,45 @@
"description": "If specified, skips the automatic restore of the project on create.",
"defaultValue": "false",
"displayName": "Skip restore"
},
"csharp10orLater": {
"type": "generated",
"generator": "regexMatch",
"datatype": "bool",
"parameters": {
"pattern": "^(|10\\.0|10|preview|latest|default|latestMajor)$",
"source": "langVersion"
}
},
"csharp9orLater": {
"type": "generated",
"generator": "regexMatch",
"datatype": "bool",
"parameters": {
"pattern": "^(|9|9\\.0|10\\.0|10|preview|latest|default|latestMajor)$",
"source": "langVersion"
}
},
"csharp8orLater": {
"type": "generated",
"generator": "regexMatch",
"datatype": "bool",
"parameters": {
"pattern": "^(|8|8\\.0|9|9\\.0|10\\.0|10|preview|latest|default|latestMajor)$",
"source": "langVersion"
}
},
"csharpFeature_ImplicitUsings": {
"type": "computed",
"value": "csharp10orLater == \"true\""
},
"csharpFeature_Nullable": {
"type": "computed",
"value": "csharp8orLater == \"true\""
},
"csharpFeature_TopLevelProgram": {
"type": "computed",
"value": "csharp9orLater == \"true\""
}
},
"primaryOutputs": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
<OutputType>Exe</OutputType>
<TargetFramework Condition="'$(TargetFrameworkOverride)' == ''">net6.0</TargetFramework>
<TargetFramework Condition="'$(TargetFrameworkOverride)' != ''">TargetFrameworkOverride</TargetFramework>
<RootNamespace Condition="'$(name)' != '$(name{-VALUE-FORMS-}safe_namespace)'">Company.ConsoleApplication1</RootNamespace>
<LangVersion Condition="'$(langVersion)' != ''">$(ProjectLanguageVersion)</LangVersion>
<Nullable>enable</Nullable>
<DisableImplicitNamespaceImports Condition="'$(csharpFeature_ImplicitUsings)' != 'true'">true</DisableImplicitNamespaceImports>
<Nullable Condition="'$(csharpFeature_Nullable)' == 'true'">enable</Nullable>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,2 +1,21 @@
Console.WriteLine("Hello, World!");
#if (csharpFeature_TopLevelProgram)
// See https://aka.ms/new-console-template for more information
#endif
#if (!csharpFeature_ImplicitUsings)
using System;

#endif
#if (csharpFeature_TopLevelProgram)
Console.WriteLine("Hello, World!");
#else
namespace Company.ConsoleApplication1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you planning on duplicating this content for the file-scoped namespaces feature?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't tested it yet, but I guess no. Top-level programs are supported since 9.0 and file scoped namespaces are supported since 10.0, so there should be no need as if top level program is not supported, file scoped namespaces won't be supported too.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah of course, this is Program.cs 😄 👍

{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
}
}
}
#endif
Loading