-
-
Notifications
You must be signed in to change notification settings - Fork 833
/
Enforce.cs
100 lines (87 loc) · 3.3 KB
/
Enforce.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright (c) Autofac Project. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
using System.Globalization;
using System.Reflection;
namespace Autofac.Util;
/// <summary>
/// Helper methods used throughout the codebase.
/// </summary>
internal static class Enforce
{
/// <summary>
/// Enforce that sequence does not contain null. Returns the
/// value if valid so that it can be used inline in
/// base initializer syntax.
/// </summary>
/// <param name="value">The value.</param>
/// <param name="name">The parameter name.</param>
[SuppressMessage("ReSharper", "PossibleMultipleEnumeration")]
public static IEnumerable<T> ArgumentElementNotNull<T>(IEnumerable<T> value, string name)
where T : class
{
if (value == null)
{
throw new ArgumentNullException(name);
}
if (value.Any(e => e == null))
{
throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, EnforceResources.ElementCannotBeNull, name));
}
return value;
}
/// <summary>
/// Enforces that the provided object is non-null.
/// </summary>
/// <typeparam name="T">The type of value being checked.</typeparam>
/// <param name="value">The value.</param>
/// <returns><paramref name="value"/> if not null.</returns>
public static T NotNull<T>([ValidatedNotNull] T value)
where T : class
{
if (value == null)
{
throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, EnforceResources.CannotBeNull, typeof(T).FullName));
}
return value;
}
/// <summary>
/// Enforce that an argument is not null or empty. Returns the
/// value if valid so that it can be used inline in
/// base initializer syntax.
/// </summary>
/// <param name="value">The value.</param>
/// <param name="description">The description.</param>
/// <returns><paramref name="value"/>if not null or empty.</returns>
public static string ArgumentNotNullOrEmpty([ValidatedNotNull] string value, string description)
{
if (description == null)
{
throw new ArgumentNullException(nameof(description));
}
if (value == null)
{
throw new ArgumentNullException(description);
}
if (value.Length == 0)
{
throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, EnforceResources.CannotBeEmpty, description));
}
return value;
}
/// <summary>
/// Enforce that the argument is a delegate type.
/// </summary>
/// <param name="delegateType">The type to test.</param>
public static void ArgumentTypeIsFunction(Type delegateType)
{
if (delegateType == null)
{
throw new ArgumentNullException(nameof(delegateType));
}
MethodInfo invoke = delegateType.GetDeclaredMethod("Invoke") ?? throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, EnforceResources.NotDelegate, delegateType));
if (invoke.ReturnType == typeof(void))
{
throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, EnforceResources.DelegateReturnsVoid, delegateType));
}
}
}