Skip to content

WithLambdaExpressionBody Mappings condition

ldfallas edited this page Aug 31, 2015 · 3 revisions

Usage

<map:WithLambdaExpressionBody>
    ... conditions ...
</map:WithLambdaExpressionBody>

Description

Tests conditions against the lambda expression body.

Properties

​Property ​Usage ​Description
Subconditions Content property Conditions to test against the lambda parameter

Example

Say that we want to convert uses of System.Collections.Generic.List<T>.ForEach to a foreach statement.

For example we want to turn:

public void DoTask(List<int> aList)
{
  aList.ForEach(x => System.Diagnostics.Debug.WriteLine(x));
}

Into the following expression:

public void DoTask(List<int> aList)
{
    foreach ( var x in aList )
    {
        System.Diagnostics.Debug.WriteLine(x);
    }
}

Here we use the WithMethodCall element to identify the method call and perform more inner checks.

<MapUnit xmlns="clr-namespace:Mobilize.Mappers.Extensibility.Core;assembly=Mobilize.ExtensibleMappers"
         xmlns:map="clr-namespace:Mobilize.Mappers.Extensibility.Code;assembly=Mobilize.ExtensibleMappers">
   <MapUnit.Elements>
      <map:CodeMapPackage Type="System.Collections.Generic.List">
         <map:CodeMapPackage.Maps>
            <map:CodeMap Kind="Call" MemberName="ForEach">
               <map:Conditional>
                  <map:Case>
                     <map:Case.Condition>
                        <map:WithMethodCall>
                           <map:ArgumentCount>1</map:ArgumentCount>
                           <map:WithCalledMemberOwner>
                              <map:AssignName>$obj</map:AssignName>
                           </map:WithCalledMemberOwner>
                           <map:WithArgument Position="0">
                              <map:WithLambdaExpressionParameter Index="0">
                                 <map:AssignName>$x</map:AssignName>
                              </map:WithLambdaExpressionParameter>
                              <map:WithLambdaExpressionBody>
                                 <map:AssignName>$body</map:AssignName>
                              </map:WithLambdaExpressionBody>
                           </map:WithArgument>
                        </map:WithMethodCall>
                     </map:Case.Condition>
                     <map:Case.Action>
                        <map:ActionSequence>
                           <map:AddPreStatementFromTemplate>
                              foreach(var $x in $obj) {
                                 $body;
                              }
                           </map:AddPreStatementFromTemplate>
                           <map:RemoveCurrentStatement />
                        </map:ActionSequence>
                     </map:Case.Action>
                  </map:Case>
                  <map:Default>
                     <map:Keep />
                  </map:Default>
               </map:Conditional>
            </map:CodeMap>
         </map:CodeMapPackage.Maps>
      </map:CodeMapPackage>
   </MapUnit.Elements>
</MapUnit>

Notes

  • This mapping condition fails if the current element is not a lambda expression

TODO

Overview

Writing mappings

Code Mapping Actions

Code Mapping Conditions

XAML mapping actions

XAML mapping conditions

Misc

Clone this wiki locally