-
Notifications
You must be signed in to change notification settings - Fork 5
IsUsedAsExpression Code Mapping Condition
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.
no properties
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>
Contact us for more information
Overview
Writing mappings
Code Mapping Actions
- ActionSequence
- AddHelper
- AddNamespaceImport
- AddPreStatementFromTemplate
- CommentOut
- Conditional
- Keep Code Mapping Action
- MarkAsNotMapped
- RedirectCall
- RedirectCallToInnerMember
- RedirectIndexer
- RedirectProperty
- RemoveCurrentStatement
- RemoveParameter
- ReplaceClassUsage
- ReplaceMethodBodyWithTemplate
- ReplaceParameterDeclarationType
- ReplaceParameterMember
- ReplaceParameterValue
- ReplaceWithMethodCall
- ReplaceWithProperty
- ReplaceWithTemplate
Code Mapping Conditions
- AllConditionsApply
- ArgumentCount
- AssignName
- AssignNameToArgumentRange
- IsExpressionOfType
- IsStringLiteralMatchingRegex
- WithArgument
- WithAssignment
- WithAssignmentLeftSide
- WithAssignmentRightSide
- WithCalledMemberOwner
- WithCalledMethodExpression
- WithConstructorCall
- WithLambdaExpressionBody
- WithLambdaExpressionParameter
- WithLeftSideOfDottedAccess
- WithMemberInitValue
- WithMethodCall
XAML mapping actions
- ActionSequence
- AddStatementToConstructorFromTemplate
- BindPropertyValueElement Xaml mapping action
- ChangeEventHandlerEventArgsType
- CommentOutElement
- CommentOutProperty
- MarkAsNotMapped
- MoveValueToContentProperty
- RemoveNamespaceDeclaration
- RenameElement
- RenameProperty
- ReplaceAttributeValue
- ReplaceEventHandlerBodyWithTemplate
- ReplaceEventHandlerParameterMember
- ReplaceNamespaceDeclaration
- ReplacePropertyValueWithParentResource
- ReplaceStaticResourceWithThemeResource
- SetPropertyValueToComplexElement
- SetPropertyValueToSimpleValue
- WrapContent
XAML mapping conditions
Misc