The difference between ArgumentNullException
and NullReferenceException
is that ArgumentNullException
is always thrown explicitly like so:
if (parameter == null)
throw new ArgumentNullException("parameter");
Had a quick look at ILDASM output, the local variables are indeed present inside a function's IL. There is still, however, no API to retrieve those names programatically. My understanding is that it would be fairly complex as you would basically need to build a parse tree that represents a function with scopes, variables, statements etc.
It's further complicated by the fact that it's not just simple variables that can throw NullReferenceException
, but result of a function call, a property or an expression. I could get pretty complicated pretty fast.
Imagine this:
internalObject.OtherProperty = myObject.GetOtherObject().ThirdObject.SomeProperty == "value"
? myObject.OtherProperty
: myObject.GetSomethingElse();
There are multiple points of failure there and building up a string representing what is actually null
could be tricky.