Importing Binary Types Programmatically

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

Home >  UModel Programmer's Reference > The UModel API > API Usage Examples > C# >

Importing Binary Types Programmatically

With UModel, you can import binary types from .NET .dll or Java .jar files, either from the graphical user interface, or programmatically using the UModel API. This example illustrates how to import binary types from a NET .dll file into UModel using the UModel API. For information about importing binary types from the graphical user interface, see Importing Java, C# and VB.NET Binaries.

 

This example uses Microsoft Visual Studio 2015 and C#. The instructions below (except for the code listing) are similar for VB.NET. To complete this example, you also need a .dll that contains some types (such as classes or interfaces) that you would like to import into UModel.

 

To accomplish the task, we will use an existing C# demo application that already integrates into the UModel API, rather than creating a new project from scratch. Namely, we will add to this demo application a new button. When clicked, the button will create a new UModel project and import into it types from a .dll file. To begin, run Visual Studio and open the following solution: C:\Users\<username>\Documents\Altova\UModel2019\UModelExamplesAPI\C#\AutomateUModel_VS2010.sln.

 

Note:The demo application already includes a reference to the UModel Type Library so it is not necessary to add a reference explicitly. However, if you are creating a new Visual Studio project, make sure to reference the UModel Type Library from your project, see How to Reference the UModel Type Library.

 

Next, open the Form1.cs in the Design Editor and add a new button. Let's call it Import Binary Types.

um_api_bintypes_01

Double-click the new button and paste the following code into the body of the handler method. Make sure that the path to the .dll file is correct and that the .dll qualifies for import of binary types (that is, it must not be obfuscated).

 

try
{
  // Create a new document
  UModelDocument = UModel.NewDocument();
  // Instantiate the Import Binary Types dialog
  UModelLib.ImportBinaryTypesDlg dlg = UModel.Dialogs.ImportBinaryTypesDlg;  
  // Set the .NET runtime version according to your environment (must be greater than v2.0)
  dlg.Runtime = "v2.0.50727";
  // Set the import language (C# 6.0, in this case)
  dlg.Language = UModelLib.ENUMCodeLangVersion.eCodeLang_CSharp_6_0;
  // No need to show the dialog since we want to do this programmatically
  dlg.ShowDialog = false;
  // Add a new binary type entry to be imported
  UModelLib.IBinaryTypeEntry entry = dlg.CSharp_BinaryTypes.AddItem();

  // Specify the .dll to import (make sure to adjust the path)
  entry.Entry = "C:\\Path\\To\\My.dll";
  // All types shall be imported from this .dll
  entry.ImportTypes = true;
  // The .dll is an executable
  entry.Executeable = true;
  // Perform the actual import
  UModelDocument.ImportBinaryTypes(dlg);
}
catch (Exception ex)
{
  MessageBox.Show(ex.Message);
}

Importing all types

The code above essentially creates a new UModel project, sets the import options in the "Import Binary Types" dialog box, and performs the actual import of binary types.

 

Build and run the solution. Click Start UModel, and be patient while the application loads. Only after the application has finished loading, click Import Binary Types, and observe the outcome in the Messages window of UModel.

 

If you would like to import only specific types, set the ImportTypes property is false, and supply the types to be imported as arguments to the TypesToImport method. The list of distinct types can be separated by comma, semi-colon, or space characters, as illustrated in the code listing below.

 

try
{
  UModelDocument = UModel.NewDocument();
  UModelLib.ImportBinaryTypesDlg dlg = UModel.Dialogs.ImportBinaryTypesDlg;
  dlg.ShowDialog = false;
  dlg.CSharp_BinaryTypes.RemoveAllItems();
  UModelLib.IBinaryTypeEntry entry = dlg.CSharp_BinaryTypes.AddItem();
  entry.Entry = "C:\\Path\\To\\My.dll";
  entry.ImportTypes = false;
  entry.Executeable = true;

  // import only specific types:
  entry.TypesToImport = "MyNamespace.Class1; MyNamespace.Class2";
  UModelDocument.ImportBinaryTypes(dlg);
}
catch (Exception ex)
{
  MessageBox.Show(ex.Message);
}

Importing distinct types


© 2019 Altova GmbH