You can call .NET Framework type members, including methods, properties, and constructors, from C/AL code. To call members, you define a DotNet type variable that references the .NET Framework assembly type that contains the member and then call the variable from C/AL code of the Microsoft Dynamics NAV object. A .NET Framework interoperability example is included in this topic.

To create a variable for a .NET Framework type

  1. If the assembly that you want to reference is a custom assembly and not installed in the global assembly cache, then copy the assembly to the Add-ins folder of the Microsoft Dynamics NAV installation folder.

    By default, the path to the installation folder is C:\Program Files\Microsoft Dynamics NAV\60\Classic. For more information about the global assembly cache, see Assembly Installation in the GAC in the MSDN Library.

    Tip
    You can install the assembly in the global assembly cache with the GAC utility. For more information, see Global Assembly Cache Tool in the MSDN Library.
  2. In the Classic client, open Object Designer, and then open the object that will use .NET Framework interoperability, such as the page or codeunit.

  3. To open the C/AL code for the object, on the View menu, click C/AL Code.

  4. To create the variable for .NET Framework interoperability, do one of the following steps:

    • To create a global variable, on the View menu, click C/AL Globals.

    • To create a local variable, on the View menu, click C/AL Locals.

      Important
      A local DotNet variable, including any resources that it references, is disposed after its C/AL function is run.
  5. On the Variables tab, in the Name field, type a name for the variable.

  6. Set the DataType field to DotNet.

  7. In the SubType field, click the AssistEdit button to open the .NET Type List window.

  8. In the Assembly field, click the AssistEdit button to open the Assembly List window.

  9. If the assembly that you want to reference is in the global assembly cache, then click the .NET tab. If the assembly is in the Add-ins folder of your Microsoft Dynamics NAV installation, then click the Dynamics NAV tab.

  10. In the list of assemblies, select the assembly, and then click OK to return to the .NET Type List window.

    If you do not see the assembly that you want, then make sure that you have installed it correctly. For more information, see step 1 of this procedure.

  11. In the .NET Type List window, select the type that contains the member that you want to call from the Microsoft Dynamics NAV object, and then click OK.

    The .NET Type List window displays all available types, including classes, enumerations, structs, and interfaces, which are available in the selected assembly.

    By default, variables are set to run the assembly instance on Microsoft Dynamics NAV Server. If you want to run the assembly instance on the RoleTailored client, see How to: Set .NET Framework Types to Target the RoleTailored Client or Microsoft Dynamics NAV Server.

To call members in a .NET Framework type from C/AL code

  1. Open the C/AL code for the object.

  2. On the View menu, click C/AL Symbol Menu.

    You can use the C/AL Symbol Menu window to help you call constructors, members, and properties of the .NET Framework type.

  3. In your C/AL code, add the following code to call the constructor that creates a new instance of the type. If you are calling a static method, then a constructor is not required, and you can skip this step.

      Copy Code
    MyVariableName.ConstructorName(arguments);
    
    • The MyVariableName variable is the name of the .NET Framework variable.

    • The ConstructorName variable is the same as the type name.

    • The arguments variable represents any parameters that you must set for the constructor.

    Note
    Member names are case-sensitive. Also, constructor names that are longer than 30 characters are truncated after 30 characters.
  4. Add the following code to call the methods or properties.

      Copy Code
    MyVariableName.MethodPropertyName(arguments);
    

    The MethodPropertyName variable is the name of the method or property that you want to call, and the arguments variable represents any arguments of the method or property.

    Important
    Some types in the System namespace of the .NET Framework class library are automatically converted to C/AL types. You cannot assign values to .NET Framework variables for these types. For more information, see .NET Framework and C/AL Type Mappings.
    Note
    Methods can be overloaded. Therefore see the description at the bottom of the C/AL Symbol Menu, which shows the parameter and return types for each method. Also, method and property names that are longer than 30 characters will be truncated, and you cannot compile the Microsoft Dynamics NAV object. For more information, see Calling .NET Framework Members from C/AL Code.

Example

The following code example uses .NET Framework interoperability to display headlines from an RSS feed from the Microsoft Dynamics NAV team blog, which has the following URL: http://feeds.feedburner.com/MicrosoftDynamicsNavTeamBlog?format=xml.

This example calls members of the System.XML assembly, which is part of the Microsoft .NET Framework class library and is installed in the global assembly cache.

To implement this example, you create a codeunit that has the following local variables.

Variable name DataType SubType

xml

DotNet

'System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlDocument

items

DotNet

'System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlNodeList

i

Integer

title

DotNet

'System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlNode

After you create the codeunit, add the following code to its OnRun trigger.

  Copy Code
xml := xml.XmlDocument();
xml.Load('http://feeds.feedburner.com/MicrosoftDynamicsNavTeamBlog?format=xml');
items := xml.SelectNodes('/rss/channel/item');
FOR i := 0 TO items.Count - 1 DO
BEGIN
	title := items.Item(i).SelectSingleNode('title/text()');
	MESSAGE( title.Value );
END

To see the example in the RoleTailored client, you can create an action on a page that opens the codeunit.

See Also