Skip to content

WithMethodCall Code Mappings condition

ldfallas edited this page Sep 11, 2015 · 5 revisions

Usage

<map:WithMethodCall>
    ... sub  conditions ...
</map:WithMethodCall>

Description

This condition verifies that the current expression is a method call. It also allows to test conditions on the method call.

Properties

Property ​Usage ​Description
​Subconditions ​Content property The sub conditions to check against the method invocation

Examples

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 condition succeeds if the current expression is a method call and if its inner conditions succeed.

Overview

Writing mappings

Code Mapping Actions

Code Mapping Conditions

XAML mapping actions

XAML mapping conditions

Misc

Clone this wiki locally