Skip to content

Commit fd871d6

Browse files
authored
Merge pull request #7 from dotnet-campus/t/walterlv/index-range
Add System.Index and System.Range
2 parents c8a7a9c + 7ded21c commit fd871d6

27 files changed

Lines changed: 477 additions & 156 deletions

Directory.Build.props

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@
77
<LangVersion>latest</LangVersion>
88
<Nullable>enable</Nullable>
99
<Deterministic>true</Deterministic>
10-
<GenerateDocumentationFile>True</GenerateDocumentationFile>
11-
<ArtifactsPath>$(MSBuildThisFileDirectory)artifacts</ArtifactsPath>
1210
<RepositoryRoot>$(MSBuildThisFileDirectory)</RepositoryRoot>
11+
<ArtifactsPath>$(MSBuildThisFileDirectory)artifacts\</ArtifactsPath>
1312
</PropertyGroup>
1413

1514
<PropertyGroup>
@@ -28,8 +27,8 @@
2827
<Company>dotnet campus(.NET 职业技术学院)</Company>
2928
<Copyright>Copyright © 2023-2024 dotnet campus, All Rights Reserved.</Copyright>
3029
<RepositoryType>git</RepositoryType>
31-
<RepositoryUrl>https://github.com/dotnet-campus/dotnetCampus.LatestCSharpFeatures</RepositoryUrl>
32-
<PackageProjectUrl>https://github.com/dotnet-campus/dotnetCampus.LatestCSharpFeatures</PackageProjectUrl>
30+
<RepositoryUrl>https://github.com/dotnet-campus/DotNetCampus.LatestCSharpFeatures</RepositoryUrl>
31+
<PackageProjectUrl>https://github.com/dotnet-campus/DotNetCampus.LatestCSharpFeatures</PackageProjectUrl>
3332
</PropertyGroup>
3433

3534
</Project>
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "build", "build", "{8A6F61D3
1313
build\Version.props = build\Version.props
1414
EndProjectSection
1515
EndProject
16-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "dotnetCampus.LatestCSharpFeatures", "src\dotnetCampus.LatestCSharpFeatures\dotnetCampus.LatestCSharpFeatures.csproj", "{D0B0F7EB-3C81-45C4-BB47-5DCB16F2EA55}"
16+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DotNetCampus.LatestCSharpFeatures", "src\DotNetCampus.LatestCSharpFeatures\DotNetCampus.LatestCSharpFeatures.csproj", "{D0B0F7EB-3C81-45C4-BB47-5DCB16F2EA55}"
1717
ProjectSection(ProjectDependencies) = postProject
1818
{642FA0CA-E827-4D96-9358-D596CD87DAF4} = {642FA0CA-E827-4D96-9358-D596CD87DAF4}
1919
EndProjectSection
2020
EndProject
2121
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{878C1D77-0A77-44BD-847E-164188B10677}"
2222
EndProject
23-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "dotnetCampus.LatestCSharpFeatures.Tests", "tests\dotnetCampus.LatestCSharpFeatures.Tests\dotnetCampus.LatestCSharpFeatures.Tests.csproj", "{D10BF09A-FDF3-43F0-BD54-49A73554CDAB}"
23+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DotNetCampus.LatestCSharpFeatures.Tests", "tests\DotNetCampus.LatestCSharpFeatures.Tests\DotNetCampus.LatestCSharpFeatures.Tests.csproj", "{D10BF09A-FDF3-43F0-BD54-49A73554CDAB}"
2424
EndProject
25-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "dotnetCampus.LatestCSharpFeatures.Analyzer", "src\dotnetCampus.LatestCSharpFeatures.Analyzer\dotnetCampus.LatestCSharpFeatures.Analyzer.csproj", "{642FA0CA-E827-4D96-9358-D596CD87DAF4}"
25+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DotNetCampus.LatestCSharpFeatures.Analyzer", "src\DotNetCampus.LatestCSharpFeatures.Analyzer\DotNetCampus.LatestCSharpFeatures.Analyzer.csproj", "{642FA0CA-E827-4D96-9358-D596CD87DAF4}"
2626
EndProject
2727
Global
2828
GlobalSection(SolutionConfigurationPlatforms) = preSolution

LICENSE.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2023 dotnetCampus.LatestCSharpFeatures
3+
Copyright (c) 2023-2025 DotNetCampus.LatestCSharpFeatures
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
# Latest CSharp Features
22

