Skip to content

IsUsedAsExpression Code Mapping Condition

ldfallas edited this page Nov 6, 2015 · 1 revision

Description

This condition succeeds if the value of the current expression is used by a another language construct.

For example:

var x = instance.SampleMethod();

In this case the value of the instance.SampleMethod invocation is used as part of a local variable declaration. Hence, in this case, the condition succeeds for the mapping of SampleMethod.

In the following scenario the condition fails since it is used as an statement.

if (instance != null)
{
    instance.SampleMethod();
}

This condition is useful when trying to determine if a method being called as an statement.

Properties

no properties

Example

Say for example that we want to convert MessageBox.Show("") invocations in a different way when it's used as an expression.

For example for the following WP8 code:

MessageBoxResult result = MessageBox.Show("Hi as expression!");

MessageBox.Show("Hi as statement "+ result.ToString());

We want to generate:

int result = (int)(await (new Windows.UI.Popups.MessageDialog("Hi as expression!")
    {
        Commands =
        {
            new Windows.UI.Popups.UICommand()
            {
            Id = 10, Label = "OK"
            },
        }
    }).ShowAsync()).Id;
await (new Windows.UI.Popups.MessageDialog("Hi as statement " + result.ToString())).ShowAsync();

The following mapping show how we use the IsUsedAsExpression mapping condition to generate code in a different way depending on the way MessageBox.Show is being used.

<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.Windows.MessageBox">
      <map:CodeMapPackage.Maps>
        <map:CodeMap Kind="Call" MemberName="Show">
          <map:Conditional>
            <!-- Case for one argument and used as expression -->
            <map:Case>
              <map:Case.Condition>
                <map:WithMethodCall>
                  <map:ArgumentCount>1</map:ArgumentCount>
                  <map:IsUsedAsExpression/>
                  <map:WithArgument Position="0">
                    <map:AssignName>$message</map:AssignName>
                  </map:WithArgument>
                </map:WithMethodCall>
              </map:Case.Condition>
              <map:Case.Action>
                <map:ReplaceWithTemplate>
                  <![CDATA[
                  (int)(await (new Windows.UI.Popups.MessageDialog($message) {
                        Commands =
                        {
                          new Windows.UI.Popups.UICommand() { Id = 10, Label = "OK" },
                        }
                  }).ShowAsync()).Id
                  ]]>
                </map:ReplaceWithTemplate>
              </map:Case.Action>
            </map:Case>
            <!-- Case for one argument -->
            <map:Case>
              <map:Case.Condition>
                <map:WithMethodCall>
                  <map:ArgumentCount>1</map:ArgumentCount>
                  <map:WithArgument Position="0">
                    <map:AssignName>$message</map:AssignName>
                  </map:WithArgument>
                </map:WithMethodCall>
              </map:Case.Condition>
              <map:Case.Action>
                <map:ReplaceWithTemplate>
                  await (new Windows.UI.Popups.MessageDialog($message)).ShowAsync()
                </map:ReplaceWithTemplate>
              </map:Case.Action>
            </map:Case>
      </map:CodeMapPackage.Maps>
    </map:CodeMapPackage>
  </MapUnit.Elements>
</MapUnit>

Overview

Writing mappings

Code Mapping Actions

Code Mapping Conditions

XAML mapping actions

XAML mapping conditions

Misc

Clone this wiki locally