Using Fore.NET in Fore

A number of interfaces is implemented in this assembly to use resources of .NET objects in forms/units written in the Fore language. To understand the general principle of work with .NET objects, specify a number of definitions:

Entire work on .NET objects is performed within the context of current running Foresight Analytics Platform application. The IForeNETAssemblyClass.Runtime static property is used to get the application context.

Var
    Run: IForeNETRuntime;
Begin
    Run := ForeNETAssemblyClass.Runtime;

Having received a context of the application it is possible to access any .NET assemblies of repository or system assemblies, registered in GAC or stored as external DLL files.

Access to .NET assemblies of repository:

Var
    MB: IMetabase;
    Run: IForeNETRuntime;
    Asm: IForeNETRuntimeAssembly;
Begin
    MB := MetabaseClass.Active;
    Run := ForeNETAssemblyClass.Runtime;
    Asm := Run.Assembly(MB.ItemById("NetAssembly_1").Bind As IForeNETAssembly);

Access to system assemblies, registered in GAC:

Var
    Run: IForeNETRuntime;
    Asm1: IForeNETRuntimeAssembly;
Begin
    Run := ForeNETAssemblyClass.Runtime;
    Asm1 := Run.SystemAssembly("System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");

Access to .NET assemblies, implemented in separate DLL files:

Var
    Run: IForeNETRuntime;
    Asm2: IForeNETRuntimeAssembly;
Begin
    Run := ForeNETAssemblyClass.Runtime;
    Asm2 := Run.SystemAssembly("file:///C:\Work\UserFunc.dll");

Having received the access to .NET assembly it is possible to work with all its types. If work is performed with .NET assembly of repository, then via its context it is possible to receive context of any .NET form, implemented in this assembly:

Var
    //...
    Asm: IForeNETRuntimeAssembly;
    NetForm: IForeNETRuntimeForm;
Begin
    //...
    NetForm := Asm.Form("NetAssembly_1.MainForm");
    NetForm.Show;

Access to types of .NET assemblies:

Var
    //...
    Asm, Asm1, Asm2: IForeNETRuntimeAssembly;
    Type, Type1, Type2: IForeNETRuntimeType;
Begin
    //...
    Type := Asm.Type("NetAssembly_1.UserObject");
    Type1 := Asm1.Type("System.Windows.Forms.Cursors");
    Type2 := Asm2.Type("UserFunc.ClassFunc");

NOTE. The Asm, Asm1, Asm2 variables correspond to variables from the examples above. The UserObject class should be present in .NET assembly of repository; in a code of .NET assembly, implemented in UserFunc.dll - of the ClassFunc class.

Having any .NET type for the further work it is possible to create an instance of object for the given type. Two methods are used to create object instances: IForeNETRuntimeType.CreateInstance and IForeNETRuntimeType.CreateInstanceVar. These methods search and execute the constructor of .NET type. Depending on the signature of constructor that is used to create of object instance, additional parameters can be required in these methods. Constructors of .NET type can differ slightly: different types of parameters, number of parameters. CreateInstance is used if it is necessary to determine the type of constructor parameters and its value. Using it is relevant, if constructor parameters have any .NET type that is not compatible with Fore data types. To create additional parameters of this method, use such methods as IForeNETRuntime.CreateArgs and IForeNETRuntime.CreateBinding.

CreateInstanceVar is the special case of the CreateInstance method. This method is used if constructor parameters have simple types compatible with Fore data types.

Var
    //...
    Type: IForeNETRuntimeType;
    TypeInst: IForeNETRuntimeObjectInstance;
Begin
    //...
    TypeInst := Type.CreateInstance;

Having an object instance it is possible to perform various actions with it, to execute any .NET methods for it, to pass it as parameter for other .NET methods and so on.

The number of methods is implemented in Fore to execute .NET methods:

Examples of methods use are presented in their description.

See also:

Introduction