Skip to content

Commit 9d994da

Browse files
authored
Merge pull request #96 from feO2x/dev
v11.0.0 Release
2 parents a067d91 + ee447b0 commit 9d994da

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+200
-63
lines changed

.github/workflows/build-and-test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
- name: Setup .NET
1919
uses: actions/setup-dotnet@v3
2020
with:
21-
dotnet-version: 7.0.x
21+
dotnet-version: 8.0.x
2222
- name: Restore dependencies
2323
run: dotnet restore ./Code/Light.GuardClauses.AllProjects.sln
2424
- name: Build Analyzer

.github/workflows/release-on-nuget.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ on:
88
dotnetVersion:
99
description: "The version of .NET to use"
1010
required: false
11-
default: "7.0.x"
11+
default: "8.0.x"
1212

1313
jobs:
1414
release-to-nuget:
@@ -20,7 +20,7 @@ jobs:
2020
- name: Setup .NET
2121
uses: actions/setup-dotnet@v3
2222
with:
23-
dotnet-version: ${{ github.event.inputs.dotnetVersion || '7.0.x' }}
23+
dotnet-version: ${{ github.event.inputs.dotnetVersion || '8.0.x' }}
2424
- name: Prepare SNK file
2525
env:
2626
LIGHT_GUARDCLAUSES_SNK: ${{ secrets.LIGHT_GUARDCLAUSES_SNK }}

Code/Light.GuardClauses.Performance/CommonAssertions/MustBeValidEnumValueBenchmark.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public ConsoleColor NoFlagsBaseVersion()
3535

3636
public static class MustBeValidEnumValueExtensionMethods
3737
{
38-
public static T OldMustBeValidEnumValue<T>(this T parameter, string parameterName = null, string message = null, Func<Exception> exception = null) where T : Enum
38+
public static T OldMustBeValidEnumValue<T>(this T parameter, string parameterName = null, string message = null, Func<Exception> exception = null) where T : struct, Enum
3939
{
4040
if (parameter.IsValidEnumValue())
4141
return parameter;

Code/Light.GuardClauses.Performance/Light.GuardClauses.Performance.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFrameworks>net7.0;net48</TargetFrameworks>
5+
<TargetFrameworks>net8.0;net48</TargetFrameworks>
66
<LangVersion>11</LangVersion>
77
</PropertyGroup>
88

Code/Light.GuardClauses.Tests/CommonAssertions/IsValidEnumValueTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public static class IsValidEnumValueTests
2020
[InlineData(UInt64Enum.AllLow | UInt64Enum.High1)]
2121
[InlineData(UInt64Enum.AllHigh)]
2222
[InlineData(UInt64Enum.MaxValue)]
23-
public static void EnumValueValid<T>(T enumValue) where T : Enum, IComparable
23+
public static void EnumValueValid<T>(T enumValue) where T : struct, Enum, IComparable
2424
{
2525
if (enumValue is BindingFlags)
2626
{
@@ -42,6 +42,7 @@ public static void EnumValueValid<T>(T enumValue) where T : Enum, IComparable
4242
[InlineData(int.MinValue)]
4343
[InlineData(2048)]
4444
[InlineData(2055)]
45+
[InlineData(4096)]
4546
[InlineData(int.MaxValue)]
4647
public static void InvalidNumberStyles(int invalidValue) => ((NumberStyles) invalidValue).IsValidEnumValue().Should().BeFalse();
4748

Code/Light.GuardClauses.Tests/CommonAssertions/MustNotBeEmptyGuidTests.cs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,28 @@ public static void CallerArgumentExpression()
4646
act.Should().Throw<EmptyGuidException>()
4747
.WithParameterName(nameof(emptyGuid));
4848
}
49-
}
49+
50+
public class Entity(Guid id)
51+
{
52+
public Guid Id { get; } = id.MustNotBeEmpty();
53+
}
54+
55+
[Fact]
56+
public static void PrimaryConstructorValidArgument()
57+
{
58+
var guid = Guid.NewGuid();
59+
var entity = new Entity(guid);
60+
61+
entity.Id.Should().Be(guid);
62+
}
63+
64+
[Fact]
65+
public static void PrimaryConstructorInvalidArgument()
66+
{
67+
var act = () => new Entity(Guid.Empty);
68+
69+
act.Should().Throw<EmptyGuidException>()
70+
.And.Message.Should().StartWith("id must be a valid GUID, but it actually is an empty one.");
71+
}
72+
}
73+

Code/Light.GuardClauses.Tests/Light.GuardClauses.Tests.csproj

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33
<Import Project="TargetFrameworks.props" Condition="Exists('TargetFrameworks.props')" />
44

55
<PropertyGroup>
6-
<TargetFrameworks Condition="'$(TargetFrameworks)' == ''">net7.0</TargetFrameworks>
6+
<TargetFrameworks Condition="'$(TargetFrameworks)' == ''">net8.0</TargetFrameworks>
77
<IsPackable>false</IsPackable>
8-
<LangVersion>11.0</LangVersion>
8+
<LangVersion>12</LangVersion>
99
</PropertyGroup>
1010

1111
<ItemGroup>
12-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.0" />
13-
<PackageReference Include="FluentAssertions" Version="6.11.0" />
14-
<PackageReference Include="xunit" Version="2.4.2" />
15-
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.0" />
12+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
13+
<PackageReference Include="FluentAssertions" Version="6.12.0" />
14+
<PackageReference Include="xunit" Version="2.5.1" />
15+
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.1" />
1616
<ProjectReference Include="..\Light.GuardClauses\Light.GuardClauses.csproj" />
1717
</ItemGroup>
1818

Code/Light.GuardClauses/CallerArgumentExpressionAttribute.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
// ReSharper disable once CheckNamespace -- CallerArgumentExpression must be in exactly this namespace
1+
#if !NET8_0
2+
// ReSharper disable once CheckNamespace -- CallerArgumentExpression must be in exactly this namespace
23
namespace System.Runtime.CompilerServices;
34

45
[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = false)]
@@ -10,4 +11,5 @@ public CallerArgumentExpressionAttribute(string parameterName)
1011
}
1112

