C#では、式ツリーを使用してオブジェクトグラフの文字列表現をかなり簡単に作成できました。
public static string GetGraph<TModel, T>(TModel model, Expression<Func<TModel, T>> action) where TModel : class
{
var method = action.Body as MethodCallExpression;
var body = method != null ? method.Object != null ? method.Object as MemberExpression : method.Arguments.Any() ? method.Arguments.First() as MemberExpression : null : action.Body as MemberExpression;
if (body != null)
{
string graph = GetObjectGraph(body, typeof(TModel))
return graph;
}
throw new Exception("Could not create object graph");
}
F#では、同じことをしようとする見積もりを調べていましたが、それを完全に理解することはできません。PowerPackライブラリを使用して引用を式に変換しようとしましたが、これまでのところうまくいきませんでした。インターネット上の情報は、このトピックに関してかなりまばらに見えます。
入力が次の場合:
let result = getGraph myObject <@ myObject.MyProperty @>
出力は「myobject.MyProperty」である必要があります