Is there a way to pass a property around as a parameter without reflection?
My current motivation for this is the code below is testing multiple
properties to see that an exception is thrown if a user tries to set the
string value to an empty value. Ideally I'd like to have one method that
would take a Property as a parameter and test it:
[Test]
public void CompanyName_Err or_CannotSetToE mpty() {
var customer = new Customer { ID = "TEST" };
try {
customer.Compan yName = string.Empty;
Assert.Fail("Em ptyValueExcepti on for CompanyName property should
have been thrown.");
}
catch (EmptyValueExce ption ex) {
_assertProperty NameIsSetProper ly(ex, "CompanyNam e");
}
}
[Test]
public void PhoneNumber_Err or_CannotSetToE mpty() {
var customer = new Customer { ID = "TEST" };
try {
customer.Phone = string.Empty;
Assert.Fail("Em ptyValueExcepti on for Phone property should have
been thrown.");
}
catch (EmptyValueExce ption ex) {
_assertProperty NameIsSetProper ly(ex, "Phone");
}
}
private static void _assertProperty NameIsSetProper ly(EmptyValueEx ception
ex, string expectedName) {
Assert.That(ex. PropertyName, Is.EqualTo(expe ctedName));
}