dotNetCore4Delphi Examples

The following examples demonstrate how to use the dotNetCore4Delphi to produce a software by combining Delphi Source Codes with .Net Core framework and other .Net Core libraries.

How to write a simple Hello World using .Net Console

Exported from Notepad++
program Project2; {$APPTYPE CONSOLE} {$R *.res} uses System.SysUtils, CNCoreClrLib.ObjectMgr; type (** * Delphi equivalent of the C# Console Class *) TConsole = class private class var FConsole: TCoreClrStaticObject; class function GetTitle: string; static; class procedure SetTitle(AValue: string); static; public class constructor Create; class destructor Destroy; class procedure Write(value: string); overload; class procedure WriteLine(); overload; class procedure WriteLine(value: Variant); overload; class function ReadLine(): string; class property Title: string read GetTitle write SetTitle; end; { TConsole } class constructor TConsole.Create; begin FConsole := TCoreClrStaticObject.Create('System.Console'); end; class destructor TConsole.Destroy; begin FConsole.Free; end; class function TConsole.GetTitle: string; begin Result := FConsole.GetPropertyValue('Title'); end; class function TConsole.ReadLine: string; begin Result := FConsole.InvokeMethod('ReadLine'); end; class procedure TConsole.SetTitle(AValue: string); begin FConsole.SetPropertyValue('Title', AValue); end; class procedure TConsole.Write(value: string); begin FConsole.InvokeMethod('Write', ['System.String'], [value]); end; class procedure TConsole.WriteLine(value: Variant); begin FConsole.InvokeMethod('WriteLine', ['System.Object'], [value]); end; class procedure TConsole.WriteLine; begin FConsole.InvokeMethod('WriteLine'); end; begin try TConsole.WriteLine(' Hello! Welcome to dotNetCore4Delphi '); TConsole.WriteLine('=================================================='); TConsole.WriteLine(' The program displays the string Hello World! '); TConsole.Title := 'Hello Word Title'; TConsole.WriteLine(''); TConsole.WriteLine('Hello World!'); except on E: Exception do Writeln(E.ClassName, ': ', E.Message); end; end.

How to Load .Net Core Assemblies

Exported from Notepad++
program Project2; {$APPTYPE CONSOLE} {$R *.res} uses System.SysUtils, CNCoreClrLib.Intf, CNCoreClrLib.AssemblyMgr; const DONET_CORE_VER = '5.0.2'; SYSTEM_DATA_COMMON_DLL = 'C:\Program Files (x86)\dotnet\shared\Microsoft.NETCore.App\'+DONET_CORE_VER+'\System.Data.Common.dll'; SYSTEM_NET_SECURITY_DLL = 'C:\Program Files (x86)\dotnet\shared\Microsoft.NETCore.App\'+DONET_CORE_VER+'\System.Net.Security.dll'; SYSTEM_DATA_COMMON_ASSEMBLY_STRING = 'System.Data.Common, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'; SYSTEM_PRIVATE_DLL = 'C:\Program Files (x86)\dotnet\shared\Microsoft.NETCore.App\'+DONET_CORE_VER+'\System.Private.CoreLib.dll'; SYSTEM_RUNTIME_ASSEMBLY_STRING = 'System.Runtime, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'; procedure DisplayAssemblyTypes(AAssembly : ICoreClrAssembly); var I: Integer; m_types: TArray<ICoreClrType>; begin Writeln('Loading Types for Assembly ''{0}''', AAssembly.FullName); Writeln('-----------------------------------------------------'); m_types := AAssembly.GetTypes; // Display all the types contained in the specified assembly. for I := 0 to Length(m_types) - 1 do Writeln(m_types[I].Name); Writeln(''); Writeln(''); end; procedure LoadAssemblyFromFile; var m_assembly: ICoreClrAssembly; begin Writeln('Loading System.Data.Common.dll using Assembly File'); m_assembly := TCoreClrAssembly.LoadFrom(SYSTEM_DATA_COMMON_DLL); DisplayAssemblyTypes(m_assembly); end; //The following example loads an assembly given its fully qualified name, and //lists all the types contained in the specified assembly. procedure LoadAssemblyByAssemblyString; var m_assembly: ICoreClrAssembly; m_types: TArray<ICoreClrType>; I: Integer; begin // You must supply a valid fully qualified assembly name. m_assembly := TCoreClrAssembly.Load(SYSTEM_DATA_COMMON_ASSEMBLY_STRING); DisplayAssemblyTypes(m_assembly); end; procedure LoadAssemblyByFilePath; var m_assembly: ICoreClrAssembly; begin Writeln('Loading System.Net.Security.dll using File Path'); m_assembly := TCoreClrAssembly.LoadFile(SYSTEM_NET_SECURITY_DLL); DisplayAssemblyTypes(m_assembly); end; procedure LoadAssemblyError; var m_assembly: ICoreClrAssembly; begin Writeln('Loading Inaccessible Assembly. This should error.'); TCoreClrAssembly.LoadFile(SYSTEM_PRIVATE_DLL); end; begin try Writeln(' Hello! Welcome to dotNetCore4Delphi '); Writeln('========================================================='); Writeln('This program demonstrate how to Load .Net Core Assemblies'); LoadAssemblyFromFile; LoadAssemblyByAssemblyString; LoadAssemblyByFilePath; LoadAssemblyError; except on E: Exception do Writeln(E.ClassName, ': ', E.Message); end; end.

How to Create instance of a .Net Object

Exported from Notepad++
program Project2; {$APPTYPE CONSOLE} {$R *.res} uses System.SysUtils, CNCoreClrLib.Intf, CNCoreClrLib.ObjectMgr; procedure CreateBaseObject; var baseObject: ICoreClrObject; begin baseObject := TCoreClrObject.Create(); Writeln(baseObject.GetType.FullName); Writeln(baseObject.ToString); end; procedure CreateIntegerObject; var value: Integer; clrObject: ICoreClrObject; begin value := 9878; clrObject := TCoreClrObject.Create(value); Writeln(clrObject.GetType.FullName); Writeln(clrObject.ToString); end; procedure CreateArrayObject; var clrObject: ICoreClrObject; begin clrObject := TCoreClrObject.Create(TArray<Integer>.Create(1, 2, 3, 4)); Writeln(clrObject.GetType.FullName); Writeln(clrObject.ToString); Writeln(clrObject.IsArray); Writeln(clrObject.GetArrayLength); Writeln(clrObject.getArrayElement(0)); Writeln(clrObject.getArrayElement(1)); clrObject.SetArrayElement(1, 1000); Writeln(clrObject.getArrayElement(1)); end; begin try Writeln(' Hello! Welcome to dotNetCore4Delphi '); Writeln('========================================================'); Writeln('This program demonstrate how to Create .Net Core Objects'); // Create Basic Objects CreateBaseObject; CreateIntegerObject; CreateArrayObject; except on E: Exception do Writeln(E.ClassName, ': ', E.Message); end; end.

How to Create static .Net Object

Exported from Notepad++
program Project2; {$APPTYPE CONSOLE} {$R *.res} uses System.SysUtils, CNCoreClrLib.Intf, CNCoreClrLib.ObjectMgr; type //Provides constants and static methods for trigonometric, logarithmic, //and other common mathematical functions. TMath = class private class var FMath: TCoreClrStaticObject; class function GetPI: Double; static; public class constructor Create; class destructor Destroy; class function Abs(Value: Double): Double; static; class function Floor(Value: Double): Double; static; class function Ceiling(Value: Double): Double; static; class function Min(Value1, Value2: Double): Double; static; class function Max(Value1, Value2: Double): Double; static; class function Pow(x, y: Double): Double; static; class function Round(Value: Double): Double; static; class function Truncate(Value: Double): Double; static; class property PI: Double read GetPI; end; TCreateStaticObjectDemo = class public class procedure Abs; static; class procedure Floor; static; class procedure Round; static; class procedure Truncate; static; class procedure PI; static; end; { TMath } class function TMath.Abs(Value: Double): Double; begin Result := FMath.InvokeMethod('Abs', ['System.Double'], [Value]); end; class function TMath.Ceiling(Value: Double): Double; begin Result := FMath.InvokeMethod('Ceiling', ['System.Double'], [Value]); end; class constructor TMath.Create; begin FMath := TCoreClrStaticObject.Create('System.Math'); end; class destructor TMath.Destroy; begin FMath.Free; end; class function TMath.Floor(Value: Double): Double; begin Result := FMath.InvokeMethod('Floor', ['System.Double'], [Value]); end; class function TMath.GetPI: Double; begin Result := FMath.GetFieldValue('PI'); end; class function TMath.Max(Value1, Value2: Double): Double; begin Result := FMath.InvokeMethod('Max', ['System.Double', 'System.Double'], [Value1, Value2]); end; class function TMath.Min(Value1, Value2: Double): Double; begin Result := FMath.InvokeMethod('Min', ['System.Double', 'System.Double'], [Value1, Value2]); end; class function TMath.Pow(x, y: Double): Double; begin Result := FMath.InvokeMethod('Pow', ['System.Double', 'System.Double'], [x, y]); end; class function TMath.Round(Value: Double): Double; begin Result := FMath.InvokeMethod('Round', ['System.Double'], [Value]); end; class function TMath.Truncate(Value: Double): Double; begin Result := FMath.InvokeMethod('Truncate', ['System.Double'], [Value]); end; { TCreateStaticObjectDemo } //The following example uses the Abs(Double) method to //get the absolute value of a number of Double values. class procedure TCreateStaticObjectDemo.Abs; var doubles: TArray<Double>; value: Double; begin doubles := TArray<Double>.Create(1.797693134E+308, 16.354e-17, 15.098123, 0, -19.069713, -15.058e18, -1.797693134E+308); Writeln('Abs:'); for value in doubles do Writeln(FloatToStr(TMath.Abs(value))); end; //The following example illustrates the Math.Floor(Double) method and contrasts it //with the Ceiling(Double) method. class procedure TCreateStaticObjectDemo.Floor; var doubles: TArray<Double>; value: Double; begin doubles := TArray<Double>.Create(7.03, 7.64, 0.12, -0.12, -7.1, -7.6); Writeln('Floors:'); for value in doubles do Writeln(FloatToStr(TMath.Floor(value))); end; //The following example uses the Pow method to calculate the value that results from //raising 2 to a power ranging from 0 to 32. class procedure TCreateStaticObjectDemo.PI; begin Writeln(FloatToStr(TMath.PI)); end; class procedure TCreateStaticObjectDemo.Round; begin Writeln('Classic Math.Round in CSharp'); Writeln(FloatToStr(TMath.Round(4.4))); // 4 Writeln(FloatToStr(TMath.Round(4.5))); // 4 Writeln(FloatToStr(TMath.Round(4.6))); // 5 Writeln(FloatToStr(TMath.Round(5.5))); // 6 end; class procedure TCreateStaticObjectDemo.Truncate; var floatNumber: double; begin floatNumber := 32.7865; // Displays 32 Writeln(FloatToStr(TMath.Truncate(floatNumber))); floatNumber := -32.9012; // Displays -32 Writeln(FloatToStr(TMath.Truncate(floatNumber))); end; begin try Writeln(' Hello! Welcome to dotNetCore4Delphi '); Writeln('===================================================='); Writeln('This program demonstrate how to Create Static Object'); TCreateStaticObjectDemo.Abs; TCreateStaticObjectDemo.Floor; TCreateStaticObjectDemo.Round; TCreateStaticObjectDemo.Truncate; TCreateStaticObjectDemo.PI; except on E: Exception do Writeln(E.ClassName, ': ', E.Message); end; end.

How to Create instance of a generic Object

Exported from Notepad++
program Project2; {$APPTYPE CONSOLE} {$R *.res} uses System.SysUtils, CNCoreClrLib.Intf, CNCoreClrLib.ObjectMgr, CoreClrTypes; type TCoreGenericObject = class(TCoreClrObject) public constructor Create(AGenericTypeName: string; AGenericTypeNames: array of string; AArguments: array of Variant); overload; constructor Create(AGenericTypeName: string; AGenericTypeNames: array of string; AArguments: array of NCObject); overload; constructor Create(AGenericTypeName: string; AGenericTypeNames: array of string; AArguments: NCObjectArray); overload; end; [TypeMappingAttribute('Project2.TList<T>', 'System.Collections.Generic.List`1')] IList<T> = Interface(ICoreClrObject) end; TList<T> = class(TCoreClrGenericObject, IList<T>) public constructor Create(); overload; constructor Create(capacity: Integer); overload; end; { TList<T> } constructor TList<T>.Create(capacity: Integer); begin Inherited Create([GetTypeInfo<T>], 'System.Collections.Generic.List`1', [GetTypeName<T>], [capacity]); end; constructor TList<T>.Create; begin Inherited Create([GetTypeInfo<T>], 'System.Collections.Generic.List`1', [GetTypeName<T>], []); end; { TCoreGenericObject } constructor TCoreGenericObject.Create(AGenericTypeName: string; AGenericTypeNames: array of string; AArguments: NCObjectArray); begin inherited Create(AGenericTypeName, AGenericTypeNames, AArguments); end; constructor TCoreGenericObject.Create(AGenericTypeName: string; AGenericTypeNames: array of string; AArguments: array of NCObject); begin inherited Create(AGenericTypeName, AGenericTypeNames, AArguments); end; constructor TCoreGenericObject.Create(AGenericTypeName: string; AGenericTypeNames: array of string; AArguments: array of Variant); begin inherited Create(AGenericTypeName, AGenericTypeNames, AArguments); end; { Generic Object Examples } procedure DisplayGenericInfo( GenericObj: TCoreClrObject); begin Writeln(GenericObj.GetType.FullName); Writeln(GenericObj.IsGeneric); Writeln(GenericObj.GetPropertyValue('Capacity')); end; procedure CreateGenericListOfInt32; var genericObject: TCoreGenericObject; begin //C# List<int> genericObject = new List<int>(10); genericObject := TCoreGenericObject.Create('System.Collections.Generic.List`1', ['System.Int32'], [10]); try DisplayGenericInfo(genericObject); finally genericObject.Free; end; end; procedure CreateGenericListOfString; var genericObject: TCoreGenericObject; begin //C# List<string> genericObject = new List<string>(); genericObject := TCoreGenericObject.Create('System.Collections.Generic.List`1', ['System.String'], nil); try DisplayGenericInfo(genericObject); finally genericObject.Free; end; end; procedure CreateGenericListObjectOfInteger; var genericObject: TList<Integer>; begin //C# List<int> genericObject = new List<int>(10); genericObject := TList<Integer>.Create(10); try DisplayGenericInfo(genericObject); finally genericObject.Free; end; end; begin try Writeln(' Hello! Welcome to dotNetCore4Delphi '); Writeln('================================================================'); Writeln('This program demonstrate how to Create .Net Core Generic Objects'); CreateGenericListOfInt32; CreateGenericListOfString; CreateGenericListObjectOfInteger; except on E: Exception do Writeln(E.ClassName, ': ', E.Message); end; end.

How to load external .Net Core DLL and invoke members

Exported from Notepad++
program Project2; {$APPTYPE CONSOLE} {$R *.res} uses System.SysUtils, CNCoreClrLib.Intf, CNCoreClrLib.ObjectMgr, CNCoreClrLib.AssemblyMgr; (* Assuming you have .Net Core library project below built as Mathematics.Dll namespace Mathematics { public class Mathematics { public int Add(int a, int b) { return a + b; } public int Subtract(int a, int b) { return a - b; } public bool Equal(int a, int b) { return a == b; } } } *) //Corresponding Delphi class type of Mathematics type in the Mathematics.dll const // Assuming the .Net Core project was built and saved into C:\Temp Assembly_Path = 'C:\Temp\Mathematics.dll'; Math_TypeName = 'Mathematics.Mathematics'; type TMathematics = class(TCoreClrObject) public constructor Create; function Add(a, b: Integer): Integer; function Subtract(a, b: Integer): Integer; function Equal(a, b: Integer): Boolean; end; { TMathematics } function TMathematics.Add(a, b: Integer): Integer; begin Result := InvokeMethod('Add', ['System.Int32', 'System.Int32'], [a, b]); end; constructor TMathematics.Create; begin inherited Create(Math_TypeName); end; function TMathematics.Equal(a, b: Integer): Boolean; begin Result := InvokeMethod('Equal', ['System.Int32', 'System.Int32'], [a, b]); end; function TMathematics.Subtract(a, b: Integer): Integer; begin Result := InvokeMethod('Subtract', ['System.Int32', 'System.Int32'], [a, b]); end; var mathematics: TMathematics; begin try Writeln(' Hello! Welcome to dotNetCore4Delphi. '); Writeln('====================================================='); Writeln('This program demonstrate how to use dotNetCore4Delphi'); Writeln('to load and invoke .Net Library and Types '); Writeln(''); // Load the assembly TCoreClrAssembly.LoadFrom(Assembly_Path); // Create Mathematics Object mathematics := TMathematics.Create; try Writeln('Add(30, 50): ' + IntToStr(mathematics.Add(30, 50))); Writeln('Subtract(30, 50): ' + IntToStr(mathematics.Subtract(30, 50))); Writeln('Equal(30, 50): ' + BoolToStr(mathematics.Equal(30, 50), True)); Writeln('Equal(50, 50): ' + BoolToStr(mathematics.Equal(50, 50), True)); finally mathematics.Free; end; except on E: Exception do Writeln(E.ClassName, ': ', E.Message); end; end.

How to use .Net ArrayList in Delphi

Exported from Notepad++
program Project2; {$APPTYPE CONSOLE} {$R *.res} uses System.SysUtils, CNCoreClrLib.ObjectMgr; type (** * Delphi equivalent of the C# ArrayList Class *) TArrayList = class(TCoreClrObject) private function GetCount: Integer; function GetCapacity: Integer; function GetItem(index: Integer): Variant; procedure SetItem(Index: Integer; Value: Variant); public constructor Create; function Add(Value: Variant): Integer; property Count: Integer read GetCount; property Capacity: Integer read GetCapacity; property Item[Index: Integer]: Variant read GetItem write SetItem; default; end; { TArrayList } function TArrayList.Add(Value: Variant): Integer; begin Result := InvokeMethod('Add', ['System.Object'], [value]); end; constructor TArrayList.Create; begin inherited Create('System.Collections.ArrayList'); end; function TArrayList.GetCapacity: Integer; begin Result := GetPropertyValue('Capacity'); end; function TArrayList.GetCount: Integer; begin Result := GetPropertyValue('Count'); end; function TArrayList.GetItem(index: Integer): Variant; begin Result := GetPropertyValue('Item', index); end; procedure TArrayList.SetItem(Index: Integer; Value: Variant); begin SetPropertyValue('Item', Index, value); end; var AArrayList: TArrayList; I: Integer; begin try Writeln(' Hello! Welcome to dotNetCore4Delphi '); Writeln('=================================================='); Writeln(' This program prints out ArrayList values. '); Writeln(''); AArrayList := TArrayList.Create(); AArrayList.Add('Hello'); AArrayList.Add('World'); AArrayList.Add('!'); Writeln('Array List'); Writeln(' Count: ' + IntToStr(AArrayList.Count)); Writeln(' Capacity: ' + IntToStr(AArrayList.Capacity)); Write(' Values:'); Writeln(''); for I := 0 to AArrayList.Count - 1 do Writeln(Chr(9) + ' ' + AArrayList[I]); except on E: Exception do Writeln(E.ClassName, ': ', E.Message); end; end.