테스트 API를 설계하고 있습니다. 내가 좋아하는 API를 갖고 싶어 :동적 매개 변수를 입력으로 사용하는 람다에서 속성 이름 가져 오기
// There is a dynamic object which should be tested to have certain properties.
dynamic result = SomeMethod();
AssertPropertyIsNotNull(resut, o => o.Title);
AssertPropertyIsNotNull(resut, o => o.City.Name);
내가 속성을 주장 TestProperty
방법을 쓰고 싶어을하고 적절한 메시지를 표시 그것은처럼 실패 :이 예에서는 다음
private void AssertPropertyIsNotNull(dynamic result, Func<dynamic, object> propertySelector)
{
var propertyPath = GetPropertyPathFromFunc(propertySelector);
var errorMessage = $"{propertyPath} is not filled properly."
Assert.IsNotNull(propertySelector(result), errorMessage);
}
, I 시체가 GetPropertyPathFromFunc
에 필요합니다.
질문 어떻게 입력으로 o => City.Name
같은 람다를 얻고 결과 "City.Name"
같은 문자열을 반환하는 방법을 쓸 수 있습니다.
http://stackoverflow.com/questions/8215449/c-sharp-converting-lambda-expression-function-to-descriptive-string? – tire0011