1213
public string ParameterName { get; }
13-
}
14+
}
15+
#endif

Code/Light.GuardClauses/Check.CommonAssertions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ public static T MustBeOfType<T>([ValidatedNotNull, NoEnumeration] this object? p
175175
/// <typeparam name="T">The type of the enum.</typeparam>
176176
/// <param name="parameter">The enum value to be checked.</param>
177177
[MethodImpl(MethodImplOptions.AggressiveInlining)]
178-
public static bool IsValidEnumValue<T>(this T parameter) where T : Enum
178+
public static bool IsValidEnumValue<T>(this T parameter) where T : struct, Enum
179179
=> EnumInfo<T>.IsValidEnumValue(parameter);
180180

181181
/// <summary>
@@ -189,7 +189,7 @@ public static bool IsValidEnumValue<T>(this T parameter) where T : Enum
189189
/// <param name="message">The message that will be passed to the resulting exception (optional).</param>
190190
/// <exception cref="EnumValueNotDefinedException">Thrown when <paramref name="parameter" /> is no valid enum value.</exception>
191191
[MethodImpl(MethodImplOptions.AggressiveInlining)]
192-
public static T MustBeValidEnumValue<T>(this T parameter, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) where T : Enum
192+
public static T MustBeValidEnumValue<T>(this T parameter, [CallerArgumentExpression("parameter")] string? parameterName = null, string? message = null) where T : struct, Enum
193193
{
194194
if (!EnumInfo<T>.IsValidEnumValue(parameter))
195195
Throw.EnumValueNotDefined(parameter, parameterName, message);
@@ -207,7 +207,7 @@ public static T MustBeValidEnumValue<T>(this T parameter, [CallerArgumentExpress
207207
/// <exception cref="Exception">Your custom exception thrown when <paramref name="parameter" /> is no valid enum value, or when <typeparamref name="T" /> is no enum type.</exception>
208208
[MethodImpl(MethodImplOptions.AggressiveInlining)]
209209
[ContractAnnotation("exceptionFactory:null => halt")]
210-
public static T MustBeValidEnumValue<T>(this T parameter, Func<T, Exception> exceptionFactory) where T : Enum
210+
public static T MustBeValidEnumValue<T>(this T parameter, Func<T, Exception> exceptionFactory) where T : struct, Enum
211211
{
212212
if (!EnumInfo<T>.IsValidEnumValue(parameter))
213213
Throw.CustomException(exceptionFactory, parameter);

Code/Light.GuardClauses/Check.TypeAssertions.cs

Lines changed: 72 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
using System.Collections.Generic;
33
using System.Runtime.CompilerServices;
44
using JetBrains.Annotations;
5+
#if NET8_0
6+
using System.Diagnostics.CodeAnalysis;
7+
#endif
58

69
namespace Light.GuardClauses;
710

@@ -41,11 +44,19 @@ private static bool CheckTypeEquivalency(Type type, Type other)
4144
/// <param name="interfaceType">The interface type that <paramref name="type" /> should implement.</param>
4245
/// <exception cref="ArgumentNullException">Thrown when <paramref name="type" /> or <paramref name="interfaceType" /> is null.</exception>
4346
[ContractAnnotation("type:null => halt; interfaceType:null => halt")]
44-
public static bool Implements([ValidatedNotNull] this Type type, [ValidatedNotNull] Type interfaceType)
47+
public static bool Implements(
48+
#if NET8_0
49+
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)]
50+
#endif
51+
[ValidatedNotNull]
52+
this Type type,
53+
[ValidatedNotNull] Type interfaceType
54+
)
4555
{
46-
interfaceType.MustNotBeNull(nameof(interfaceType));
47-
var implementedInterfaces = type.MustNotBeNull(nameof(type)).GetInterfaces();
56+
type.MustNotBeNull();
57+
interfaceType.MustNotBeNull();
4858

59+
var implementedInterfaces = type.GetInterfaces();
4960
for (var i = 0; i < implementedInterfaces.Length; ++i)
5061
{
5162
if (interfaceType.IsEquivalentTypeTo(implementedInterfaces[i]))
@@ -64,12 +75,21 @@ public static bool Implements([ValidatedNotNull] this Type type, [ValidatedNotNu
6475
/// <param name="typeComparer">The equality comparer used to compare the interface types.</param>
6576
/// <exception cref="ArgumentNullException">Thrown when <paramref name="type" />, or <paramref name="interfaceType" />, or <paramref name="typeComparer" /> is null.</exception>
6677
[ContractAnnotation("type:null => halt; interfaceType:null => halt; typeComparer:null => halt")]
67-
public static bool Implements([ValidatedNotNull] this Type type, [ValidatedNotNull] Type interfaceType, [ValidatedNotNull] IEqualityComparer<Type> typeComparer)
78+
public static bool Implements(
79+
#if NET8_0
80+
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)]
81+
#endif
82+
[ValidatedNotNull]
83+
this Type type,
84+
[ValidatedNotNull] Type interfaceType,
85+
[ValidatedNotNull] IEqualityComparer<Type> typeComparer
86+
)
6887
{
69-
interfaceType.MustNotBeNull(nameof(interfaceType));
70-
typeComparer.MustNotBeNull(nameof(typeComparer));
88+
type.MustNotBeNull();
89+
interfaceType.MustNotBeNull();
90+
typeComparer.MustNotBeNull();
7191

72-
var implementedInterfaces = type.MustNotBeNull(nameof(type)).GetInterfaces();
92+
var implementedInterfaces = type.GetInterfaces();
7393
for (var i = 0; i < implementedInterfaces.Length; ++i)
7494
{
7595
if (typeComparer.Equals(implementedInterfaces[i], interfaceType))
@@ -88,7 +108,12 @@ public static bool Implements([ValidatedNotNull] this Type type, [ValidatedNotNu
88108
/// <exception cref="ArgumentNullException">Thrown when <paramref name="type" /> or <paramref name="otherType" /> is null.</exception>
89109
[MethodImpl(MethodImplOptions.AggressiveInlining)]
90110
[ContractAnnotation("type:null => halt; otherType:null => halt")]
91-
public static bool IsOrImplements([ValidatedNotNull] this Type type, [ValidatedNotNull] Type otherType) =>
111+
public static bool IsOrImplements(
112+
#if NET8_0
113+
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)]
114+
#endif
115+
[ValidatedNotNull] this Type type,
116+
[ValidatedNotNull] Type otherType) =>
92117
type.IsEquivalentTypeTo(otherType.MustNotBeNull(nameof(otherType))) || type.Implements(otherType);
93118

94119
/// <summary>
@@ -102,7 +127,13 @@ public static bool IsOrImplements([ValidatedNotNull] this Type type, [ValidatedN
102127
/// <exception cref="ArgumentNullException">Thrown when <paramref name="type" /> or <paramref name="otherType" /> is null.</exception>
103128
[MethodImpl(MethodImplOptions.AggressiveInlining)]
104129
[ContractAnnotation("type:null => halt; otherType:null => halt")]
105-
public static bool IsOrImplements([ValidatedNotNull] this Type type, [ValidatedNotNull] Type otherType, [ValidatedNotNull] IEqualityComparer<Type> typeComparer) =>
130+
public static bool IsOrImplements(
131+
#if NET8_0
132+
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)]
133+
#endif
134+
[ValidatedNotNull] this Type type,
135+
[ValidatedNotNull] Type otherType,
136+
[ValidatedNotNull] IEqualityComparer<Type> typeComparer) =>
106137
typeComparer.MustNotBeNull(nameof(typeComparer)).Equals(type.MustNotBeNull(nameof(type)), otherType.MustNotBeNull(nameof(otherType))) || type.Implements(otherType, typeComparer);
107138

108139
/// <summary>
@@ -190,11 +221,16 @@ public static bool IsOrDerivesFrom([ValidatedNotNull] this Type type, [Validated
190221
/// <exception cref="ArgumentNullException">Thrown when <paramref name="type" /> or <paramref name="baseClassOrInterfaceType" /> is null.</exception>
191222
[MethodImpl(MethodImplOptions.AggressiveInlining)]
192223
[ContractAnnotation("type:null => halt; baseClassOrInterfaceType:null => halt")]
193-
public static bool InheritsFrom([ValidatedNotNull] this Type type, [ValidatedNotNull] Type baseClassOrInterfaceType) =>
224+
public static bool InheritsFrom(
225+
#if NET8_0
226+
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)]
227+
#endif
228+
[ValidatedNotNull] this Type type,
229+
[ValidatedNotNull] Type baseClassOrInterfaceType) =>
194230
baseClassOrInterfaceType.MustNotBeNull(nameof(baseClassOrInterfaceType))
195-
.IsInterface
196-
? type.Implements(baseClassOrInterfaceType)
197-
: type.DerivesFrom(baseClassOrInterfaceType);
231+
.IsInterface ?
232+
type.Implements(baseClassOrInterfaceType) :
233+
type.DerivesFrom(baseClassOrInterfaceType);
198234

199235
/// <summary>
200236
/// Checks if the given type derives from the specified base class or interface type. This overload uses the specified <paramref name="typeComparer" />
@@ -206,11 +242,17 @@ public static bool InheritsFrom([ValidatedNotNull] this Type type, [ValidatedNot
206242
/// <exception cref="ArgumentNullException">Thrown when <paramref name="type" />, or <paramref name="baseClassOrInterfaceType" />, or <paramref name="typeComparer" /> is null.</exception>
207243
[MethodImpl(MethodImplOptions.AggressiveInlining)]
208244
[ContractAnnotation("type:null => halt; baseClassOrInterfaceType:null => halt; typeComparer:null => halt")]
209-
public static bool InheritsFrom([ValidatedNotNull] this Type type, [ValidatedNotNull] Type baseClassOrInterfaceType, [ValidatedNotNull] IEqualityComparer<Type> typeComparer) =>
245+
public static bool InheritsFrom(
246+
#if NET8_0
247+
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)]
248+
#endif
249+
[ValidatedNotNull] this Type type,
250+
[ValidatedNotNull] Type baseClassOrInterfaceType,
251+
[ValidatedNotNull] IEqualityComparer<Type> typeComparer) =>
210252
baseClassOrInterfaceType.MustNotBeNull(nameof(baseClassOrInterfaceType))
211-
.IsInterface
212-
? type.Implements(baseClassOrInterfaceType, typeComparer)
213-
: type.DerivesFrom(baseClassOrInterfaceType, typeComparer);
253+
.IsInterface ?
254+
type.Implements(baseClassOrInterfaceType, typeComparer) :
255+
type.DerivesFrom(baseClassOrInterfaceType, typeComparer);
214256

215257
/// <summary>
216258
/// Checks if the given <paramref name="type" /> is equal to the specified <paramref name="otherType" /> or if it derives from it or implements it.
@@ -222,7 +264,12 @@ public static bool InheritsFrom([ValidatedNotNull] this Type type, [ValidatedNot
222264
/// <exception cref="ArgumentNullException">Thrown when <paramref name="type" /> or <paramref name="otherType" /> is null.</exception>
223265
[MethodImpl(MethodImplOptions.AggressiveInlining)]
224266
[ContractAnnotation("type:null => halt; otherType:null => halt")]
225-
public static bool IsOrInheritsFrom([ValidatedNotNull] this Type type, [ValidatedNotNull] Type otherType) =>
267+
public static bool IsOrInheritsFrom(
268+
#if NET8_0
269+
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)]
270+
#endif
271+
[ValidatedNotNull] this Type type,
272+
[ValidatedNotNull] Type otherType) =>
226273
type.IsEquivalentTypeTo(otherType.MustNotBeNull(nameof(otherType))) || type.InheritsFrom(otherType);
227274

228275

@@ -236,7 +283,13 @@ public static bool IsOrInheritsFrom([ValidatedNotNull] this Type type, [Validate
236283
/// <exception cref="ArgumentNullException">Thrown when <paramref name="type" /> or <paramref name="otherType" /> is null.</exception>
237284
[MethodImpl(MethodImplOptions.AggressiveInlining)]
238285
[ContractAnnotation("type:null => halt; otherType:null => halt; typeComparer:null => halt")]
239-
public static bool IsOrInheritsFrom([ValidatedNotNull] this Type type, [ValidatedNotNull] Type otherType, [ValidatedNotNull] IEqualityComparer<Type> typeComparer) =>
286+
public static bool IsOrInheritsFrom(
287+
#if NET8_0
288+
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)]
289+
#endif
290+
[ValidatedNotNull] this Type type,
291+
[ValidatedNotNull] Type otherType,
292+
[ValidatedNotNull] IEqualityComparer<Type> typeComparer) =>
240293
typeComparer.MustNotBeNull(nameof(typeComparer)).Equals(type, otherType.MustNotBeNull(nameof(otherType))) || type.InheritsFrom(otherType, typeComparer);
241294

242295

0 commit comments

Comments
 (0)