|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
namespace TinyLisp {
// Adapter to ease binding to WPF. Main goal was to create an IEnumerable of childnodes
public class ExpressionAdapter {
// private fields.
private Expression _Expr;
private string _ParentPropertyName;
// constructor
public ExpressionAdapter(Expression Expr, string ParentPropertyName) {
_Expr = Expr;
_ParentPropertyName = ParentPropertyName;
}
// Returns string with information about the current Property - Expression pair
public string Text {
get {
if (_Expr is ConstantExpression) {
ConstantExpression ce = _Expr as ConstantExpression;
return _ParentPropertyName + " = " + (_Expr == null ? "null" : (_Expr.NodeType.ToString() + " " + ce.Value));
} else {
return _ParentPropertyName + " = " + (_Expr == null ? "null" : (_Expr.NodeType.ToString()));
}
}
}
// Returns all properties of type Expression as an IEnumerable<ExpressionAdapter>
public IEnumerable<ExpressionAdapter> Children {
get {
if (_Expr == null) return null;
else
return from childExpression in _Expr.GetType().GetProperties().OrderBy(p=>p.Name)
where childExpression.PropertyType == typeof(Expression)
select new ExpressionAdapter((Expression)childExpression.GetValue(_Expr, null), childExpression.Name);
}
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Gert-Jan is a Senior Quantitative Risk Manager at Leaseplan Corporation. In that job he doesn't get to code much he does these little projects to keep his skills up and feed the inner geek.