3-
| Language |
4-
| ---------------------------------- |
5-
| [简体中文](/docs/README.zh-CHS.md) |
6-
| [繁體中文](/docs/README.zh-CHT.md) |
7-
| [English](/README.md) |
3+
| [English][en] | [简体中文][zh-hans] | [繁體中文][zh-hant] |
4+
| ------------- | ------------------- | ------------------- |
5+
6+
[en]: /README.md
7+
[zh-hans]: /docs/README.zh-hans.md
8+
[zh-hant]: /docs/README.zh-hant.md
89

910
[![NuGet](https://img.shields.io/nuget/v/dotnetCampus.LatestCSharpFeatures.svg)](https://www.nuget.org/packages/dotnetCampus.LatestCSharpFeatures)
1011

@@ -23,11 +24,22 @@ Simply install the dotnetCampus.LatestCSharpFeatures NuGet package.
2324
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:
2425

2526
```xml
26-
<!-- By default, dotnetCampus.LatestCSharpFeatures introduces new C# features into the current project using the internal modifier.
27-
By using this conditional compilation symbol, these types can be set to public, allowing other projects referencing this project to also use these new features. -->
27+
<!-- By default, dotnetCampus.LatestCSharpFeatures introduces most new C# features into the current project using the internal modifier.
28+
By using this conditional compilation symbol, these types can be set to public, allowing other projects referencing this project to also use these new features.
29+
Note: Index/Range types are always internal and not affected by this setting. See explanation below. -->
2830
<DefineConstants>$(DefineConstants);USE_PUBLIC_LATEST_CSHARP_FEATURES</DefineConstants>
2931
```
3032

33+
If your project already references other libraries that provide implementations of Index/Range, type conflicts may occur. In such cases, you can disable our Index/Range implementation:
34+
35+
```xml
36+
<!-- If your project already has other Index/Range implementations (e.g., through the official System.Runtime.CompilerServices.IndexRange or other third-party libraries),
37+
use this conditional compilation symbol to disable our Index/Range implementation to avoid type conflicts. -->
38+
<DefineConstants>$(DefineConstants);DISABLE_LATEST_CSHARP_FEATURES_INDEX_RANGE</DefineConstants>
39+
```
40+
41+
> **Special Note**: Unlike other features, Index/Range types always maintain an internal access level and will not become public due to the `USE_PUBLIC_LATEST_CSHARP_FEATURES` setting. This is because Index/Range are used in public APIs, and if set to public, they may cause compatibility issues with other libraries that use the official Index/Range implementation. Other features are typically for internal use and do not cause such compatibility issues.
42+
3143
## Feedback and Contributions
3244

3345
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.

build/Version.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project>
22
<PropertyGroup>
33
<!-- 此版本号仅作标记使用,标记当前支持的最新 C# 版本 -->
4-
<Version>12.0.0</Version>
4+
<Version>13.0.0</Version>
55
</PropertyGroup>
66
</Project>
Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
# Latest CSharp Features - 最新 C# 语言特性
22

3-
| Language |
4-
| ---------------------------------- |
5-
| [简体中文](/docs/README.zh-CHS.md) |
6-
| [繁體中文](/docs/README.zh-CHT.md) |
7-
| [English](/README.md) |
3+
| [English][en] | [简体中文][zh-hans] | [繁體中文][zh-hant] |
4+
| ------------- | ------------------- | ------------------- |
5+
6+
[en]: /README.md
7+
[zh-hans]: /docs/README.zh-hans.md
8+
[zh-hant]: /docs/README.zh-hant.md
89

910
[![NuGet](https://img.shields.io/nuget/v/dotnetCampus.LatestCSharpFeatures.svg)](https://www.nuget.org/packages/dotnetCampus.LatestCSharpFeatures)
1011

@@ -23,11 +24,22 @@
2324
如果你希望这些新语言特性对其他引用了此项目的项目也生效,可以在 csproj 文件中增加一个条件编译符:
2425

2526
```xml
26-
<!-- 默认情况下,dotnetCampus.LatestCSharpFeatures 会将 C# 新特性以 internal 修饰符引入当前项目。
27-
使用此条件编译符,可以将这些类型设为 public,使得引用此项目的其他项目也能使用这些新特性。 -->
27+
<!-- 默认情况下,dotnetCampus.LatestCSharpFeatures 会将大多数 C# 新特性以 internal 修饰符引入当前项目。
28+
使用此条件编译符,可以将这些类型设为 public,使得引用此项目的其他项目也能使用这些新特性。
29+
注意:Index/Range 类型始终是 internal 的,不受此设置影响,详见下方说明。 -->
2830
<DefineConstants>$(DefineConstants);USE_PUBLIC_LATEST_CSHARP_FEATURES</DefineConstants>
2931
```
3032

33+
如果你的项目已经引用了其他提供 Index/Range 实现的库,可能会发生类型冲突。在这种情况下,你可以禁用我们提供的 Index/Range 实现:
34+
35+
```xml
36+
<!-- 如果你的项目中已经有了其他的 Index/Range 实现(例如通过官方的 System.Runtime.CompilerServices.IndexRange 或其他第三方库),
37+
使用此条件编译符可以禁用我们提供的 Index/Range 实现,以避免类型冲突。 -->
38+
<DefineConstants>$(DefineConstants);DISABLE_LATEST_CSHARP_FEATURES_INDEX_RANGE</DefineConstants>
39+
```
40+
41+
> **特别说明**:与其他特性不同,Index/Range 类型始终保持为 internal 访问级别,不会因为 `USE_PUBLIC_LATEST_CSHARP_FEATURES` 设置而变为 public。这是因为 Index/Range 会被用在公开的 API 中,如果设为 public 可能会与使用官方 Index/Range 实现的其他库产生不兼容问题。其他特性通常是自用类型,不会导致此类兼容性问题。
42+
3143
## 反馈与贡献
3244

3345
我们欢迎所有用户的反馈和贡献。如果你在使用过程中发现任何问题,或者有任何改进建议,都可以通过 GitHub Issues 提交。
Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
# Latest CSharp Features 最新的 C# 特性
22

3-
| Language |
4-
| ---------------------------------- |
5-
| [简体中文](/docs/README.zh-CHS.md) |
6-
| [繁體中文](/docs/README.zh-CHT.md) |
7-
| [English](/README.md) |
3+
| [English][en] | [简体中文][zh-hans] | [繁體中文][zh-hant] |
4+
| ------------- | ------------------- | ------------------- |
5+
6+
[en]: /README.md
7+
[zh-hans]: /docs/README.zh-hans.md
8+
[zh-hant]: /docs/README.zh-hant.md
89

910
[![NuGet](https://img.shields.io/nuget/v/dotnetCampus.LatestCSharpFeatures.svg)](https://www.nuget.org/packages/dotnetCampus.LatestCSharpFeatures)
1011

@@ -23,11 +24,22 @@
2324
如果您希望這些新語言特性對其他引用了此項目的項目也生效,可以在 csproj 文件中增加一個條件編譯符:
2425

2526
```xml
26-
<!-- 預設情況下,dotnetCampus.LatestCSharpFeatures 會將 C# 新特性以 internal 修飾符引入當前項目。
27-
使用此條件編譯符,可以將這些類型設為 public,使得引用此項目的其他項目也能使用這些新特性。 -->
27+
<!-- 預設情況下,dotnetCampus.LatestCSharpFeatures 會將大多數 C# 新特性以 internal 修飾符引入當前項目。
28+
使用此條件編譯符,可以將這些類型設為 public,使得引用此項目的其他項目也能使用這些新特性。
29+
注意:Index/Range 類型始終是 internal 的,不受此設置影響,詳見下方說明。 -->
2830
<DefineConstants>$(DefineConstants);USE_PUBLIC_LATEST_CSHARP_FEATURES</DefineConstants>
2931
```
3032

33+
如果您的項目已經引用了其他提供 Index/Range 實現的庫,可能會發生類型衝突。在這種情況下,您可以禁用我們提供的 Index/Range 實現:
34+
35+
```xml
36+
<!-- 如果您的項目中已經有了其他的 Index/Range 實現(例如通過官方的 System.Runtime.CompilerServices.IndexRange 或其他第三方庫),
37+
使用此條件編譯符可以禁用我們提供的 Index/Range 實現,以避免類型衝突。 -->
38+
<DefineConstants>$(DefineConstants);DISABLE_LATEST_CSHARP_FEATURES_INDEX_RANGE</DefineConstants>
39+
```
40+
41+
> **特別說明**:與其他特性不同,Index/Range 類型始終保持為 internal 訪問級別,不會因為 `USE_PUBLIC_LATEST_CSHARP_FEATURES` 設置而變為 public。這是因為 Index/Range 會被用在公開的 API 中,如果設為 public 可能會與使用官方 Index/Range 實現的其他庫產生不兼容問題。其他特性通常是自用類型,不會導致此類兼容性問題。
42+
3143
## 反饋與貢獻
3244

3345
我們歡迎所有用戶的反饋和貢獻。如果您在使用過程中發現任何問題,或者有任何改進建議,都可以通過 GitHub Issues 提交。

src/dotnetCampus.LatestCSharpFeatures.Analyzer/dotnetCampus.LatestCSharpFeatures.Analyzer.csproj renamed to src/DotNetCampus.LatestCSharpFeatures.Analyzer/DotNetCampus.LatestCSharpFeatures.Analyzer.csproj

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFramework>netstandard2.0</TargetFramework>
5+
<RootNamespace>DotNetCampus.LatestCSharpFeatures</RootNamespace>
56
<ImplicitUsings>true</ImplicitUsings>
67
<IsPackable>false</IsPackable>
78
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
@@ -15,8 +16,9 @@
1516
</ItemGroup>
1617

1718
<ItemGroup>
18-
<Compile Include="..\dotnetCampus.LatestCSharpFeatures\ExternalInit\*.cs" Link="Features\ExternalInit\%(RecursiveDir)\%(FileName)%(Extension)" />
19-
<EmbeddedResource Include="..\dotnetCampus.LatestCSharpFeatures\**\*.cs" Link="Features\%(RecursiveDir)\%(FileName)%(Extension)" />
19+
<Compile Include="..\DotNetCampus.LatestCSharpFeatures\ExternalInit\*.cs" LinkBase="Features\ExternalInit" />
20+
<Compile Include="..\DotNetCampus.LatestCSharpFeatures\IndexRange\*.cs" LinkBase="Features\IndexRange" />
21+
<EmbeddedResource Include="..\DotNetCampus.LatestCSharpFeatures\**\*.cs" LinkBase="Features" />
2022
</ItemGroup>
2123

2224
</Project>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace DotNetCampus.LatestCSharpFeatures;
2+
3+
internal static class GeneratorInfo
4+
{
5+
public static string RootNamespace { get; } = typeof(GeneratorInfo).Namespace!;
6+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System.Text;
2+
using DotNetCampus.LatestCSharpFeatures.Utils;
3+
using Microsoft.CodeAnalysis;
4+
using Microsoft.CodeAnalysis.Text;
5+
6+
namespace DotNetCampus.LatestCSharpFeatures.Generators;
7+
8+
/// <summary>
9+
/// 生成 C# 新特性所需的类。
10+
/// </summary>
11+
[Generator(LanguageNames.CSharp)]
12+
public class FeatureGenerator : IIncrementalGenerator
13+
{
14+
public void Initialize(IncrementalGeneratorInitializationContext context)
15+
{
16+
context.RegisterPostInitializationOutput(OnExecute);
17+
}
18+
19+
private void OnExecute(IncrementalGeneratorPostInitializationContext context)
20+
{
21+
// .NET Core 3.0 / .NET Standard 2.1 才开始支持 Nullable;.NET 5.0 开始支持更多。
22+
GenerateFeatureSource(context, "Nullable");
23+
24+
// .NET Core 3.0 / .NET Standard 2.1 才开始支持 Index 和 Range。
25+
GenerateFeatureSource(context, "IndexRange");
26+
27+
// .NET 5.0 才开始支持 ExternalInit。
28+
GenerateFeatureSource(context, "ExternalInit");
29+
30+
// .NET 6.0 才开始支持 DynamicallyAccessed
31+
// 为低版本 .NET 生成对应代码,主要是简化编译多目标框架时的繁琐。
32+
GenerateFeatureSource(context, "DynamicallyAccessed");
33+
34+
// .NET 7.0 才开始支持 SetsRequiredMembersAttribute。
35+
GenerateFeatureSource(context, "Required");
36+
}
37+
38+
private void GenerateFeatureSource(IncrementalGeneratorPostInitializationContext context, string featureName)
39+
{
40+
foreach (var source in EmbeddedSourceFiles.Enumerate($"Features.{featureName}"))
41+
{
42+
context.AddSource(source.FileName, SourceText.From(source.Content, Encoding.UTF8));
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)