I am currently using this code in class i, which is called "Provide", which is essentially the class of static method labels that I use to make throw exceptions easier, so I don’t have to write at least 3 to make an exception all the time, all this can always be done on 1 line.
[DebuggerHidden, DebuggerStepThrough]
public static void ArgumentNotNull(object argument, string name)
{
if (argument == null)
{
throw new ArgumentNullException(name, "Cannot be null");
}
}
[DebuggerHidden, DebuggerStepThrough]
public static void ArgumentNotNull<T>(Expression<Func<T>> expr)
{
var e = (MemberExpression)expr.Body;
var val = GetValue<T>(e);
ArgumentNotNull(val, e.Member.Name);
}
My problem is that when Ensure.ArgumentNotNullI call, I must either do:
Ensure.ArgumentNotNull(arg, "arg");
or
Ensure.ArgumentNotNull(() => arg);
Since I need a name in order to be able to explain which argument caused the exception in the exception, its me.
Is there a way to make a call ArgumentNotNullwithout needing a () =>lambda part , and just call Ensure.ArgumentNotNull(arg)and still be able to get the name of the passed argument, without having to pass the name as well.