IForeNETRuntimeObjectInstance.InvokeMethod

Syntax

InvokeMethod(Name: String; Args: Array): Variant;

Parameters

Name. Name of type member to be executed.

Args. Values of type member parameters used on executing. Values can be passed as an array, or enumerate using the "," separator.

Description

The InvokeMethod method executes the type member and returns the result of execution.

Comments

Execution result of .NET method is placed into the variable of the Variant type. If the execution result is an object of .NET type, then for further work with it the VariantToObject method should be used.

Referring to the limitations, this method does not enable user to perform the .NET-methods, the result of which is a structure. To execute such .NET methods, use IForeNETRuntimeMethod.Invoke.

It is required to pass an array of simple types parameters as values of the parameters specified in Args. If it is required to execute the .NET method having complex types parameters, also use the IForeNETRuntimeMethod.Invoke method.

Example

Executing this example requires a .NET assembly with the NETAssembly_1 identifier. This assembly should have the .NET unit, containing the next code:

Imports System;
Imports System.Text;
Imports System.IO;
Imports System.Security.Cryptography;

Public Class RijndaelSample
    Public Sub EncryptData(Path, Data: String);
    Var
        DataFile, Key, IV: FileStream;
        RijndaelAlg: Rijndael;
        KeyArr, IVArr: Array Of byte;
        CrStr: CryptoStream;
        sWriter: StreamWriter;
    Begin
        DataFile := File.Open(Path + "CodedFile.txt", FileMode.OpenOrCreate);
        Key := File.Open(Path + "Key.txt", FileMode.OpenOrCreate);
        IV := File.Open(Path + "IY.txt", FileMode.OpenOrCreate);
        RijndaelAlg := Rijndael.Create;
        KeyArr := RijndaelAlg.Key;
        IVArr := RijndaelAlg.IV;
        //Creation of instance of encoder
        CrStr := New CryptoStream(DataFile, RijndaelAlg.CreateEncryptor(KeyArr, IVArr), CryptoStreamMode.Write);
        sWriter := New StreamWriter(CrStr);
        sWriter.WriteLine(Data);
        sWriter.Close;
        //Saving of secret key
        sWriter := New StreamWriter(Key);
        sWriter.WriteLine(Convert.ToBase64String(KeyArr));
        sWriter.Close;
        //Saving of initialization vector
        sWriter := New StreamWriter(IV);
        sWriter.WriteLine(Convert.ToBase64String(IVArr));
        sWriter.Close;
        //Closing of streams
        CrStr.Close;
        DataFile.Close;
        Key.Close;
        IV.Close;
    End Sub EncryptData;
    
    Public Function DecryptData(Path: String): String;
    Var
        KeyArr, IVArr: Array Of byte;
        RijndaelAlg: Rijndael;
        CrStr: CryptoStream;
        DataFile, Key, IV: FileStream;
        sReader: StreamReader;
        s: string;
    Begin
        DataFile := File.Open(Path + "CodedFile.txt", FileMode.Open);
        Key := File.Open(Path + "Key.txt", FileMode.Open);
        IV := File.Open(Path + "IY.txt", FileMode.Open);
        //Loading of secret key
        sReader := New StreamReader(Key);
        s := sReader.ReadLine;
        KeyArr := Convert.FromBase64String(s);
        //Loading of initialization vector
        sReader := New StreamReader(IV);
        s := sReader.ReadLine;
        IVArr := Convert.FromBase64String(s);
        RijndaelAlg := Rijndael.Create;
        //Creation of encoder instance
        CrStr := New CryptoStream(DataFile, RijndaelAlg.CreateDecryptor(KeyArr, IVArr), CryptoStreamMode.Read);
        sReader := New StreamReader(CrStr);
        //Encoding data
        s := sReader.ReadLine;
        sReader.Close;
        //Closing of streams
        CrStr.Close;
        DataFile.Close;
        Key.Close;
        IV.Close;
        Return s;
    End Function DecryptData;

End Class RijndaelSample;

In the RijndaelSample class the procedure and function are implemented. The EncryptData procedure encodes, and the DecryptData function decodes with the Rijndael symmetric encryption algorithm. The result of EncryptData procedure work is three files, containing encoded data, secret key and initialization vector. The files are saved in the folder specified in the Path parameter of the method. The result of DecryptData function work is the decoded text.

How to use this class to encrypt and decrypt data in Fore is shown in the following example. Executing this example requires a form with two buttons named Button1 and Button2. There is the folder C:\Work in the file system.

Class TestForm: Form
    Button1: Button;
    Button2: Button;

    Sub Button1OnClick(Sender: Object; Args: IMouseEventArgs);
    Var
        MB: IMetabase;
        Run: IForeNETRuntime;
        NetAsm: IForeNETAssembly;
        Asm: IForeNETRuntimeAssembly;
        Typ: IForeNETRuntimeType;
        ObjInst: IForeNETRuntimeObjectInstance;
        v: Array;
    Begin
        MB := MetabaseClass.Active;
        Run := ForeNETAssemblyClass.Runtime;
        NetAsm := MB.ItemById("NETAssembly_1").Bind As IForeNETAssembly;
        //Receiving of assembly
        Asm := Run.Assembly(NetAsm);
        //Receiving of type
        Typ := Asm.Type("RijndaelSample");
        //Calling of constructor without parameters for type
        ObjInst := Typ.CreateInstance;
        //Creation of array of parameters for execution of type member
        v := New Variant[2];
        v[0] := "C:\Work\";
        v[1] := "Test text for encoding";
        //Execution of type member
        ObjInst.InvokeMethod("EncryptData", v);
    End Sub Button1OnClick;
    
    Sub Button2OnClick(Sender: Object; Args: IMouseEventArgs);
    Var
        MB: IMetabase;
        Run: IForeNETRuntime;
        NetAsm: IForeNETAssembly;
        Asm: IForeNETRuntimeAssembly;
        Typ: IForeNETRuntimeType;
        ObjInst: IForeNETRuntimeObjectInstance;
        s: String;
    Begin
        MB := MetabaseClass.Active;
        Run := ForeNETAssemblyClass.Runtime;
        NetAsm := MB.ItemById("NETAssembly_1").Bind As IForeNETAssembly;
        //Receiving of assembly
        Asm := Run.Assembly(NetAsm);
        //Receiving of type
        Typ := Asm.Type("RijndaelSample");
        //Calling of constructor without parameters for type
        ObjInst := Typ.CreateInstance;
        //Execution of type member
        s := ObjInst.InvokeMethod("DecryptData""C:\Work\");
        Debug.WriteLine(s);
    End Sub Button2OnClick;
End Class TestForm;

Pressing the button Button1 calls the procedure EncryptText of the RijndaelSample class. Three files: CodedFile.txt, Key.txt and IY.txt, are created in the folder C:\Work\ at work of this procedure. These files contain the encoded data, the secret key and initialization vector value accordingly. Pressing the button Button2 calls the DecryptText function. When this function works, it decodes the contents of the CodedFile.txt file. The value of the secret key and initialization vector, that are required for the Rijndael algorithm operation, are taken from Key.txt and IY.txt files. The result of decoding is displayed in the development environment console.

See also:

IForeNETRuntimeObjectInstance