I have the following extension method:
public static void ThrowIfArgumentIsNull<T>(this T value, string argument)
where T : class
{
if (value == null)
{
throw new ArgumentNullException(argument);
}
}
and this is an example of its usage....
// Note: I've poorly named the argument, on purpose, for this question.
public void Save(Category qwerty)
{
qwerty.ThrowIfArgumentIsNull("qwerty");
....
}
works 100% fine.
But, I don't like how I have to provide the name of the variable, just to help my exception message.
I was wondering if it's possible to refactor the extension method, so it could be called like this...
qwerty.ThrowIfArgumentIsNull();
and it automatically figures out that the name of the variable is 'qwerty' and therefore uses that as the value for the ArgumentNullException.
Possible? I'm assuming reflection could do this?