Added FisheryLib.Collections.FishTable and Mono.Cecil extension methods

A bit of refactoring has been done too
This commit is contained in:
bbradson
2023-06-03 20:10:58 +02:00
parent 8fa92f15a6
commit cdf209eb02
72 changed files with 8145 additions and 2098 deletions
+63 -16
View File
@@ -3,8 +3,9 @@
// See the LICENSE file in the project root for more information.
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using JetBrains.Annotations;
using NotNullAttribute = System.Diagnostics.CodeAnalysis.NotNullAttribute;
namespace FisheryLib.Utility.Diagnostics;
@@ -12,6 +13,7 @@ namespace FisheryLib.Utility.Diagnostics;
/// Helper methods to verify conditions when running code.
/// </summary>
[DebuggerStepThrough]
[PublicAPI]
public static partial class Guard
{
/// <summary>
@@ -22,7 +24,7 @@ public static partial class Guard
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentException">Thrown if <paramref name="value"/> is not <see langword="null"/>.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsNull<T>(T? value, [CallerArgumentExpression("value")] string name = "")
public static void IsNull<T>([NoEnumeration] T? value, [CallerArgumentExpression("value")] string name = "")
{
if (value is null)
return;
@@ -38,7 +40,7 @@ public static partial class Guard
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentException">Thrown if <paramref name="value"/> is not <see langword="null"/>.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsNull<T>(T? value, [CallerArgumentExpression("value")] string name = "")
public static void IsNull<T>([NoEnumeration] T? value, [CallerArgumentExpression("value")] string name = "")
where T : struct
{
if (value is null)
@@ -55,7 +57,8 @@ public static partial class Guard
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="value"/> is <see langword="null"/>.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsNotNull<T>([NotNull] T? value, [CallerArgumentExpression("value")] string name = "")
public static void IsNotNull<T>([NoEnumeration] [NotNull] T? value,
[CallerArgumentExpression("value")] string name = "")
{
if (value is not null)
return;
@@ -71,7 +74,8 @@ public static partial class Guard
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="value"/> is <see langword="null"/>.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsNotNull<T>([NotNull] T? value, [CallerArgumentExpression("value")] string name = "")
public static void IsNotNull<T>([NoEnumeration] [NotNull] T? value,
[CallerArgumentExpression("value")] string name = "")
where T : struct
{
if (value is not null)
@@ -88,7 +92,7 @@ public static partial class Guard
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentException">Thrown if <paramref name="value"/> is not of type <typeparamref name="T"/>.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsOfType<T>(object value, [CallerArgumentExpression("value")] string name = "")
public static void IsOfType<T>([NoEnumeration] object value, [CallerArgumentExpression("value")] string name = "")
{
if (value.GetType() == typeof(T))
return;
@@ -104,7 +108,8 @@ public static partial class Guard
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentException">Thrown if <paramref name="value"/> is of type <typeparamref name="T"/>.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsNotOfType<T>(object value, [CallerArgumentExpression("value")] string name = "")
public static void IsNotOfType<T>([NoEnumeration] object value,
[CallerArgumentExpression("value")] string name = "")
{
if (value.GetType() != typeof(T))
return;
@@ -120,7 +125,8 @@ public static partial class Guard
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentException">Thrown if the type of <paramref name="value"/> is not the same as <paramref name="type"/>.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsOfType(object value, Type type, [CallerArgumentExpression("value")] string name = "")
public static void IsOfType([NoEnumeration] object value, Type type,
[CallerArgumentExpression("value")] string name = "")
{
if (value.GetType() == type)
return;
@@ -136,7 +142,8 @@ public static partial class Guard
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentException">Thrown if the type of <paramref name="value"/> is the same as <paramref name="type"/>.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsNotOfType(object value, Type type, [CallerArgumentExpression("value")] string name = "")
public static void IsNotOfType([NoEnumeration] object value, Type type,
[CallerArgumentExpression("value")] string name = "")
{
if (value.GetType() != type)
return;
@@ -152,7 +159,8 @@ public static partial class Guard
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentException">Thrown if <paramref name="value"/> can't be assigned to type <typeparamref name="T"/>.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsAssignableToType<T>(object value, [CallerArgumentExpression("value")] string name = "")
public static void IsAssignableToType<T>([NoEnumeration] object value,
[CallerArgumentExpression("value")] string name = "")
{
if (value is T)
return;
@@ -168,7 +176,8 @@ public static partial class Guard
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentException">Thrown if <paramref name="value"/> can be assigned to type <typeparamref name="T"/>.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsNotAssignableToType<T>(object value, [CallerArgumentExpression("value")] string name = "")
public static void IsNotAssignableToType<T>([NoEnumeration] object value,
[CallerArgumentExpression("value")] string name = "")
{
if (value is not T)
return;
@@ -184,7 +193,8 @@ public static partial class Guard
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentException">Thrown if <paramref name="value"/> can't be assigned to <paramref name="type"/>.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsAssignableToType(object value, Type type, [CallerArgumentExpression("value")] string name = "")
public static void IsAssignableToType([NoEnumeration] object value, Type type,
[CallerArgumentExpression("value")] string name = "")
{
if (type.IsInstanceOfType(value))
return;
@@ -200,13 +210,48 @@ public static partial class Guard
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentException">Thrown if <paramref name="value"/> can be assigned to <paramref name="type"/>.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsNotAssignableToType(object value, Type type, [CallerArgumentExpression("value")] string name = "")
public static void IsNotAssignableToType([NoEnumeration] object value, Type type,
[CallerArgumentExpression("value")] string name = "")
{
if (!type.IsInstanceOfType(value))
return;
ThrowHelper.ThrowArgumentExceptionForIsNotAssignableToType(value, type, name);
}
/// <summary>
/// Asserts that the input type can be assigned to a specified type.
/// </summary>
/// <param name="value">The input <see cref="Type"/> to test.</param>
/// <param name="type">The type to look for.</param>
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentException">Thrown if <paramref name="value"/> can't be assigned to <paramref name="type"/>.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsTypeAssignableToType([NoEnumeration] Type value, Type type,
[CallerArgumentExpression("value")] string name = "")
{
if (type.IsAssignableFrom(value))
return;
ThrowHelper.ThrowArgumentExceptionForIsAssignableToType(value, type, name);
}
/// <summary>
/// Asserts that the input type can't be assigned to a specified type.
/// </summary>
/// <param name="value">The input <see cref="Type"/> to test.</param>
/// <param name="type">The type to look for.</param>
/// <param name="name">The name of the input parameter being tested.</param>
/// <exception cref="ArgumentException">Thrown if <paramref name="value"/> can be assigned to <paramref name="type"/>.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsTypeNotAssignableToType([NoEnumeration] Type value, Type type,
[CallerArgumentExpression("value")] string name = "")
{
if (!type.IsAssignableFrom(value))
return;
ThrowHelper.ThrowArgumentExceptionForIsNotAssignableToType(value, type, name);
}
/// <summary>
/// Asserts that the input value must be the same instance as the target value.
@@ -218,7 +263,8 @@ public static partial class Guard
/// <exception cref="ArgumentException">Thrown if <paramref name="value"/> is not the same instance as <paramref name="target"/>.</exception>
/// <remarks>The method is generic to prevent using it with value types.</remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsReferenceEqualTo<T>(T value, T target, [CallerArgumentExpression("value")] string name = "")
public static void IsReferenceEqualTo<T>([NoEnumeration] T value, T target,
[CallerArgumentExpression("value")] string name = "")
where T : class
{
if (ReferenceEquals(value, target))
@@ -237,7 +283,8 @@ public static partial class Guard
/// <exception cref="ArgumentException">Thrown if <paramref name="value"/> is the same instance as <paramref name="target"/>.</exception>
/// <remarks>The method is generic to prevent using it with value types.</remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsReferenceNotEqualTo<T>(T value, T target, [CallerArgumentExpression("value")] string name = "")
public static void IsReferenceNotEqualTo<T>([NoEnumeration] T value, T target,
[CallerArgumentExpression("value")] string name = "")
where T : class
{
if (!ReferenceEquals(value, target))
@@ -245,4 +292,4 @@ public static partial class Guard
ThrowHelper.ThrowArgumentExceptionForIsReferenceNotEqualTo<T>(name);
}
}
}