At the moment i am using this code in a class i call "Ensure", it is essentially a shortcut class of static methods i use to make throwing exceptions easier, so i am not constantly having to write out a minimum of 3 lines to do an exception, it can all 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 issue is, currently when calling Ensure.ArgumentNotNull
, i either have to do:
Ensure.ArgumentNotNull(arg, "arg");
or
Ensure.ArgumentNotNull(() => arg);
As i need the name to be able to explain which argument caused the exception in the exception its self.
Is there a way of being able to call ArgumentNotNull
without needing the () =>
part of the lambda and simply call Ensure.ArgumentNotNull(arg)
and still be able to get the name of the argument that was passed, without having to specifically pass the name as well.