This repository has been archived by the owner on Dec 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Argument.cs
144 lines (128 loc) · 5.97 KB
/
Argument.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
namespace RSG.Utils
{
public class Argument
{
[Conditional("DEBUG")]
public static void Invariant<T>(Expression<Func<T>> parameter, Func<bool> condition)
{
var memberExpression = parameter.Body as MemberExpression;
if (memberExpression == null)
{
throw new ApplicationException("Can only use a member expression with Argument.NotNull.");
}
if (!condition())
{
var parameterName = memberExpression.Member.Name;
var stackTrace = new StackTrace(true);
var stackFrames = stackTrace.GetFrames();
throw new ArgumentException(parameterName, memberExpression.Type.Name + " parameter failed invariant condition, Function: " + stackFrames[1].GetMethod().Name);
}
}
// Todo: google debug break C#, only if running in debugger
[Conditional("DEBUG")]
public static void NotNull<T>(Expression<Func<T>> parameter)
{
var memberExpression = parameter.Body as MemberExpression;
if (memberExpression == null)
{
throw new ApplicationException("Can only use a member expression with Argument.NotNull.");
}
var value = parameter.Compile().Invoke();
if (value == null)
{
var parameterName = memberExpression.Member.Name;
var stackTrace = new StackTrace(true);
var stackFrames = stackTrace.GetFrames();
var msg = "Parameter type: " + memberExpression.Type.Name + ", Function: " + stackFrames[1].GetMethod().Name;
//if (Debugger.IsAttached)
//{
// Debugger.Break();
//}
throw new ArgumentNullException(parameterName, msg);
}
}
[Conditional("DEBUG")]
public static void StringNotNullOrEmpty(Expression<Func<Object>> parameter)
{
var memberExpression = parameter.Body as MemberExpression;
if (memberExpression == null)
{
throw new ApplicationException("Can only use a member expression with Argument.StringNotNullOrEmpty.");
}
if (memberExpression.Type != typeof(String))
{
throw new ApplicationException("StringNotNullOrEmpty can only be used with string arguments, type was " + memberExpression.Type.Name);
}
var value = parameter.Compile().Invoke() as string;
if (value == null)
{
var parameterName = memberExpression.Member.Name;
var stackTrace = new StackTrace(true);
var stackFrames = stackTrace.GetFrames();
throw new ArgumentNullException(parameterName, "Parameter type: " + memberExpression.Type.Name + ", Function: " + stackFrames[1].GetMethod().Name);
}
if (value == string.Empty)
{
var parameterName = memberExpression.Member.Name;
var stackTrace = new StackTrace(true);
var stackFrames = stackTrace.GetFrames();
throw new ArgumentException("Empty string parameter. Function: " + stackFrames[1].GetMethod().Name, parameterName);
}
}
[Conditional("DEBUG")]
public static void ArrayIndex(Expression<Func<int>> parameter)
{
var memberExpression = parameter.Body as MemberExpression;
if (memberExpression == null)
{
throw new ApplicationException("Can only use a member expression with Argument.ArrayIndex.");
}
if (memberExpression.Type != typeof(int))
{
throw new ApplicationException("ArrayIndex can only be used with int arguments, type was " + memberExpression.Type.Name);
}
var value = parameter.Compile().Invoke();
if (value < 0)
{
var parameterName = memberExpression.Member.Name;
var stackTrace = new StackTrace(true);
var stackFrames = stackTrace.GetFrames();
throw new ArgumentException("Negative array index is invalid. Function: " + stackFrames[1].GetMethod().Name, parameterName);
}
}
[Conditional("DEBUG")]
public static void ArrayIndex(Expression<Func<int>> parameter, int maxArrayElements)
{
var memberExpression = parameter.Body as MemberExpression;
if (memberExpression == null)
{
throw new ApplicationException("Can only use a member expression with Argument.ArrayIndex.");
}
if (memberExpression.Type != typeof(int))
{
throw new ApplicationException("ArrayIndex can only be used with int arguments, type was " + memberExpression.Type.Name);
}
var value = parameter.Compile().Invoke();
if (value < 0)
{
var parameterName = memberExpression.Member.Name;
var stackTrace = new StackTrace(true);
var stackFrames = stackTrace.GetFrames();
throw new ArgumentException("Negative array index is invalid. Index: " + value + ", Function: " + stackFrames[1].GetMethod().Name, parameterName);
}
else if (value >= maxArrayElements)
{
var parameterName = memberExpression.Member.Name;
var stackTrace = new StackTrace(true);
var stackFrames = stackTrace.GetFrames();
throw new ArgumentException("Array access out of bounds index is invalid. Max should be: " + maxArrayElements + ", Index is: " + value + ", Function: " + stackFrames[1].GetMethod().Name, parameterName);
}
}
}
}