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:
Context is an ordered set of properties defining the object environment. The context is created as a result of object activation, and is used to work with its resources. A number of objects may be within one context.
.NET assembly is a basic building block on the basis of which the .NET application is built. Within the platform .NET assembly is a set of .NET forms and .NET units that are compiled as a single entity. Also, resources of .NET assemblies, registered in GAC or loaded from external DLL files, can be used in the platform. The IForeNETRuntimeAssembly interface is used to work with resources of .NET assemblies. The IForeNETRuntime.Assembly property or the IForeNETRuntime.SystemAssembly property can be used to receive the context of .NET assembly.
.NET type is class, interface, structure, enumeration type or delegate implemented in .NET assembly. The IForeNETRuntimeAssembly.Type property is used to access the .NET type. The IForeNETRuntimeType interface is used to work with .NET type. The .NET type is accessed by its qualified name. Instance of .NET type should be created to make its resources usable. To get the instance of .NET type the IForeNETRuntimeType.CreateInstance method or the IForeNETRuntimeType.CreateInstanceVar method is used.
.NET method is fields, procedures, functions, events and corresponding static methods that are implemented in .NET type. The IForeNETRuntimeType.Method property is used to access the .NET method. The IForeNETRuntimeMethod interface is used to work with .NET method. Methods are executed for instances of .NET objects. The Null value is set as an object instance to execute of static methods.
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:
IForeNETRuntimeObjectInstance.InvokeMethod is an execution of specified .NET method for the current instance of object.
IForeNETRuntimeType.InvokeMethod is an execution of specified .NET method of the current .NET type for specified instance of object. It is also used to execute static .NET methods.
IForeNETRuntimeMethod.Invoke is an execution of the current .NET method for specified instance of object. It is also used to execute static .NET methods.
Examples of methods use are presented in their description.
See also: