Skip to content

Commit

Permalink
加上 dotnetCampus.LatestCsharpFeatures.Source
Browse files Browse the repository at this point in the history
  • Loading branch information
walterlv committed Jun 1, 2023
1 parent 1861128 commit e4d5412
Show file tree
Hide file tree
Showing 12 changed files with 291 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="all"/>
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.2" PrivateAssets="all" />
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.3" PrivateAssets="all" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion build/Version.props
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>
2 changes: 1 addition & 1 deletion src/dotnetCampus.IsExternalInit.Source/IsExternalInit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace System.Runtime.CompilerServices
/// This dummy class is required to compile records when targeting .NET Standard
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public static class IsExternalInit
internal static class IsExternalInit
{
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net5.0;netcoreapp3.0;netstandard1.0;net40</TargetFrameworks>
Expand All @@ -9,7 +9,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="dotnetCampus.SourceYard" Version="1.0.0-alpha01" PrivateAssets="all" />
<PackageReference Include="dotnetCampus.SourceYard" Version="1.0.0-alpha03" PrivateAssets="all" />
</ItemGroup>

</Project>
5 changes: 0 additions & 5 deletions src/dotnetCampus.LatestCsharpFeatures.Source/Class1.cs

This file was deleted.

19 changes: 19 additions & 0 deletions src/dotnetCampus.LatestCsharpFeatures.Source/IsExternalInit.cs
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 src/dotnetCampus.LatestCsharpFeatures.Source/Nullable.cs
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
}
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
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
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="dotnetCampus.SourceYard" Version="1.0.0-alpha01" PrivateAssets="all" />
<PackageReference Include="dotnetCampus.SourceYard" Version="1.0.0-alpha03" PrivateAssets="all" />
</ItemGroup>

<!-- 源代码包暂时不支持 Link 过来的文件,所以这里先不用 Link -->
<!--<ItemGroup>
<Compile Include="..\dotnetCampus.Nullable\**\*.cs" Exclude="..\dotnetCampus.Nullable\**\obj\**\*.cs;..\dotnetCampus.Nullable\**\bin\**\*.cs" />
<Compile Include="..\dotnetCampus.IsExternalInit\**\*.cs" Exclude="..\dotnetCampus.IsExternalInit\**\obj\**\*.cs;..\dotnetCampus.IsExternalInit\**\bin\**\*.cs" />
<Compile Include="..\dotnetCampus.Required\**\*.cs" Exclude="..\dotnetCampus.Required\**\obj\**\*.cs;..\dotnetCampus.Required\**\bin\**\*.cs" />
</ItemGroup>-->

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="dotnetCampus.SourceYard" Version="1.0.0-alpha01" PrivateAssets="all" />
<PackageReference Include="dotnetCampus.SourceYard" Version="1.0.0-alpha03" PrivateAssets="all" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="dotnetCampus.SourceYard" Version="1.0.0-alpha01" PrivateAssets="all" />
<PackageReference Include="dotnetCampus.SourceYard" Version="1.0.0-alpha03" PrivateAssets="all" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit e4d5412

Please sign in to comment.