Skip to content

AddNamespaceImport Code Mapping Action

ldfallas edited this page Aug 31, 2015 · 3 revisions

Description

Adds a C# using directive for the given namespace name in the current file.

Properties

​Property ​Usage ​Description
NamespaceName Required The name of the namespace to import

Example

Say that we created a helper class that exposes an extension method. As part of a mapping we are going to generate references to this extension method. However in order to use the extension method we need to make sure that the current file has a using directive that imports the namespace where the class that exposes the extension method is defined.

For example, we created the following extension method to emulate the behavior of System.IO.IsolatedStorage.IsolatedStorageSettings.TryGetValue.

Here's the definition of the extension method:

/// IsolatedStorageExtensionsHelper.cs
namespace WindowsPhoneUWP.UpgradeHelpers
{
	static class IsolatedStorageExtensionsHelper
	{
		public static bool TryGetValue<T>(this Windows.Storage.ApplicationDataContainer settings, string key, out T value)
		{
			object tmp;
			bool result;
			value = default(T);
			if (result = settings.TryGetValue(key, out tmp))
			{
				value = (T)tmp;
			}
			return result;
		}
	}
}

The mapping for the System.IO.IsolatedStorage.IsolatedStorageSettings.TryGetValue method needs to ensure that the WindowsPhoneUWP.UpgradeHelpers namespace is imported in the current project. To do that we write the following mapping:

<?xml version="1.0" encoding="utf-8"?>

<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.IO.IsolatedStorage.IsolatedStorageSettings">
      <map:CodeMapPackage.Maps>
        <map:CodeMap Kind="Call" MemberName="TryGetValue">
            <map:ActionSequence>
              <map:AddHelper Path="..\Helpers\IsolatedStorageExtensionsHelper.cs"/>
              <map:AddNamespaceImport>WindowsPhoneUWP.UpgradeHelpers</map:AddNamespaceImport>
            </map:ActionSequence>
        </map:CodeMap>
      </map:CodeMapPackage.Maps>
    </map:CodeMapPackage>
  </MapUnit.Elements>
</MapUnit>

Notes

  • This mapping action is useful when adding extension method references

Overview

Writing mappings

Code Mapping Actions

Code Mapping Conditions

XAML mapping actions

XAML mapping conditions

Misc

Clone this wiki locally