I have an Expression which looks a bit like this:
Expression<Func<TEntity, TRelated>> x = (entity) => entity.SomeRelatedObject;
I also have a class DomainObject<TEntity> which is a IDynamicMetaObjectProvider which would return a DomainObject<TRelated> when the same property is accessed through the dynamic keyword.
It basically gets the RelatedObject from the TEntity and wraps it in a DomainObject.
var x = ((dynamic)myDomainObject).SomeRelatedObject; // this gives me back DomainObject<TRelated> object
I would like to have a method within my DomainObject<TEntity> class which takes in the Expression above and gets the related object as if the call is made using the dynamic keyword.
public DomainObject<TRelated> GetRelatedObject<TRelated>(Expression<Func<T, TRelated>> expression)
var lambda = (LambdaExpression)new SomeVisitor<T, TRelated>().Visit(expression);
I assume this can be achieved by overriding the VisitMember and substituting the MemberExpression with a DynamicExpression but I couldn't find any examples of how to use Expression.Dynamic or Expression.MakeDynamic.
My questions are:
1. Does the above make sense?
2. Can anyone please show me an example of how to construct a DynamicExpression for the above?
Any help would be greatly appreciated.