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 class in an assembly
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 Windows client installation folder on the computer running the development environment.
By default, the path to the installation folder is C:\Program Files\Microsoft Dynamics NAV\71\RoleTailored Client or C:\Program Files (x86)\Microsoft Dynamics NAV\71\RoleTailored Client. You can place the assembly in a subfolder.
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.
In the development environment, open Object Designer, and then open the object that will use .NET Framework interoperability, such as the page or codeunit.
To open the C/AL code for the object, on the View menu, choose C/AL Code.
To create the variable for .NET Framework interoperability, do one of the following steps:
To create a global variable, on the View menu, choose C/AL Globals.
To create a local variable, on the View menu, choose C/AL Locals.
Important A local DotNet variable, including any resources that it references, is disposed after its C/AL function is run.
- To create a global variable, on the View menu, choose C/AL Globals.
On the Variables tab, in the Name field, type a name for the variable.
Set the DataType field to DotNet.
In the SubType field, choose the AssistEdit button to open the .NET Type List window.
In the Assembly field, choose the AssistEdit button to open the Assembly List window.
If the assembly that you want to reference is in the global assembly cache, then choose the .NET tab. If the assembly is in the Add-ins folder of your Microsoft Dynamics NAV installation, then choose the Dynamics NAV tab.
In the list of assemblies, select the assembly, and then choose 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.
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 choose 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 Microsoft Dynamics NAV Windows client, see How to: Set .NET Framework Types to Run on the Microsoft Dynamics NAV Windows Client or Microsoft Dynamics NAV Server Computers.
To call methods and properties in a .NET Framework class
Open the C/AL code for the object.
On the View menu, choose C/AL Symbol Menu.
You use the C/AL Symbol Menu window to help you call members and properties of the .NET Framework class.
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.
MyVariableNamerepresents the name of the .NET Framework variable.
ConstructorNamerepresents the same as the type name.
argumentsrepresents any parameters that you must set for the constructor.
Add the following code to call the methods or properties.
MethodPropertyNamerepresents the name of the method or property that you want to call, and
argumentsrepresents 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 Mapping Between .NET Framework and C/AL Types. Tip 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 128 characters will be truncated, and you cannot compile the Microsoft Dynamics NAV object. For more information, see Calling .NET Framework Members from C/AL.
The following code example uses .NET Framework interoperability to display headlines from the RSS feed http://blogs.msdn.com/inside_office_online/rss.xml. This example accesses classes in the System.XML assembly that is found in Global Assembly Cache. To implement this example, create a codeunit that has the following local variables:
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:
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.
'System.Xml, Version=22.214.171.124, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlDocument
'System.Xml, Version=126.96.36.199, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlNodeList
'System.Xml, Version=188.8.131.52, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlNode
After you create the codeunit, add the following code to its OnRun trigger:
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 Microsoft Dynamics NAV Windows client, you can create an action on a page that opens the codeunit.