The simplest way for me to describe this is in the Unit Testing context.
I have a method that accepts a parameter and asserts that parameter isn't null.
public void DoStuff(string requirementName)
{
if (requirementName == null) throw new ArgumentNullException(nameof(requirementName));
}
Then I have a test that asserts this exception is thrown.
[Test]
public void NullRequirementName_ThrowsArgNullException()
{
var sub = new DoStuffClass();
Action doStuff = () => sub.DoStuff(null);
doStuff.Should().Throw<ArgumentNullException>();
}
What I want is to assert that the message of the exception contains the name of the parameter, and yet allow the parameter name in the signature of DoStuff
to change and give compile time indication that the name is changed.
[Test]
public void NullRequirementName_ThrowsArgNullException()
{
var sub = new DoStuffClass();
Action doStuff = () => sub.DoStuff(null);
doStuff.Should().Throw<ArgumentNullException>().Where(ex => ex.Message.Contains("requirementName");
}
The above assertion works fine, but doesn't allow the param name to change without breaking the test at run-time. I'm looking for a simple looking syntax that effectively gives me nameof(DoStuffClass.DoStuff.requirementName)