How do I translate an expression tree of one type to a different expression type?

Here's a visitor that does the job it makes a copy of the parameter (since we'll need to create a new parameter and substitute all references of the old parameter for the new one) it walks the Body of the tree, substituting the parameter, and switching any member-access against the old type to a like-named member on the new type it re-assembles a lambda using the parameter we invented earler Code: class TypeChangeVisitor : ExpressionVisitor { private readonly Type from, to; private readonly Dictionary substitutions; public TypeChangeVisitor(Type from, Type to, Dictionary substitutions) { this. From = from; this. To = to; this.

Substitutions = substitutions; } public override Expression Visit(Expression node) { // general substitutions (for example, parameter swaps) Expression found; if(substitutions! = null && substitutions. TryGetValue(node, out found)) { return found; } return base.

Visit(node); } protected override Expression VisitMember(MemberExpression node) { // if we see x. Name on the old type, substitute for new type if (node.Member. DeclaringType == from) { return Expression.

MakeMemberAccess(Visit(node. Expression), to. GetMember(node.Member.Name, node.Member.

MemberType, BindingFlags. Instance | BindingFlags. Static | BindingFlags.

Public | BindingFlags. NonPublic).Single()); } return base. VisitMember(node); } } public class Program { public static void Main() { Expression> predicate = x => x.

Name == "abc"; var switched = Translate(predicate); } public static Expression> Translate(Expression> expression) { var param = Expression. Parameter(typeof(TTo), expression. Parameters0.

Name); var subst = new Dictionary { { expression. Parameters0, param } }; var visitor = new TypeChangeVisitor(typeof(TFrom), typeof(TTo), subst); return Expression. Lambda>(visitor.

Visit(expression. Body), param); } } Note that if you have x.Something.Name you might need to be a bit more careful, but this should get you a reasonable way.

Here's a visitor that does the job. It makes a copy of the parameter (since we'll need to create a new parameter and substitute all references of the old parameter for the new one) it walks the . Body of the tree, substituting the parameter, and switching any member-access against the old type to a like-named member on the new type it re-assembles a lambda using the parameter we invented earler Code: class TypeChangeVisitor : ExpressionVisitor { private readonly Type from, to; private readonly Dictionary substitutions; public TypeChangeVisitor(Type from, Type to, Dictionary substitutions) { this.

From = from; this. To = to; this. Substitutions = substitutions; } public override Expression Visit(Expression node) { // general substitutions (for example, parameter swaps) Expression found; if(substitutions!

= null && substitutions. TryGetValue(node, out found)) { return found; } return base. Visit(node); } protected override Expression VisitMember(MemberExpression node) { // if we see x.Name on the old type, substitute for new type if (node.Member.

DeclaringType == from) { return Expression. MakeMemberAccess(Visit(node. Expression), to.

GetMember(node.Member. Name, node.Member. MemberType, BindingFlags.

Instance | BindingFlags. Static | BindingFlags. Public | BindingFlags.

NonPublic).Single()); } return base. VisitMember(node); } } public class Program { public static void Main() { Expression> predicate = x => x.Name == "abc"; var switched = Translate(predicate); } public static Expression> Translate(Expression> expression) { var param = Expression. Parameter(typeof(TTo), expression.

Parameters0.Name); var subst = new Dictionary { { expression. Parameters0, param } }; var visitor = new TypeChangeVisitor(typeof(TFrom), typeof(TTo), subst); return Expression. Lambda>(visitor.

Visit(expression. Body), param); } } Note that if you have x.Something. Name you might need to be a bit more careful, but this should get you a reasonable way.

Jul 15 at 22:38 Ain't help though – Agzam Jul 15 at 23:02 check for yourself - if you have Int Id in Animal and also AnimalViewModel derived from BaseViewModel which holds Id. And if you try to use Id in predicate wouldn't work. Don't know why – Agzam Jul 15 at 23:22.

TranslateFrom:Detect language—AfrikaansAlbanianArabicArmenianAzerbaijaniBasqueBelarusianBengaliBulgarianCatalanChineseCroatianCzechDanishDutchEnglishEsperantoEstonianFilipinoFinnishFrenchGalicianGeorgianGermanGreekGujaratiHaitian CreoleHebrewHindiHungarianIcelandicIndonesianIrishItalianJapaneseKannadaKoreanLaoLatinLatvianLithuanianMacedonianMalayMalteseNorwegianPersianPolishPortugueseRomanianRussianSerbianSlovakSlovenianSpanishSwahiliSwedishTamilTeluguThaiTurkishUkrainianUrduVietnameseWelshYiddishFrom: Detect languageTo:AfrikaansAlbanianArabicArmenianAzerbaijaniBasqueBelarusianBengaliBulgarianCatalanChinese (Simplified)Chinese (Traditional)CroatianCzechDanishDutchEnglishEsperantoEstonianFilipinoFinnishFrenchGalicianGeorgianGermanGreekGujaratiHaitian CreoleHebrewHindiHungarianIcelandicIndonesianIrishItalianJapaneseKannadaKoreanLaoLatinLatvianLithuanianMacedonianMalayMalteseNorwegianPersianPolishPortugueseRomanianRussianSerbianSlovakSlovenianSpanishSwahiliSwedishTamilTeluguThaiTurkishUkrainianUrduVietnameseWelshYiddishTo: EnglishEnglishSpanishFrenchTranslate text or webpage Type text or a website address or translate a document. Drag and drop file or link here to translate the document or web page. Drag and drop link here to translate the web page.

We do not support the type of file you drop. Please try other file types. We do not support the type of link you drop.

Please try link of other types.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions