diff --git a/.github/workflows/dotnet-build.yml b/.github/workflows/dotnet-build.yml index 63252ef..e02fd87 100644 --- a/.github/workflows/dotnet-build.yml +++ b/.github/workflows/dotnet-build.yml @@ -14,13 +14,11 @@ jobs: uses: actions/setup-dotnet@v1 with: dotnet-version: | - 3.1.x - 5.0.x - 6.0.x - 7.0.x + 8.0.x - name: Build with dotnet - run: dotnet build --configuration Release + run: dotnet build --configuration release - - name: Test - run: dotnet test --configuration Release \ No newline at end of file +# No need because all tests passed if the solution is successfully built +# - name: Test +# run: dotnet test --configuration release diff --git a/.github/workflows/nuget-master-publish.yml b/.github/workflows/nuget-master-publish.yml deleted file mode 100644 index 9b5e85c..0000000 --- a/.github/workflows/nuget-master-publish.yml +++ /dev/null @@ -1,40 +0,0 @@ -# 去掉注释可以合并 master 分支自动打包 -# 为什么不期望推送 master 自动打包?原因是打出来的 CBB 没有 Tag 不利于回滚找到代码 - -# name: publish nuget - -# on: -# push: -# branches: -# - master - -# jobs: -# build: - -# runs-on: windows-latest - -# steps: -# - uses: actions/checkout@v1 - -# - name: Setup .NET Core -# uses: actions/setup-dotnet@v1 -# with: -# dotnet-version: 3.1.300 - -# - name: Build with dotnet -# run: | -# dotnet build --configuration Release -# dotnet pack --configuration Release --no-build - -# - name: Install Nuget -# uses: nuget/setup-nuget@v1 -# with: -# nuget-version: '5.x' - -# - name: Add private GitHub registry to NuGet -# run: | -# nuget sources add -name github -Source https://nuget.pkg.github.com/dotnet-campus/index.json -Username dotnet-campus -Password ${{ secrets.GITHUB_TOKEN }} -# - name: Push generated package to GitHub registry -# run: | -# nuget push .\bin\Release\*.nupkg -Source github -SkipDuplicate -# nuget push .\bin\Release\*.nupkg -Source https://api.nuget.org/v3/index.json -SkipDuplicate -ApiKey ${{ secrets.NugetKey }} diff --git a/.github/workflows/nuget-tag-publish.yml b/.github/workflows/nuget-tag-publish.yml index 566e99b..2c7fbe9 100644 --- a/.github/workflows/nuget-tag-publish.yml +++ b/.github/workflows/nuget-tag-publish.yml @@ -1,6 +1,6 @@ name: publish nuget -on: +on: push: tags: - '*' @@ -17,10 +17,7 @@ jobs: uses: actions/setup-dotnet@v1 with: dotnet-version: | - 3.1.x - 5.0.x - 6.0.x - 7.0.x + 8.0.x - name: Install dotnet tool run: dotnet tool install -g dotnetCampus.TagToVersion @@ -30,8 +27,8 @@ jobs: - name: Build with dotnet run: | - dotnet build --configuration Release - dotnet pack --configuration Release --no-build + dotnet build --configuration release + dotnet pack --configuration release --no-build - name: Install Nuget uses: nuget/setup-nuget@v1 @@ -44,5 +41,5 @@ jobs: - name: Push generated package to GitHub registry run: | - nuget push .\bin\Release\*.nupkg -Source github -SkipDuplicate - nuget push .\bin\Release\*.nupkg -Source https://api.nuget.org/v3/index.json -SkipDuplicate -ApiKey ${{ secrets.NugetKey }} + nuget push .\artifacts\package\release\*.nupkg -Source github -SkipDuplicate + nuget push .\artifacts\package\release\*.nupkg -Source https://api.nuget.org/v3/index.json -SkipDuplicate -ApiKey ${{ secrets.NugetKey }} diff --git a/.gitignore b/.gitignore index 9491a2f..1cd7ff5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,7 @@ -## Ignore Visual Studio temporary files, build results, and +## Ignore Visual Studio temporary files, build results, and ## files generated by popular Visual Studio add-ons. ## -## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore +## Get latest from https://github.com/github/gitignore/blob/main/VisualStudio.gitignore # User-specific files *.rsuser @@ -29,7 +29,6 @@ x86/ bld/ [Bb]in/ [Oo]bj/ -[Oo]ut/ [Ll]og/ [Ll]ogs/ @@ -91,6 +90,7 @@ StyleCopReport.xml *.tmp_proj *_wpftmp.csproj *.log +*.tlog *.vspscc *.vssscc .builds @@ -294,6 +294,17 @@ node_modules/ # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) *.vbw +# Visual Studio 6 auto-generated project file (contains which files were open etc.) +*.vbp + +# Visual Studio 6 workspace and project file (working project files containing files to include in project) +*.dsw +*.dsp + +# Visual Studio 6 technical files +*.ncb +*.aps + # Visual Studio LightSwitch build output **/*.HTMLClient/GeneratedArtifacts **/*.DesktopClient/GeneratedArtifacts @@ -350,6 +361,9 @@ ASALocalRun/ # Local History for Visual Studio .localhistory/ +# Visual Studio History (VSHistory) files +.vshistory/ + # BeatPulse healthcheck temp database healthchecksdb @@ -360,4 +374,26 @@ MigrationBackup/ .ionide/ # Fody - auto-generated XML schema -FodyWeavers.xsd \ No newline at end of file +FodyWeavers.xsd + +# VS Code files for those working on multiple tools +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json +*.code-workspace + +# Local History for Visual Studio Code +.history/ + +# Windows Installer files from build outputs +*.cab +*.msi +*.msix +*.msm +*.msp + +# JetBrains Rider +.idea/ +*.sln.iml diff --git a/Directory.Build.props b/Directory.Build.props index 271e9dd..b56a64e 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -2,30 +2,34 @@ + - latest + enable + true True + $(MSBuildThisFileDirectory)artifacts + $(MSBuildThisFileDirectory) + + - $(NoWarn);NETSDK1138;NU5100 + $(NoWarn);NETSDK1138;NU5100;NU5128 + - $(MSBuildThisFileDirectory)bin\$(Configuration) - dotnet campus(.NET 职业技术学院) + + + 提供统一的日志记录方法。使用源生成器允许库的作者在不依赖本日志库的情况下完成日志的记录,并且还能对接到产品中完成日志的统一输出。 dotnet-campus + dotnet campus(.NET 职业技术学院) + Copyright © 2023-2024 dotnet campus, All Rights Reserved. + git https://github.com/dotnet-campus/dotnetCampus.LatestCSharpFeatures https://github.com/dotnet-campus/dotnetCampus.LatestCSharpFeatures - git - Copyright © 2023 dotnet campus, All Rights Reserved. - - - - - - - \ No newline at end of file + diff --git a/README.md b/README.md index b41f577..bc96bb2 100644 --- a/README.md +++ b/README.md @@ -1,47 +1,37 @@ # Latest CSharp Features -| Language | -| --- | +| Language | +| ---------------------------------- | | [简体中文](/docs/README.zh-CHS.md) | | [繁體中文](/docs/README.zh-CHT.md) | -| [English](/README.md) | -| [日本語](/docs/README.jp.md) | +| [English](/README.md) | -This open-source project provides a series of NuGet packages that allow you to use the latest C# language features in older versions of .NET (including older .NET Framework, .NET Standard, as well as older .NET Core App, .NET). +[![NuGet](https://img.shields.io/nuget/v/dotnetCampus.LatestCSharpFeatures.svg)](https://www.nuget.org/packages/dotnetCampus.LatestCSharpFeatures) -## NuGet Packages - -We currently offer 8 NuGet packages for you to choose from based on your needs. - -|Package Name|Function|Link| -|---|---|---| -|dotnetCampus.LatestCSharpFeatures|All-in-one package, includes all new C# features|[![NuGet](https://img.shields.io/nuget/v/dotnetCampus.LatestCSharpFeatures.svg)](https://www.nuget.org/packages/dotnetCampus.LatestCSharpFeatures)| -|dotnetCampus.LatestCSharpFeatures.Source|Source code version of the all-in-one package|[![NuGet](https://img.shields.io/nuget/v/dotnetCampus.LatestCSharpFeatures.Source.svg)](https://www.nuget.org/packages/dotnetCampus.LatestCSharpFeatures.Source)| -|dotnetCampus.IsExternalInit|Supports the use of init syntax|[![NuGet](https://img.shields.io/nuget/v/dotnetCampus.IsExternalInit.svg)](https://www.nuget.org/packages/dotnetCampus.IsExternalInit)| -|dotnetCampus.IsExternalInit.Source|Source code version that supports the use of init syntax|[![NuGet](https://img.shields.io/nuget/v/dotnetCampus.IsExternalInit.Source.svg)](https://www.nuget.org/packages/dotnetCampus.IsExternalInit.Source)| -|dotnetCampus.Nullable|Supports the use of rich nullable features|[![NuGet](https://img.shields.io/nuget/v/dotnetCampus.Nullable.svg)](https://www.nuget.org/packages/dotnetCampus.Nullable)| -|dotnetCampus.Nullable.Source|Source code version that supports the use of rich nullable features|[![NuGet](https://img.shields.io/nuget/v/dotnetCampus.Nullable.Source.svg)](https://www.nuget.org/packages/dotnetCampus.Nullable.Source)| -|dotnetCampus.Required|Adds support for the required syntax*|[![NuGet](https://img.shields.io/nuget/v/dotnetCampus.Required.svg)](https://www.nuget.org/packages/dotnetCampus.Required)| -|dotnetCampus.Required.Source|Source code version that adds support for the required syntax*|[![NuGet](https://img.shields.io/nuget/v/dotnetCampus.Required.Source.svg)](https://www.nuget.org/packages/dotnetCampus.Required.Source)| - -\* For an introduction to the required syntax, you can refer to this [description](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/required)【7†source】【9†source】. +This open-source project provides a NuGet package, dotnetCampus.LatestCSharpFeatures, which allows you to use the latest C# language features in older versions of .NET, including the old .NET Framework, .NET Standard, and older versions of .NET Core App and .NET. ## How to Use -For both the all-in-one package and the single-feature packages, we provide two versions. Packages without the .Source suffix will generate a dll, so all projects that reference this project can use these new features. Packages with the .Source suffix are only effective for the project where the package is installed, and there are no additional dlls in the final generated project. +Simply install the dotnetCampus.LatestCSharpFeatures NuGet package. + +```xml + + +``` -If you want the .Source package to be effective for other projects that reference this project, you can add a conditional compilation symbol in the csproj file: +If you want these new language features to also be effective for other projects that reference this project, you can add a conditional compilation symbol in the csproj file: ```xml - + $(DefineConstants);USE_PUBLIC_LATEST_CSHARP_FEATURES ``` ## Feedback and Contributions -We welcome feedback and contributions from all users. If you encounter any problems during use, or have any suggestions for improvement, you can submit them via GitHub Issues. +We welcome feedback and contributions from all users. If you encounter any problems during use, or have any suggestions for improvements, you can submit them via GitHub Issues. -If you wish to participate in the development of the project, you are very welcome! You can Fork this repository and then submit a Pull Request. +If you wish to participate in the development of the project, you are also very welcome! You can Fork this repository and then submit a Pull Request. Thank you for your support and help with dotnetCampus.LatestCSharpFeatures! diff --git a/build/Version.props b/build/Version.props index 2b6ae1d..64a7364 100644 --- a/build/Version.props +++ b/build/Version.props @@ -1,5 +1,6 @@ - 11.0.0 + + 12.0.0 - \ No newline at end of file + diff --git a/docs/README.jp.md b/docs/README.jp.md deleted file mode 100644 index 007625b..0000000 --- a/docs/README.jp.md +++ /dev/null @@ -1,47 +0,0 @@ -# 最新のCSharpの特徴 - -| Language | -| --- | -| [简体中文](/docs/README.zh-CHS.md) | -| [繁體中文](/docs/README.zh-CHT.md) | -| [English](/README.md) | -| [日本語](/docs/README.jp.md) | - -このオープンソースプロジェクトは一連のNuGetパッケージを提供し、古いバージョンの.NET(古い.NET Framework、.NET Standard、古い.NET Core App、.NETを含む)で最新のC#言語の特徴を使用できます。 - -## NuGetパッケージ - -現在、8つのNuGetパッケージを提供しており、必要に応じて選択してインストールできます。 - -|パッケージ名|機能|リンク| -|---|---|---| -|dotnetCampus.LatestCSharpFeatures|全機能パッケージ、すべての新しいC#機能が含まれています|[![NuGet](https://img.shields.io/nuget/v/dotnetCampus.LatestCSharpFeatures.svg)](https://www.nuget.org/packages/dotnetCampus.LatestCSharpFeatures)| -|dotnetCampus.LatestCSharpFeatures.Source|全機能パッケージのソースコードバージョン|[![NuGet](https://img.shields.io/nuget/v/dotnetCampus.LatestCSharpFeatures.Source.svg)](https://www.nuget.org/packages/dotnetCampus.LatestCSharpFeatures.Source)| -|dotnetCampus.IsExternalInit|init構文の使用をサポート|[![NuGet](https://img.shields.io/nuget/v/dotnetCampus.IsExternalInit.svg)](https://www.nuget.org/packages/dotnetCampus.IsExternalInit)| -|dotnetCampus.IsExternalInit.Source|init構文の使用をサポートするソースコードバージョン|[![NuGet](https://img.shields.io/nuget/v/dotnetCampus.IsExternalInit.Source.svg)](https://www.nuget.org/packages/dotnetCampus.IsExternalInit.Source)| -|dotnetCampus.Nullable|豊富なnull許容型の使用をサポート|[![NuGet](https://img.shields.io/nuget/v/dotnetCampus.Nullable.svg)](https://www.nuget.org/packages/dotnetCampus.Nullable)| -|dotnetCampus.Nullable.Source|豊富なnull許容型の使用をサポートするソースコードバージョン|[![NuGet](https://img.shields.io/nuget/v/dotnetCampus.Nullable.Source.svg)](https://www.nuget.org/packages/dotnetCampus.Nullable.Source)| -|dotnetCampus.Required|required構文のサポートを追加*|[![NuGet](https://img.shields.io/nuget/v/dotnetCampus.Required.svg)](https://www.nuget.org/packages/dotnetCampus.Required)| -|dotnetCampus.Required.Source|required構文のサポートを追加するソースコードバージョン*|[![NuGet](https://img.shields.io/nuget/v/dotnetCampus.Required.Source.svg)](https://www.nuget.org/packages/dotnetCampus.Required.Source)| - -\* required構文の紹介については、ここでの[説明](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/required)を参照してください【7†source】【9†source】。 - -## 使用方法 - -全機能パッケージでも単一機能パッケージでも、2つのバージョンを提供しています。.Sourceサフィックスがないパッケージはdllを生成し、このプロジェクトを参照したすべてのプロジェクトでこれらの新機能を使用できます。一方、.Sourceサフィックスが付いたパッケージは、このパッケージをインストールしたプロジェクトにのみ有効で、最終的に生成されたプロジェクトには追加のdllがありません。 - -.Sourceパッケージをこのプロジェクトを参照した他のプロジェクトにも有効にしたい場合は、csprojファイルに条件付きコンパイルシンボルを追加できます: - -```xml - -$(DefineConstants);USE_PUBLIC_LATEST_CSHARP_FEATURES -``` - -## フィードバックと貢献 - -すべてのユーザーからのフィードバックと貢献を歓迎します。何か問題が見つかった場合や改善の提案がある場合は、GitHub Issuesで提出してください。 - -プロジェクトの開発に参加したい場合も、大歓迎です!このリポジトリをフォークして、Pull Requestを提出できます。 - -dotnetCampus.LatestCSharpFeaturesへのご支援とご協力に感謝します! diff --git a/docs/README.zh-CHS.md b/docs/README.zh-CHS.md index 3ad06ed..bda8d7c 100644 --- a/docs/README.zh-CHS.md +++ b/docs/README.zh-CHS.md @@ -1,39 +1,29 @@ -# Latest CSharp Features +# Latest CSharp Features - 最新 C# 语言特性 -| Language | -| --- | +| Language | +| ---------------------------------- | | [简体中文](/docs/README.zh-CHS.md) | | [繁體中文](/docs/README.zh-CHT.md) | -| [English](/README.md) | -| [日本語](/docs/README.jp.md) | +| [English](/README.md) | -此开源项目提供一系列 NuGet 包,让您可以在旧版本的 .NET(包括旧的 .NET Framework、.NET Standard,以及旧的 .NET Core App、.NET)中,使用最新的 C# 语言特性。 +[![NuGet](https://img.shields.io/nuget/v/dotnetCampus.LatestCSharpFeatures.svg)](https://www.nuget.org/packages/dotnetCampus.LatestCSharpFeatures) -## NuGet 包 - -我们目前提供 8 个 NuGet 包,您可以根据需要选择安装。 - -|包名|功能|链接| -|---|---|---| -|dotnetCampus.LatestCSharpFeatures|全功能包,包含所有 C# 新功能|[![NuGet](https://img.shields.io/nuget/v/dotnetCampus.LatestCSharpFeatures.svg)](https://www.nuget.org/packages/dotnetCampus.LatestCSharpFeatures)| -|dotnetCampus.LatestCSharpFeatures.Source|全功能包的源代码版本|[![NuGet](https://img.shields.io/nuget/v/dotnetCampus.LatestCSharpFeatures.Source.svg)](https://www.nuget.org/packages/dotnetCampus.LatestCSharpFeatures.Source)| -|dotnetCampus.IsExternalInit|支持使用 init 语法|[![NuGet](https://img.shields.io/nuget/v/dotnetCampus.IsExternalInit.svg)](https://www.nuget.org/packages/dotnetCampus.IsExternalInit)| -|dotnetCampus.IsExternalInit.Source|支持使用 init 语法的源代码版本|[![NuGet](https://img.shields.io/nuget/v/dotnetCampus.IsExternalInit.Source.svg)](https://www.nuget.org/packages/dotnetCampus.IsExternalInit.Source)| -|dotnetCampus.Nullable|支持使用丰富的可空支持|[![NuGet](https://img.shields.io/nuget/v/dotnetCampus.Nullable.svg)](https://www.nuget.org/packages/dotnetCampus.Nullable)| -|dotnetCampus.Nullable.Source|支持使用丰富的可空支持的源代码版本|[![NuGet](https://img.shields.io/nuget/v/dotnetCampus.Nullable.Source.svg)](https://www.nuget.org/packages/dotnetCampus.Nullable.Source)| -|dotnetCampus.Required|增加了 required 语法的支持*|[![NuGet](https://img.shields.io/nuget/v/dotnetCampus.Required.svg)](https://www.nuget.org/packages/dotnetCampus.Required)| -|dotnetCampus.Required.Source|增加了 required 语法支持的源代码版本*|[![NuGet](https://img.shields.io/nuget/v/dotnetCampus.Required.Source.svg)](https://www.nuget.org/packages/dotnetCampus.Required.Source)| - -\* 对于 required 语法的介绍,您可以参考这里的[说明](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/required)【7†source】【9†source】。 +此开源项目提供一个 NuGet 包 dotnetCampus.LatestCSharpFeatures,让您可以在旧版本的 .NET(包括旧的 .NET Framework、.NET Standard,以及旧的 .NET Core App、.NET)中,使用最新的 C# 语言特性。 ## 使用方法 -无论是全功能包还是单一功能包,我们都提供了两种版本。不带 .Source 后缀的包会生成一个 dll,这样所有引用了这个项目的项目都能使用到这些新功能。而带有 .Source 后缀的包则只对安装此包的项目有效,最终生成的项目中没有额外的 dll。 +直接安装 dotnetCampus.LatestCSharpFeatures NuGet 包即可。 + +```xml + + +``` -如果您希望 .Source 包对其他引用了此项目的项目也生效,可以在 csproj 文件中增加一个条件编译符: +如果你希望这些新语言特性对其他引用了此项目的项目也生效,可以在 csproj 文件中增加一个条件编译符: ```xml - $(DefineConstants);USE_PUBLIC_LATEST_CSHARP_FEATURES ``` diff --git a/docs/README.zh-CHT.md b/docs/README.zh-CHT.md index fc57134..1987455 100644 --- a/docs/README.zh-CHT.md +++ b/docs/README.zh-CHT.md @@ -1,47 +1,37 @@ -# 最新的 CSharp 特性 +# Latest CSharp Features 最新的 C# 特性 -| Language | -| --- | +| Language | +| ---------------------------------- | | [简体中文](/docs/README.zh-CHS.md) | | [繁體中文](/docs/README.zh-CHT.md) | -| [English](/README.md) | -| [日本語](/docs/README.jp.md) | +| [English](/README.md) | -此開源項目提供一系列 NuGet 包,讓您可以在舊版本的 .NET(包括舊的 .NET Framework、.NET Standard,以及舊的 .NET Core App、.NET)中,使用最新的 C# 語言特性。 +[![NuGet](https://img.shields.io/nuget/v/dotnetCampus.LatestCSharpFeatures.svg)](https://www.nuget.org/packages/dotnetCampus.LatestCSharpFeatures) -## NuGet 包 - -我們目前提供 8 個 NuGet 包,您可以根據需要選擇安裝。 - -|包名|功能|連結| -|---|---|---| -|dotnetCampus.LatestCSharpFeatures|全功能包,包含所有 C# 新功能|[![NuGet](https://img.shields.io/nuget/v/dotnetCampus.LatestCSharpFeatures.svg)](https://www.nuget.org/packages/dotnetCampus.LatestCSharpFeatures)| -|dotnetCampus.LatestCSharpFeatures.Source|全功能包的源碼版本|[![NuGet](https://img.shields.io/nuget/v/dotnetCampus.LatestCSharpFeatures.Source.svg)](https://www.nuget.org/packages/dotnetCampus.LatestCSharpFeatures.Source)| -|dotnetCampus.IsExternalInit|支援使用 init 語法|[![NuGet](https://img.shields.io/nuget/v/dotnetCampus.IsExternalInit.svg)](https://www.nuget.org/packages/dotnetCampus.IsExternalInit)| -|dotnetCampus.IsExternalInit.Source|支援使用 init 語法的源碼版本|[![NuGet](https://img.shields.io/nuget/v/dotnetCampus.IsExternalInit.Source.svg)](https://www.nuget.org/packages/dotnetCampus.IsExternalInit.Source)| -|dotnetCampus.Nullable|支援使用豐富的可空支援|[![NuGet](https://img.shields.io/nuget/v/dotnetCampus.Nullable.svg)](https://www.nuget.org/packages/dotnetCampus.Nullable)| -|dotnetCampus.Nullable.Source|支援使用豐富的可空支援的源碼版本|[![NuGet](https://img.shields.io/nuget/v/dotnetCampus.Nullable.Source.svg)](https://www.nuget.org/packages/dotnetCampus.Nullable.Source)| -|dotnetCampus.Required|增加了 required 語法的支援*|[![NuGet](https://img.shields.io/nuget/v/dotnetCampus.Required.svg)](https://www.nuget.org/packages/dotnetCampus.Required)| -|dotnetCampus.Required.Source|增加了 required 語法支援的源碼版本*|[![NuGet](https://img.shields.io/nuget/v/dotnetCampus.Required.Source.svg)](https://www.nuget.org/packages/dotnetCampus.Required.Source)| - -\* 對於 required 語法的介紹,您可以參考這裡的[說明](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/required)【7†source】【9†source】。 +此開源項目提供一個 NuGet 包 dotnetCampus.LatestCSharpFeatures,讓您可以在舊版本的 .NET(包括舊的 .NET Framework、.NET Standard,以及舊的 .NET Core App、.NET)中,使用最新的 C# 語言特性。 ## 使用方法 -無論是全功能包還是單一功能包,我們都提供了兩種版本。不帶 .Source 後綴的包會生成一個 dll,這樣所有引用了這個項目的項目都能使用到這些新功能。而帶有 .Source 後綴的包則只對安裝此包的項目有效,最終生成的項目中沒有額外的 dll。 +直接安裝 dotnetCampus.LatestCSharpFeatures NuGet 包即可。 + +```xml + + +``` -如果您希望 .Source 包對其他引用了此項目的項目也生效,可以在 csproj 文件中增加一個條件編譯符: +如果您希望這些新語言特性對其他引用了此項目的項目也生效,可以在 csproj 文件中增加一個條件編譯符: ```xml - $(DefineConstants);USE_PUBLIC_LATEST_CSHARP_FEATURES ``` ## 反饋與貢獻 -我們歡迎所有用戶的反饋和貢獻。如果你在使用過程中發現任何問題,或者有任何改進建議,都可以通過 GitHub Issues 提交。 +我們歡迎所有用戶的反饋和貢獻。如果您在使用過程中發現任何問題,或者有任何改進建議,都可以通過 GitHub Issues 提交。 -如果你希望參與到項目的開發中,也非常歡迎!你可以 Fork 本倉庫,然後提交 Pull Request。 +如果您希望參與到項目的開發中,也非常歡迎!您可以 Fork 本倉庫,然後提交 Pull Request。 -感謝你對 dotnetCampus.LatestCSharpFeatures 的支援和幫助! +感謝您對 dotnetCampus.LatestCSharpFeatures 的支持和幫助! diff --git a/dotnetCampus.LatestCSharpFeatures.sln b/dotnetCampus.LatestCSharpFeatures.sln index c1d0bb8..d2810c4 100644 --- a/dotnetCampus.LatestCSharpFeatures.sln +++ b/dotnetCampus.LatestCSharpFeatures.sln @@ -3,8 +3,6 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.6.33712.159 MinimumVisualStudioVersion = 15.0.26124.0 -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "dotnetCampus.Nullable", "src\dotnetCampus.Nullable\dotnetCampus.Nullable.csproj", "{2ECF1CEB-070E-4933-93C4-F7FD42D8693C}" -EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "build", "build", "{8A6F61D3-076F-4B75-80A1-FF377AC69D73}" ProjectSection(SolutionItems) = preProject .gitattributes = .gitattributes @@ -15,27 +13,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "build", "build", "{8A6F61D3 build\Version.props = build\Version.props EndProjectSection EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "dotnetCampus.IsExternalInit", "src\dotnetCampus.IsExternalInit\dotnetCampus.IsExternalInit.csproj", "{0DDC38EF-851E-49E7-B72A-DF46E44E3586}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "dotnetCampus.IsExternalInit.Source", "src\dotnetCampus.IsExternalInit.Source\dotnetCampus.IsExternalInit.Source.csproj", "{94E8B753-81BF-44DC-A8D6-AC1D1FE54244}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "dotnetCampus.Required", "src\dotnetCampus.Required\dotnetCampus.Required.csproj", "{6ED76C7A-A16A-45A5-BC1F-91A880839C31}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "dotnetCampus.Required.Source", "src\dotnetCampus.Required.Source\dotnetCampus.Required.Source.csproj", "{4EBF62CB-613E-4496-B890-B1F433ED7581}" -EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "dotnetCampus.LatestCSharpFeatures", "src\dotnetCampus.LatestCSharpFeatures\dotnetCampus.LatestCSharpFeatures.csproj", "{D0B0F7EB-3C81-45C4-BB47-5DCB16F2EA55}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "dotnetCampus.Nullable.Source", "src\dotnetCampus.Nullable.Source\dotnetCampus.Nullable.Source.csproj", "{72163C73-CBC5-4CB5-BDDF-769B7AFD6FAB}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "dotnetCampus.LatestCSharpFeatures.Source", "src\dotnetCampus.LatestCSharpFeatures.Source\dotnetCampus.LatestCSharpFeatures.Source.csproj", "{16D75C29-3176-4CFD-A1B5-B5F24876B038}" -EndProject -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "IsExternalInit", "IsExternalInit", "{4E06689B-E73A-4A04-AAA0-7959A6EC06E6}" -EndProject -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Nullable", "Nullable", "{CCAAE7A8-1879-4F67-98AA-D837C80227A2}" -EndProject -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Required", "Required", "{F0CBB519-BB38-4104-B840-4076BB206E36}" -EndProject -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{CAB44C21-D58C-4732-B9D8-60B1DAE82BEF}" + ProjectSection(ProjectDependencies) = postProject + {642FA0CA-E827-4D96-9358-D596CD87DAF4} = {642FA0CA-E827-4D96-9358-D596CD87DAF4} + EndProjectSection EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{878C1D77-0A77-44BD-847E-164188B10677}" EndProject @@ -49,38 +30,10 @@ Global Release|Any CPU = Release|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {2ECF1CEB-070E-4933-93C4-F7FD42D8693C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {2ECF1CEB-070E-4933-93C4-F7FD42D8693C}.Debug|Any CPU.Build.0 = Debug|Any CPU - {2ECF1CEB-070E-4933-93C4-F7FD42D8693C}.Release|Any CPU.ActiveCfg = Release|Any CPU - {2ECF1CEB-070E-4933-93C4-F7FD42D8693C}.Release|Any CPU.Build.0 = Release|Any CPU - {0DDC38EF-851E-49E7-B72A-DF46E44E3586}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {0DDC38EF-851E-49E7-B72A-DF46E44E3586}.Debug|Any CPU.Build.0 = Debug|Any CPU - {0DDC38EF-851E-49E7-B72A-DF46E44E3586}.Release|Any CPU.ActiveCfg = Release|Any CPU - {0DDC38EF-851E-49E7-B72A-DF46E44E3586}.Release|Any CPU.Build.0 = Release|Any CPU - {94E8B753-81BF-44DC-A8D6-AC1D1FE54244}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {94E8B753-81BF-44DC-A8D6-AC1D1FE54244}.Debug|Any CPU.Build.0 = Debug|Any CPU - {94E8B753-81BF-44DC-A8D6-AC1D1FE54244}.Release|Any CPU.ActiveCfg = Release|Any CPU - {94E8B753-81BF-44DC-A8D6-AC1D1FE54244}.Release|Any CPU.Build.0 = Release|Any CPU - {6ED76C7A-A16A-45A5-BC1F-91A880839C31}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {6ED76C7A-A16A-45A5-BC1F-91A880839C31}.Debug|Any CPU.Build.0 = Debug|Any CPU - {6ED76C7A-A16A-45A5-BC1F-91A880839C31}.Release|Any CPU.ActiveCfg = Release|Any CPU - {6ED76C7A-A16A-45A5-BC1F-91A880839C31}.Release|Any CPU.Build.0 = Release|Any CPU - {4EBF62CB-613E-4496-B890-B1F433ED7581}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {4EBF62CB-613E-4496-B890-B1F433ED7581}.Debug|Any CPU.Build.0 = Debug|Any CPU - {4EBF62CB-613E-4496-B890-B1F433ED7581}.Release|Any CPU.ActiveCfg = Release|Any CPU - {4EBF62CB-613E-4496-B890-B1F433ED7581}.Release|Any CPU.Build.0 = Release|Any CPU {D0B0F7EB-3C81-45C4-BB47-5DCB16F2EA55}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {D0B0F7EB-3C81-45C4-BB47-5DCB16F2EA55}.Debug|Any CPU.Build.0 = Debug|Any CPU {D0B0F7EB-3C81-45C4-BB47-5DCB16F2EA55}.Release|Any CPU.ActiveCfg = Release|Any CPU {D0B0F7EB-3C81-45C4-BB47-5DCB16F2EA55}.Release|Any CPU.Build.0 = Release|Any CPU - {72163C73-CBC5-4CB5-BDDF-769B7AFD6FAB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {72163C73-CBC5-4CB5-BDDF-769B7AFD6FAB}.Debug|Any CPU.Build.0 = Debug|Any CPU - {72163C73-CBC5-4CB5-BDDF-769B7AFD6FAB}.Release|Any CPU.ActiveCfg = Release|Any CPU - {72163C73-CBC5-4CB5-BDDF-769B7AFD6FAB}.Release|Any CPU.Build.0 = Release|Any CPU - {16D75C29-3176-4CFD-A1B5-B5F24876B038}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {16D75C29-3176-4CFD-A1B5-B5F24876B038}.Debug|Any CPU.Build.0 = Debug|Any CPU - {16D75C29-3176-4CFD-A1B5-B5F24876B038}.Release|Any CPU.ActiveCfg = Release|Any CPU - {16D75C29-3176-4CFD-A1B5-B5F24876B038}.Release|Any CPU.Build.0 = Release|Any CPU {D10BF09A-FDF3-43F0-BD54-49A73554CDAB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {D10BF09A-FDF3-43F0-BD54-49A73554CDAB}.Debug|Any CPU.Build.0 = Debug|Any CPU {D10BF09A-FDF3-43F0-BD54-49A73554CDAB}.Release|Any CPU.ActiveCfg = Release|Any CPU @@ -94,15 +47,6 @@ Global HideSolutionNode = FALSE EndGlobalSection GlobalSection(NestedProjects) = preSolution - {2ECF1CEB-070E-4933-93C4-F7FD42D8693C} = {CCAAE7A8-1879-4F67-98AA-D837C80227A2} - {0DDC38EF-851E-49E7-B72A-DF46E44E3586} = {4E06689B-E73A-4A04-AAA0-7959A6EC06E6} - {94E8B753-81BF-44DC-A8D6-AC1D1FE54244} = {4E06689B-E73A-4A04-AAA0-7959A6EC06E6} - {6ED76C7A-A16A-45A5-BC1F-91A880839C31} = {F0CBB519-BB38-4104-B840-4076BB206E36} - {4EBF62CB-613E-4496-B890-B1F433ED7581} = {F0CBB519-BB38-4104-B840-4076BB206E36} - {72163C73-CBC5-4CB5-BDDF-769B7AFD6FAB} = {CCAAE7A8-1879-4F67-98AA-D837C80227A2} - {4E06689B-E73A-4A04-AAA0-7959A6EC06E6} = {CAB44C21-D58C-4732-B9D8-60B1DAE82BEF} - {CCAAE7A8-1879-4F67-98AA-D837C80227A2} = {CAB44C21-D58C-4732-B9D8-60B1DAE82BEF} - {F0CBB519-BB38-4104-B840-4076BB206E36} = {CAB44C21-D58C-4732-B9D8-60B1DAE82BEF} {D10BF09A-FDF3-43F0-BD54-49A73554CDAB} = {878C1D77-0A77-44BD-847E-164188B10677} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution diff --git a/src/dotnetCampus.IsExternalInit.Source/dotnetCampus.IsExternalInit.Source.csproj b/src/dotnetCampus.IsExternalInit.Source/dotnetCampus.IsExternalInit.Source.csproj deleted file mode 100644 index 5d8588f..0000000 --- a/src/dotnetCampus.IsExternalInit.Source/dotnetCampus.IsExternalInit.Source.csproj +++ /dev/null @@ -1,14 +0,0 @@ - - - - net5.0;netcoreapp3.0;netstandard2.0;net40 - System.Diagnostics.CodeAnalysis - dotnetCampus.IsExternalInit - If you use C# 9.0 or later and want to use record types, this '.Source' library adds internal IsExternalInit attribute support to your project. This confines the new language feature to the project itself without affecting other projects that reference it. - - - - - - - \ No newline at end of file diff --git a/src/dotnetCampus.IsExternalInit/_._ b/src/dotnetCampus.IsExternalInit/_._ deleted file mode 100644 index e69de29..0000000 diff --git a/src/dotnetCampus.IsExternalInit/dotnetCampus.IsExternalInit.csproj b/src/dotnetCampus.IsExternalInit/dotnetCampus.IsExternalInit.csproj deleted file mode 100644 index 6d8be72..0000000 --- a/src/dotnetCampus.IsExternalInit/dotnetCampus.IsExternalInit.csproj +++ /dev/null @@ -1,25 +0,0 @@ - - - - net5.0;netcoreapp3.0;netstandard2.0;net40 - true - System.Diagnostics.CodeAnalysis - If you use C# 9.0 or later and want to use record types, this library adds public IsExternalInit attribute support for your project, allowing the new language feature to be propagated to other projects that reference this one. - $(DefineConstants);USE_PUBLIC_LATEST_CSHARP_FEATURES - - - - false - - - - - - - - - - - - - diff --git a/src/dotnetCampus.LatestCSharpFeatures.Analyzer/FeatureGenerator.cs b/src/dotnetCampus.LatestCSharpFeatures.Analyzer/FeatureGenerator.cs index 437216e..40f2fff 100644 --- a/src/dotnetCampus.LatestCSharpFeatures.Analyzer/FeatureGenerator.cs +++ b/src/dotnetCampus.LatestCSharpFeatures.Analyzer/FeatureGenerator.cs @@ -1,4 +1,4 @@ -using dotnetCampus.LatestCSharpFeatures.Analyzer; +using dotnetCampus.LatestCSharpFeatures.Analyzer; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.Diagnostics; @@ -19,8 +19,13 @@ public void Initialize(IncrementalGeneratorInitializationContext context) private void OnExecute(SourceProductionContext context, AnalyzerConfigOptionsProvider provider) { - GenerateFeatureSource(context, provider, "IsExternalInit"); + // .NET Core 3.0 / .NET Standard 2.1 才开始支持 Nullable;.NET 5.0 开始支持更多。 GenerateFeatureSource(context, provider, "Nullable"); + + // .NET 5.0 才开始支持 ExternalInit + GenerateFeatureSource(context, provider, "ExternalInit"); + + // .NET 7.0 才开始支持 SetsRequiredMembersAttribute GenerateFeatureSource(context, provider, "Required"); } diff --git a/src/dotnetCampus.LatestCSharpFeatures.Analyzer/Utils/EmbededSourceFile.cs b/src/dotnetCampus.LatestCSharpFeatures.Analyzer/Utils/EmbeddedSourceFile.cs similarity index 80% rename from src/dotnetCampus.LatestCSharpFeatures.Analyzer/Utils/EmbededSourceFile.cs rename to src/dotnetCampus.LatestCSharpFeatures.Analyzer/Utils/EmbeddedSourceFile.cs index 2ba335b..fa1b58f 100644 --- a/src/dotnetCampus.LatestCSharpFeatures.Analyzer/Utils/EmbededSourceFile.cs +++ b/src/dotnetCampus.LatestCSharpFeatures.Analyzer/Utils/EmbeddedSourceFile.cs @@ -1,10 +1,11 @@ namespace dotnetCampus.LatestCSharpFeatures.Analyzer; + /// /// 嵌入的文本资源的数据。 /// -/// 文件在嵌入的资源中的名称。 +/// 文件在嵌入的资源中的名称。 /// 文件的文本内容。 -internal readonly record struct EmbededSourceFile(string EmbededName, string Content) +internal readonly record struct EmbeddedSourceFile(string EmbeddedName, string Content) { /// /// 根据资源名称猜测文件的无扩展名的名称。 @@ -12,7 +13,7 @@ internal readonly record struct EmbededSourceFile(string EmbededName, string Con /// 无扩展名的文件名。 public ReadOnlySpan GuessFileNameWithoutExtension() { - var span = EmbededName.AsSpan(); + var span = EmbeddedName.AsSpan(); var secondLastDotIndex = 0; var lastDotIndex = 0; for (var i = 0; i < span.Length; i++) diff --git a/src/dotnetCampus.LatestCSharpFeatures.Analyzer/Utils/EmbededSourceFiles.cs b/src/dotnetCampus.LatestCSharpFeatures.Analyzer/Utils/EmbededSourceFiles.cs index 339e99c..898e8c3 100644 --- a/src/dotnetCampus.LatestCSharpFeatures.Analyzer/Utils/EmbededSourceFiles.cs +++ b/src/dotnetCampus.LatestCSharpFeatures.Analyzer/Utils/EmbededSourceFiles.cs @@ -1,6 +1,7 @@ using System.Reflection; namespace dotnetCampus.LatestCSharpFeatures.Analyzer; + /// /// 从嵌入的资源中寻找源代码。 /// @@ -11,7 +12,7 @@ internal static class EmbededSourceFiles /// /// 资源文件夹名称。 /// - internal static IEnumerable Enumerate(string folderName) + internal static IEnumerable Enumerate(string folderName) { // 资源字符串格式为:"{Namespace}.{Folder}.{filename}.{Extension}" var desiredFolder = $"{typeof(EmbededSourceFiles).Namespace}.{folderName}"; diff --git a/src/dotnetCampus.LatestCSharpFeatures.Analyzer/dotnetCampus.LatestCSharpFeatures.Analyzer.csproj b/src/dotnetCampus.LatestCSharpFeatures.Analyzer/dotnetCampus.LatestCSharpFeatures.Analyzer.csproj index 2e31557..21a2843 100644 --- a/src/dotnetCampus.LatestCSharpFeatures.Analyzer/dotnetCampus.LatestCSharpFeatures.Analyzer.csproj +++ b/src/dotnetCampus.LatestCSharpFeatures.Analyzer/dotnetCampus.LatestCSharpFeatures.Analyzer.csproj @@ -10,16 +10,12 @@ - + - - - - - - + + diff --git a/src/dotnetCampus.LatestCSharpFeatures.Source/dotnetCampus.LatestCSharpFeatures.Source.csproj b/src/dotnetCampus.LatestCSharpFeatures.Source/dotnetCampus.LatestCSharpFeatures.Source.csproj deleted file mode 100644 index a44e7c6..0000000 --- a/src/dotnetCampus.LatestCSharpFeatures.Source/dotnetCampus.LatestCSharpFeatures.Source.csproj +++ /dev/null @@ -1,22 +0,0 @@ - - - - net5.0;netcoreapp3.0;netstandard2.0;net40 - true - System.Diagnostics.CodeAnalysis - This comprehensive package provides support for the latest C# language features in your project, enabling older .NET projects to use newer C# language constructs. Currently includes support for 'nullable', 'init', and 'required' keywords. The types introduced are internal, confining these features to the project itself without affecting other projects that reference it. By installing this package, you get all the newest C# features in one place, eliminating the need to install separate packages for each feature. Future updates will include more new features as they are released. - false - - - - - - - - - - - - - - diff --git a/src/dotnetCampus.IsExternalInit.Source/IsExternalInit.cs b/src/dotnetCampus.LatestCSharpFeatures/ExternalInit/IsExternalInit.cs similarity index 84% rename from src/dotnetCampus.IsExternalInit.Source/IsExternalInit.cs rename to src/dotnetCampus.LatestCSharpFeatures/ExternalInit/IsExternalInit.cs index 9f6856a..923396b 100644 --- a/src/dotnetCampus.IsExternalInit.Source/IsExternalInit.cs +++ b/src/dotnetCampus.LatestCSharpFeatures/ExternalInit/IsExternalInit.cs @@ -1,7 +1,5 @@ #if NET5_0_OR_GREATER -// .NET 5.0 开始已包含 IsExternalInit。 #else -// 旧框架需要包含 IsExternalInit。 using System.ComponentModel; namespace System.Runtime.CompilerServices @@ -21,4 +19,4 @@ static class IsExternalInit { } } -#endif \ No newline at end of file +#endif diff --git a/src/dotnetCampus.LatestCSharpFeatures/Nullable/Nullable.cs b/src/dotnetCampus.LatestCSharpFeatures/Nullable/Nullable.cs new file mode 100644 index 0000000..5c86fdf --- /dev/null +++ b/src/dotnetCampus.LatestCSharpFeatures/Nullable/Nullable.cs @@ -0,0 +1,200 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Diagnostics.CodeAnalysis +{ +#if NETSTANDARD2_1 || NETCOREAPP3_0 || NETCOREAPP3_1 || NET5_0_OR_GREATER +#else + /// Specifies that null is allowed as an input even if the corresponding type disallows it. + [AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property, Inherited = false)] +#if USE_PUBLIC_LATEST_CSHARP_FEATURES + public +#else + internal +#endif + sealed class AllowNullAttribute : Attribute { } + + /// Specifies that null is disallowed as an input even if the corresponding type allows it. + [AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property, Inherited = false)] +#if USE_PUBLIC_LATEST_CSHARP_FEATURES + public +#else + internal +#endif + sealed class DisallowNullAttribute : Attribute { } + + /// Specifies that an output may be null even if the corresponding type disallows it. + [AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue, Inherited = false)] +#if USE_PUBLIC_LATEST_CSHARP_FEATURES + public +#else + internal +#endif + sealed class MaybeNullAttribute : Attribute { } + + /// Specifies that an output will not be null even if the corresponding type allows it. Specifies that an input argument was not null when the call returns. + [AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue, Inherited = false)] +#if USE_PUBLIC_LATEST_CSHARP_FEATURES + public +#else + internal +#endif + sealed class NotNullAttribute : Attribute { } + + /// Specifies that when a method returns , the parameter may be null even if the corresponding type disallows it. + [AttributeUsage(AttributeTargets.Parameter, Inherited = false)] +#if USE_PUBLIC_LATEST_CSHARP_FEATURES + public +#else + internal +#endif + sealed class MaybeNullWhenAttribute : Attribute + { + /// Initializes the attribute with the specified return value condition. + /// + /// The return value condition. If the method returns this value, the associated parameter may be null. + /// + public MaybeNullWhenAttribute(bool returnValue) => ReturnValue = returnValue; + + /// Gets the return value condition. + public bool ReturnValue { get; } + } + + /// Specifies that when a method returns , the parameter will not be null even if the corresponding type allows it. + [AttributeUsage(AttributeTargets.Parameter, Inherited = false)] +#if USE_PUBLIC_LATEST_CSHARP_FEATURES + public +#else + internal +#endif + sealed class NotNullWhenAttribute : Attribute + { + /// Initializes the attribute with the specified return value condition. + /// + /// The return value condition. If the method returns this value, the associated parameter will not be null. + /// + public NotNullWhenAttribute(bool returnValue) => ReturnValue = returnValue; + + /// Gets the return value condition. + public bool ReturnValue { get; } + } + + /// Specifies that the output will be non-null if the named parameter is non-null. + [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue, AllowMultiple = true, Inherited = false)] +#if USE_PUBLIC_LATEST_CSHARP_FEATURES + public +#else + internal +#endif + sealed class NotNullIfNotNullAttribute : Attribute + { + /// Initializes the attribute with the associated parameter name. + /// + /// The associated parameter name. The output will be non-null if the argument to the parameter specified is non-null. + /// + public NotNullIfNotNullAttribute(string parameterName) => ParameterName = parameterName; + + /// Gets the associated parameter name. + public string ParameterName { get; } + } + + /// Applied to a method that will never return under any circumstance. + [AttributeUsage(AttributeTargets.Method, Inherited = false)] +#if USE_PUBLIC_LATEST_CSHARP_FEATURES + public +#else + internal +#endif + sealed class DoesNotReturnAttribute : Attribute { } + + /// Specifies that the method will not return if the associated Boolean parameter is passed the specified value. + [AttributeUsage(AttributeTargets.Parameter, Inherited = false)] +#if USE_PUBLIC_LATEST_CSHARP_FEATURES + public +#else + internal +#endif + sealed class DoesNotReturnIfAttribute : Attribute + { + /// Initializes the attribute with the specified parameter value. + /// + /// The condition parameter value. Code after the method will be considered unreachable by diagnostics if the argument to + /// the associated parameter matches this value. + /// + public DoesNotReturnIfAttribute(bool parameterValue) => ParameterValue = parameterValue; + + /// Gets the condition parameter value. + public bool ParameterValue { get; } + } +#endif + +#if NET5_0_OR_GREATER +#else + /// Specifies that the method or property will ensure that the listed field and property members have not-null values. + [AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, Inherited = false, AllowMultiple = true)] +#if USE_PUBLIC_LATEST_CSHARP_FEATURES + public +#else + internal +#endif + sealed class MemberNotNullAttribute : Attribute + { + /// Initializes the attribute with a field or property member. + /// + /// The field or property member that is promised to be not-null. + /// + public MemberNotNullAttribute(string member) => Members = new[] { member }; + + /// Initializes the attribute with the list of field and property members. + /// + /// The list of field and property members that are promised to be not-null. + /// + public MemberNotNullAttribute(params string[] members) => Members = members; + + /// Gets field or property member names. + public string[] Members { get; } + } + + /// Specifies that the method or property will ensure that the listed field and property members have not-null values when returning with the specified return value condition. + [AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, Inherited = false, AllowMultiple = true)] +#if USE_PUBLIC_LATEST_CSHARP_FEATURES + public +#else + internal +#endif + sealed class MemberNotNullWhenAttribute : Attribute + { + /// Initializes the attribute with the specified return value condition and a field or property member. + /// + /// The return value condition. If the method returns this value, the associated parameter will not be null. + /// + /// + /// The field or property member that is promised to be not-null. + /// + public MemberNotNullWhenAttribute(bool returnValue, string member) + { + ReturnValue = returnValue; + Members = new[] { member }; + } + + /// Initializes the attribute with the specified return value condition and list of field and property members. + /// + /// The return value condition. If the method returns this value, the associated parameter will not be null. + /// + /// + /// The list of field and property members that are promised to be not-null. + /// + public MemberNotNullWhenAttribute(bool returnValue, params string[] members) + { + ReturnValue = returnValue; + Members = members; + } + + /// Gets the return value condition. + public bool ReturnValue { get; } + + /// Gets field or property member names. + public string[] Members { get; } + } +#endif +} diff --git a/src/dotnetCampus.Required.Source/RequiredMemberAttribute.cs b/src/dotnetCampus.LatestCSharpFeatures/Required/RequiredMemberAttribute.cs similarity index 95% rename from src/dotnetCampus.Required.Source/RequiredMemberAttribute.cs rename to src/dotnetCampus.LatestCSharpFeatures/Required/RequiredMemberAttribute.cs index 38ae474..610a8d1 100644 --- a/src/dotnetCampus.Required.Source/RequiredMemberAttribute.cs +++ b/src/dotnetCampus.LatestCSharpFeatures/Required/RequiredMemberAttribute.cs @@ -1,7 +1,5 @@ #if NET7_0_OR_GREATER -// .NET 7.0 开始已包含 required。 #else -// 旧框架需要包含 required。 namespace System.Runtime.CompilerServices { /// @@ -54,4 +52,4 @@ public CompilerFeatureRequiredAttribute(string featureName) public const string RequiredMembers = nameof(RequiredMembers); } } -#endif \ No newline at end of file +#endif diff --git a/src/dotnetCampus.Required.Source/SetsRequiredMembersAttribute.cs b/src/dotnetCampus.LatestCSharpFeatures/Required/SetsRequiredMembersAttribute.cs similarity index 100% rename from src/dotnetCampus.Required.Source/SetsRequiredMembersAttribute.cs rename to src/dotnetCampus.LatestCSharpFeatures/Required/SetsRequiredMembersAttribute.cs diff --git a/src/dotnetCampus.LatestCSharpFeatures/_._ b/src/dotnetCampus.LatestCSharpFeatures/_._ deleted file mode 100644 index e69de29..0000000 diff --git a/src/dotnetCampus.LatestCSharpFeatures/dotnetCampus.LatestCSharpFeatures.csproj b/src/dotnetCampus.LatestCSharpFeatures/dotnetCampus.LatestCSharpFeatures.csproj index 015acc3..9e0653e 100644 --- a/src/dotnetCampus.LatestCSharpFeatures/dotnetCampus.LatestCSharpFeatures.csproj +++ b/src/dotnetCampus.LatestCSharpFeatures/dotnetCampus.LatestCSharpFeatures.csproj @@ -1,25 +1,21 @@  - net7.0;net5.0;netcoreapp3.0;netstandard2.0;net40 + net8.0;netstandard2.0;net40 true System.Diagnostics.CodeAnalysis - This comprehensive package provides support for the latest C# language features in your project, enabling older .NET projects to use newer C# language constructs. Currently includes support for 'nullable', 'init', and 'required' keywords. The types introduced are public, allowing these features to propagate to other projects that reference this one. By installing this package, you get all the newest C# features in one place, eliminating the need to install separate packages for each feature. Future updates will include more new features as they are released. - - - + This comprehensive package provides support for the latest C# language features in your project, enabling older .NET projects to use newer C# language constructs. Currently includes support for 'nullable', 'init', and 'required' keywords. The types introduced are internal, confining these features to the project itself without affecting other projects that reference it. By installing this package, you get all the newest C# features in one place, eliminating the need to install separate packages for each feature. Future updates will include more new features as they are released. false - - - + + - + - + diff --git a/src/dotnetCampus.Nullable.Source/Nullable.cs b/src/dotnetCampus.Nullable.Source/Nullable.cs deleted file mode 100644 index a32bb1a..0000000 --- a/src/dotnetCampus.Nullable.Source/Nullable.cs +++ /dev/null @@ -1,248 +0,0 @@ -namespace System.Diagnostics.CodeAnalysis -{ -#if NETCOREAPP3_0 || NETCOREAPP3_1 || NET5_0_OR_GREATER - // 新框架都包含基本的 Nullable Attributes。 -#else - /// - /// 标记一个不可空的输入实际上是可以传入 null 的。 - /// - [AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property, Inherited = false)] -#if USE_PUBLIC_LATEST_CSHARP_FEATURES - public -#else - internal -#endif - sealed class AllowNullAttribute : Attribute { } - - /// - /// 标记一个可空的输入实际上不应该传入 null。 - /// - [AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property, Inherited = false)] -#if USE_PUBLIC_LATEST_CSHARP_FEATURES - public -#else - internal -#endif - sealed class DisallowNullAttribute : Attribute { } - - /// - /// 标记一个非空的返回值实际上可能会返回 null,返回值包括输出参数。 - /// - [AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue, Inherited = false)] -#if USE_PUBLIC_LATEST_CSHARP_FEATURES - public -#else - internal -#endif - sealed class MaybeNullAttribute : Attribute { } - - /// - /// 标记一个可空的返回值实际上是不可能返回 null 的,返回值包括输出参数。 - /// - [AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue, Inherited = false)] -#if USE_PUBLIC_LATEST_CSHARP_FEATURES - public -#else - internal -#endif - sealed class NotNullAttribute : Attribute { } - - /// - /// 当返回指定的 true/false 时某个输出参数才可能为 null,而返回相反的值时那个输出参数则不可为 null。 - /// - [AttributeUsage(AttributeTargets.Parameter, Inherited = false)] -#if USE_PUBLIC_LATEST_CSHARP_FEATURES - public -#else - internal -#endif - sealed class MaybeNullWhenAttribute : Attribute - { - /// - /// 使用 true 或者 false 决定输出参数是否可能为 null。 - /// - /// 如果方法返回值等于这个值,那么输出参数则可能为 null,否则输出参数是不可为 null 的。 - public MaybeNullWhenAttribute(bool returnValue) => ReturnValue = returnValue; - - /// - /// 获取返回值决定是否可为空的那个判断值。 - /// - public bool ReturnValue { get; } - } - - /// - /// 当返回指定的 true/false 时,某个输出参数不可为 null,而返回相反的值时那个输出参数则可能为 null。 - /// - [AttributeUsage(AttributeTargets.Parameter, Inherited = false)] -#if USE_PUBLIC_LATEST_CSHARP_FEATURES - public -#else - internal -#endif - sealed class NotNullWhenAttribute : Attribute - { - /// - /// 使用 true 或者 false 决定输出参数是否不可为 null。 - /// - /// - /// 如果方法或属性的返回值等于这个值,那么输出参数则不可为 null,否则输出参数是可能为 null 的。 - /// - public NotNullWhenAttribute(bool returnValue) => ReturnValue = returnValue; - - /// - /// 获取返回值决定是否不可为空的那个判断值。 - /// - public bool ReturnValue { get; } - } - - /// - /// 指定的参数传入 null 时才可能返回 null,指定的参数传入非 null 时就不可能返回 null。 - /// - [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue, AllowMultiple = true, Inherited = false)] -#if USE_PUBLIC_LATEST_CSHARP_FEATURES - public -#else - internal -#endif - sealed class NotNullIfNotNullAttribute : Attribute - { - /// - /// 使用一个参数名称决定返回值是否可能为 null。 - /// - /// - /// 指定一个方法传入参数的名称,当这个参数传入非 null 时,输出参数或者返回值就是非 null;而这个参数传入可为 null 时,输出参数或者返回值就可为 null。 - /// - public NotNullIfNotNullAttribute(string parameterName) => ParameterName = parameterName; - - /// - /// 获取决定输出参数或者返回值是否可能为空的那个参数名称。 - /// - public string ParameterName { get; } - } - - /// - /// 指定一个方法是不可能返回的。 - /// - [AttributeUsage(AttributeTargets.Method, Inherited = false)] -#if USE_PUBLIC_LATEST_CSHARP_FEATURES - public -#else - internal -#endif - sealed class DoesNotReturnAttribute : Attribute { } - - /// - /// 在方法的输入参数上指定一个条件,当这个参数传入了指定的 true/false 时方法不可能返回。 - /// - [AttributeUsage(AttributeTargets.Parameter, Inherited = false)] -#if USE_PUBLIC_LATEST_CSHARP_FEATURES - public -#else - internal -#endif - sealed class DoesNotReturnIfAttribute : Attribute - { - /// - /// 使用 true/false 决定方法是否可能返回。 - /// - /// - /// 在方法的输入参数上指定一个条件,当这个参数传入的值等于这里设定的值时,方法不可能返回。 - /// - public DoesNotReturnIfAttribute(bool parameterValue) => ParameterValue = parameterValue; - - /// - /// 获取决定方法是否可返回的那个参数的值。 - /// - public bool ParameterValue { get; } - } -#endif - -#if NET5_0_OR_GREATER - // .NET 5.0 开始已包含更多 Nullable Attributes。 -#else - /// - /// 调用了此方法后,即可保证列表中所列出的字段和属性成员将不会为 null。 - /// - [AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, Inherited = false, AllowMultiple = true)] -#if USE_PUBLIC_LATEST_CSHARP_FEATURES - public -#else - internal -#endif - sealed class MemberNotNullAttribute : Attribute - { - /// - /// 指定调用了此方法后,所列出的字段和属性成员将不会为 null。 - /// - /// - /// 将保证不会为 null 的字段或属性名称。 - /// - public MemberNotNullAttribute(string member) => Members = new[] { member }; - - /// - /// 指定调用了此方法后,所列出的字段和属性成员将不会为 null。 - /// - /// - /// 将保证不会为 null 的字段或属性名称列表。 - /// - public MemberNotNullAttribute(params string[] members) => Members = members; - - /// - /// 调用了此方法后保证不会为 null 的字段或属性名称列表。 - /// - public string[] Members { get; } - } - - /// - /// 当返回指定的 true/false 时,即可保证列表中所列出的字段和属性成员将不会为 null。 - /// - [AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, Inherited = false, AllowMultiple = true)] -#if USE_PUBLIC_LATEST_CSHARP_FEATURES - public -#else - internal -#endif - sealed class MemberNotNullWhenAttribute : Attribute - { - /// - /// 使用 true 或者 false 决定是否所列出的字段和属性成员将不会为 null。 - /// - /// - /// 如果方法或属性的返回值等于这个值,那么所列出的字段和属性成员将不会为 null。 - /// - /// - /// 将保证不会为 null 的字段或属性名称列表。 - /// - public MemberNotNullWhenAttribute(bool returnValue, string member) - { - ReturnValue = returnValue; - Members = new[] { member }; - } - - /// - /// 使用 true 或者 false 决定是否所列出的字段和属性成员将不会为 null。 - /// - /// - /// 如果方法或属性的返回值等于这个值,那么所列出的字段和属性成员将不会为 null。 - /// - /// - /// 将保证不会为 null 的字段或属性名称列表。 - /// - public MemberNotNullWhenAttribute(bool returnValue, params string[] members) - { - ReturnValue = returnValue; - Members = members; - } - - /// - /// 获取返回值决定是否不可为空的那个判断值。 - /// - public bool ReturnValue { get; } - - /// - /// 调用了此方法后保证不会为 null 的字段或属性名称列表。 - /// - public string[] Members { get; } - } -#endif -} diff --git a/src/dotnetCampus.Nullable.Source/dotnetCampus.Nullable.Source.csproj b/src/dotnetCampus.Nullable.Source/dotnetCampus.Nullable.Source.csproj deleted file mode 100644 index f358221..0000000 --- a/src/dotnetCampus.Nullable.Source/dotnetCampus.Nullable.Source.csproj +++ /dev/null @@ -1,14 +0,0 @@ - - - - net5.0;netcoreapp3.0;netstandard2.0;net40 - System.Diagnostics.CodeAnalysis - dotnetCampus.Nullable - If you use C# 8.0 or later and enable the nullable types, this '.Source' library adds internal nullable reference attributes to your project. This confines the new language feature to the project itself without affecting other projects that reference it. - - - - - - - diff --git a/src/dotnetCampus.Nullable/_._ b/src/dotnetCampus.Nullable/_._ deleted file mode 100644 index e69de29..0000000 diff --git a/src/dotnetCampus.Nullable/dotnetCampus.Nullable.csproj b/src/dotnetCampus.Nullable/dotnetCampus.Nullable.csproj deleted file mode 100644 index 13c7890..0000000 --- a/src/dotnetCampus.Nullable/dotnetCampus.Nullable.csproj +++ /dev/null @@ -1,25 +0,0 @@ - - - - net5.0;netcoreapp3.0;netstandard2.0;net40 - true - System.Diagnostics.CodeAnalysis - If you use C# 8.0 or later and enable the nullable types, this library adds public nullable reference attributes to your project, allowing the new language feature to be propagated to other projects that reference this one. - $(DefineConstants);USE_PUBLIC_LATEST_CSHARP_FEATURES - - - - false - - - - - - - - - - - - - diff --git a/src/dotnetCampus.Required.Source/dotnetCampus.Required.Source.csproj b/src/dotnetCampus.Required.Source/dotnetCampus.Required.Source.csproj deleted file mode 100644 index 30e8a6c..0000000 --- a/src/dotnetCampus.Required.Source/dotnetCampus.Required.Source.csproj +++ /dev/null @@ -1,22 +0,0 @@ - - - - net7.0;netcoreapp3.0;netstandard2.0;net40 - System.Diagnostics.CodeAnalysis - dotnetCampus.Required - For C# 11.0 and newer. This package adds support for the 'required' keyword in your project, enforcing that fields or properties are initialized during object initialization. The types introduced are internal, meaning the 'required' functionality is limited to this project and won't propagate to referencing projects. - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/dotnetCampus.Required/_._ b/src/dotnetCampus.Required/_._ deleted file mode 100644 index e69de29..0000000 diff --git a/src/dotnetCampus.Required/dotnetCampus.Required.csproj b/src/dotnetCampus.Required/dotnetCampus.Required.csproj deleted file mode 100644 index f353856..0000000 --- a/src/dotnetCampus.Required/dotnetCampus.Required.csproj +++ /dev/null @@ -1,29 +0,0 @@ - - - - net7.0;net5.0;netcoreapp3.0;netstandard2.0;net40 - true - System.Diagnostics.CodeAnalysis - If you use C# 9.0 or later and want to use record types, this library adds public IsExternalInit attribute support for your project, allowing the new language feature to be propagated to other projects that reference this one. - $(DefineConstants);USE_PUBLIC_LATEST_CSHARP_FEATURES - - - - false - - - - - - - - - - - - - - - - - diff --git a/tests/dotnetCampus.LatestCSharpFeatures.Tests/dotnetCampus.LatestCSharpFeatures.Tests.csproj b/tests/dotnetCampus.LatestCSharpFeatures.Tests/dotnetCampus.LatestCSharpFeatures.Tests.csproj index d2d03ce..e343a95 100644 --- a/tests/dotnetCampus.LatestCSharpFeatures.Tests/dotnetCampus.LatestCSharpFeatures.Tests.csproj +++ b/tests/dotnetCampus.LatestCSharpFeatures.Tests/dotnetCampus.LatestCSharpFeatures.Tests.csproj @@ -1,15 +1,23 @@  - net7.0;net6.0;net5.0;netcoreapp3.1;netcoreapp3.0;netstandard2.0;net48;net472;net471;net47;net462;net461;net46;net452;net451;net45;net40 + net8.0;net7.0;net6.0;net5.0;netcoreapp3.1;netcoreapp3.0;netstandard2.0;net48;net472;net471;net47;net462;net461;net46;net452;net451;net45;net40 disable enable false true + + + $(NoWarn);NETSDK1138;NU1504 + + - +