Example: Create a Custom C# Library

www.altova.com Print this Topic Previous Page Up One Level Next page

Home >  Functions > Referencing Java, C# and C++ Libraries Manually >

Example: Create a Custom C# Library

This topic describes how to create a sample C# library and configure the .mff file so that it appears in the Libraries window of MapForce.

 

1.Create a new class library project in Visual Studio. Notice that the function has been defined as public static.

 

using System;
 
namespace HelloWorldLibrary
{
  public class Greetings
  {
      public static string HelloFunction(bool GreetingType)
      {
          if (GreetingType)
              return "Hello World!";
          return "Hello User!";
      }
  }
}

 

2.If you need special XML Schema types (such as date and duration), you will need add a reference from your Visual Studio project to the Altova.dll library. To obtain this library, generate C# code from a mapping without custom functions. The Altova.dll file will be located in the ..\Altova\bin\debug directory relative to the directory where the code was generated. To add the reference to Altova.dll in Visual Studio, on the Project menu, click Add Reference and browse for the Altova.dll file. Also, add to your code the following line: using Altova.Types; . For information about how XML Schema types map to C# types, see Data Type Mapping.
3.Build your Visual Studio project. The HelloWorldLibrary.dll is generated in your project output directory.
4.Using an XML editor, create a new .mff file and validate it against the ..\Program Files\MapForceLibraries\mff.xsd folder. Make sure that the text highlighted below points to the HelloWorldLibrary.dll file. For more information, see Configuring the .mff File.

 

<?xml version="1.0" encoding="UTF-8"?>
<mapping version="9" library="helloworld" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="mff.xsd">
  <implementations>
    <implementation language="cs">
        <setting name="namespace" value="HelloWorldLibrary"/>
        <setting name="class" value="Greetings"/>
        <setting name="reference" value="C:\Projects\HelloWorldLibrary.dll"/>
    </implementation>
  </implementations>
  <group name="Greetings">
    <component name="HelloFunction">
        <sources>
          <datapoint name="greeting_type" type="xs:boolean"/>
        </sources>
        <targets>
          <datapoint name="result" type="xs:string" />
        </targets>
        <implementations>
          <implementation language="cs">
              <function name="HelloFunction"/>
          </implementation>          
        </implementations>
        <description>
          <short>result = hello(greeting_type)</short>
          <long>Returns a greeting according to the given greeting type.</long>
        </description>
    </component>
  </group>
</mapping>

 

You have now finished creating a custom library and the .mff file which adapts it to MapForce. The custom .mff file can now be used in MapForce (see Importing the .mff File Into MapForce).


© 2019 Altova GmbH