私はテスト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);
}
、私はGetPropertyPathFromFunc
の体が必要です。
質問どのように私は、入力としてo => City.Name
のようなラムダを取得し、結果として"City.Name"
のような文字列を返すメソッドを書くことができます。
http://stackoverflow.com/questions/8215449/c-sharp-converting-lambda-expression-function-to-descriptive-string? – tire0011