-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
加上 dotnetCampus.LatestCsharpFeatures.Source
- Loading branch information
Showing
12 changed files
with
291 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
<Project> | ||
<PropertyGroup> | ||
<Version>0.0.0-alpha01</Version> | ||
<Version>0.0.1-alpha01</Version> | ||
</PropertyGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
19 changes: 19 additions & 0 deletions
19
src/dotnetCampus.LatestCsharpFeatures.Source/IsExternalInit.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#if NET5_0_OR_GREATER | ||
// .NET 5.0 开始已包含 IsExternalInit。 | ||
#else | ||
// 旧框架需要包含 IsExternalInit。 | ||
using System.ComponentModel; | ||
|
||
namespace System.Runtime.CompilerServices | ||
{ | ||
/// <summary> | ||
/// Reserved to be used by the compiler for tracking metadata. | ||
/// This class should not be used by developers in source code. | ||
/// This dummy class is required to compile records when targeting .NET Standard | ||
/// </summary> | ||
[EditorBrowsable(EditorBrowsableState.Never)] | ||
internal static class IsExternalInit | ||
{ | ||
} | ||
} | ||
#endif |
193 changes: 193 additions & 0 deletions
193
src/dotnetCampus.LatestCsharpFeatures.Source/Nullable.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,193 @@ | ||
namespace System.Diagnostics.CodeAnalysis | ||
{ | ||
#if NETCOREAPP3_0 || NETCOREAPP3_1 || NET5_0_OR_GREATER | ||
// 新框架都包含基本的 Nullable Attributes。 | ||
#else | ||
/// <summary> | ||
/// 标记一个不可空的输入实际上是可以传入 null 的。 | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property, Inherited = false)] | ||
internal sealed class AllowNullAttribute : Attribute { } | ||
|
||
/// <summary> | ||
/// 标记一个可空的输入实际上不应该传入 null。 | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property, Inherited = false)] | ||
internal sealed class DisallowNullAttribute : Attribute { } | ||
|
||
/// <summary> | ||
/// 标记一个非空的返回值实际上可能会返回 null,返回值包括输出参数。 | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue, Inherited = false)] | ||
internal sealed class MaybeNullAttribute : Attribute { } | ||
|
||
/// <summary> | ||
/// 标记一个可空的返回值实际上是不可能返回 null 的,返回值包括输出参数。 | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue, Inherited = false)] | ||
internal sealed class NotNullAttribute : Attribute { } | ||
|
||
/// <summary> | ||
/// 当返回指定的 true/false 时某个输出参数才可能为 null,而返回相反的值时那个输出参数则不可为 null。 | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Parameter, Inherited = false)] | ||
internal sealed class MaybeNullWhenAttribute : Attribute | ||
{ | ||
/// <summary> | ||
/// 使用 true 或者 false 决定输出参数是否可能为 null。 | ||
/// </summary> | ||
/// <param name="returnValue">如果方法返回值等于这个值,那么输出参数则可能为 null,否则输出参数是不可为 null 的。</param> | ||
public MaybeNullWhenAttribute(bool returnValue) => ReturnValue = returnValue; | ||
|
||
/// <summary> | ||
/// 获取返回值决定是否可为空的那个判断值。 | ||
/// </summary> | ||
public bool ReturnValue { get; } | ||
} | ||
|
||
/// <summary> | ||
/// 当返回指定的 true/false 时,某个输出参数不可为 null,而返回相反的值时那个输出参数则可能为 null。 | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Parameter, Inherited = false)] | ||
internal sealed class NotNullWhenAttribute : Attribute | ||
{ | ||
/// <summary> | ||
/// 使用 true 或者 false 决定输出参数是否不可为 null。 | ||
/// </summary> | ||
/// <param name="returnValue"> | ||
/// 如果方法或属性的返回值等于这个值,那么输出参数则不可为 null,否则输出参数是可能为 null 的。 | ||
/// </param> | ||
public NotNullWhenAttribute(bool returnValue) => ReturnValue = returnValue; | ||
|
||
/// <summary> | ||
/// 获取返回值决定是否不可为空的那个判断值。 | ||
/// </summary> | ||
public bool ReturnValue { get; } | ||
} | ||
|
||
/// <summary> | ||
/// 指定的参数传入 null 时才可能返回 null,指定的参数传入非 null 时就不可能返回 null。 | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue, AllowMultiple = true, Inherited = false)] | ||
internal sealed class NotNullIfNotNullAttribute : Attribute | ||
{ | ||
/// <summary> | ||
/// 使用一个参数名称决定返回值是否可能为 null。 | ||
/// </summary> | ||
/// <param name="parameterName"> | ||
/// 指定一个方法传入参数的名称,当这个参数传入非 null 时,输出参数或者返回值就是非 null;而这个参数传入可为 null 时,输出参数或者返回值就可为 null。 | ||
/// </param> | ||
public NotNullIfNotNullAttribute(string parameterName) => ParameterName = parameterName; | ||
|
||
/// <summary> | ||
/// 获取决定输出参数或者返回值是否可能为空的那个参数名称。 | ||
/// </summary> | ||
public string ParameterName { get; } | ||
} | ||
|
||
/// <summary> | ||
/// 指定一个方法是不可能返回的。 | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Method, Inherited = false)] | ||
internal sealed class DoesNotReturnAttribute : Attribute { } | ||
|
||
/// <summary> | ||
/// 在方法的输入参数上指定一个条件,当这个参数传入了指定的 true/false 时方法不可能返回。 | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Parameter, Inherited = false)] | ||
internal sealed class DoesNotReturnIfAttribute : Attribute | ||
{ | ||
/// <summary> | ||
/// 使用 true/false 决定方法是否可能返回。 | ||
/// </summary> | ||
/// <param name="parameterValue"> | ||
/// 在方法的输入参数上指定一个条件,当这个参数传入的值等于这里设定的值时,方法不可能返回。 | ||
/// </param> | ||
public DoesNotReturnIfAttribute(bool parameterValue) => ParameterValue = parameterValue; | ||
|
||
/// <summary> | ||
/// 获取决定方法是否可返回的那个参数的值。 | ||
/// </summary> | ||
public bool ParameterValue { get; } | ||
} | ||
#endif | ||
|
||
#if NET5_0_OR_GREATER | ||
// .NET 5.0 开始已包含更多 Nullable Attributes。 | ||
#else | ||
/// <summary> | ||
/// 调用了此方法后,即可保证列表中所列出的字段和属性成员将不会为 null。 | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, Inherited = false, AllowMultiple = true)] | ||
internal sealed class MemberNotNullAttribute : Attribute | ||
{ | ||
/// <summary> | ||
/// 指定调用了此方法后,所列出的字段和属性成员将不会为 null。 | ||
/// </summary> | ||
/// <param name="member"> | ||
/// 将保证不会为 null 的字段或属性名称。 | ||
/// </param> | ||
public MemberNotNullAttribute(string member) => Members = new[] { member }; | ||
|
||
/// <summary> | ||
/// 指定调用了此方法后,所列出的字段和属性成员将不会为 null。 | ||
/// </summary> | ||
/// <param name="members"> | ||
/// 将保证不会为 null 的字段或属性名称列表。 | ||
/// </param> | ||
public MemberNotNullAttribute(params string[] members) => Members = members; | ||
|
||
/// <summary> | ||
/// 调用了此方法后保证不会为 null 的字段或属性名称列表。 | ||
/// </summary> | ||
public string[] Members { get; } | ||
} | ||
|
||
/// <summary> | ||
/// 当返回指定的 true/false 时,即可保证列表中所列出的字段和属性成员将不会为 null。 | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, Inherited = false, AllowMultiple = true)] | ||
internal sealed class MemberNotNullWhenAttribute : Attribute | ||
{ | ||
/// <summary> | ||
/// 使用 true 或者 false 决定是否所列出的字段和属性成员将不会为 null。 | ||
/// </summary> | ||
/// <param name="returnValue"> | ||
/// 如果方法或属性的返回值等于这个值,那么所列出的字段和属性成员将不会为 null。 | ||
/// </param> | ||
/// <param name="member"> | ||
/// 将保证不会为 null 的字段或属性名称列表。 | ||
/// </param> | ||
public MemberNotNullWhenAttribute(bool returnValue, string member) | ||
{ | ||
ReturnValue = returnValue; | ||
Members = new[] { member }; | ||
} | ||
|
||
/// <summary> | ||
/// 使用 true 或者 false 决定是否所列出的字段和属性成员将不会为 null。 | ||
/// </summary> | ||
/// <param name="returnValue"> | ||
/// 如果方法或属性的返回值等于这个值,那么所列出的字段和属性成员将不会为 null。 | ||
/// </param> | ||
/// <param name="members"> | ||
/// 将保证不会为 null 的字段或属性名称列表。 | ||
/// </param> | ||
public MemberNotNullWhenAttribute(bool returnValue, params string[] members) | ||
{ | ||
ReturnValue = returnValue; | ||
Members = members; | ||
} | ||
|
||
/// <summary> | ||
/// 获取返回值决定是否不可为空的那个判断值。 | ||
/// </summary> | ||
public bool ReturnValue { get; } | ||
|
||
/// <summary> | ||
/// 调用了此方法后保证不会为 null 的字段或属性名称列表。 | ||
/// </summary> | ||
public string[] Members { get; } | ||
} | ||
#endif | ||
} |
48 changes: 48 additions & 0 deletions
48
src/dotnetCampus.LatestCsharpFeatures.Source/RequiredMemberAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#if NET7_0_OR_GREATER | ||
// .NET 7.0 开始已包含 required。 | ||
#else | ||
// 旧框架需要包含 required。 | ||
namespace System.Runtime.CompilerServices | ||
{ | ||
/// <summary> | ||
/// Specifies that a type has required members or that a member is required. | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = false)] | ||
|
||
internal sealed class RequiredMemberAttribute : Attribute | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Indicates that compiler support for a particular feature is required for the location where this attribute is applied. | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.All, AllowMultiple = true, Inherited = false)] | ||
internal sealed class CompilerFeatureRequiredAttribute : Attribute | ||
{ | ||
public CompilerFeatureRequiredAttribute(string featureName) | ||
{ | ||
FeatureName = featureName; | ||
} | ||
|
||
/// <summary> | ||
/// The name of the compiler feature. | ||
/// </summary> | ||
public string FeatureName { get; } | ||
|
||
/// <summary> | ||
/// If true, the compiler can choose to allow access to the location where this attribute is applied if it does not understand <see cref="FeatureName"/>. | ||
/// </summary> | ||
public bool IsOptional { get; init; } | ||
|
||
/// <summary> | ||
/// The <see cref="FeatureName"/> used for the ref structs C# feature. | ||
/// </summary> | ||
public const string RefStructs = nameof(RefStructs); | ||
|
||
/// <summary> | ||
/// The <see cref="FeatureName"/> used for the required members C# feature. | ||
/// </summary> | ||
public const string RequiredMembers = nameof(RequiredMembers); | ||
} | ||
} | ||
#endif |
16 changes: 16 additions & 0 deletions
16
src/dotnetCampus.LatestCsharpFeatures.Source/SetsRequiredMembersAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#if NET7_0_OR_GREATER | ||
// .NET 7.0 开始已包含 required。 | ||
#else | ||
// 旧框架需要包含 required。 | ||
namespace System.Diagnostics.CodeAnalysis | ||
{ | ||
/// <summary> | ||
/// Specifies that this constructor sets all required members for the current type, and callers | ||
/// do not need to set any required members themselves. | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Constructor, AllowMultiple = false, Inherited = false)] | ||
internal sealed class SetsRequiredMembersAttribute : Attribute | ||
{ | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters