如何使用jedi/jcl从Delphi的.NET DLL调用静态方法



我不想创建tlb、更新引用等,所以我在运行时加载.net dll,我可以调用和使用类和方法,比如

procedure TForm1.Button1Click(Sender: TObject);
var
Host: TJclClrHost;
Obj: OleVariant;
result: string
begin
Host := TJclClrHost.Create('v4.0.30319');
Host.Start();
Obj := Host.DefaultAppDomain
.CreateInstance('DLLNAME',
'NAMESPACE.INSIDE.CLASS.THAT.I.WANT')
.UnWrap();

result:=  Obj4.<MethodName>(Parameters...);
....
end;

这对非静态方法来说是个奇迹。。。。静态方法我不知道如何使用JEDI/jcl

我是如何使用jed/jcl从delphi调用静态类/方法的,或者如果在运行时有其他方法可以做到这一点,请告诉我,我只是不想进行引用、生成tlb等,只需将dll放在文件夹中即可使用。

dll.net中静态类的示例:

using System;
using System.Text;
using System.Security.Cryptography;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
namespace DLLSTATIC.SOMESUBNAME
{
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.AutoDual)]
[ProgId("DLLSTATIC.SOMESUBNAME.staticclass")]
public static class staticclass
{
[ComVisible(true)]
public static string test123(string param)
{
return "test1234";
}
}
}

ps。我声明com可见,但我不会将此dll用作com

我不喜欢com和tlb。

您也可以使用DDNRuntime(Delphi.net Framework/.net Core runtime(在运行时调用.net dll

https://github.com/ying32/DDNRuntime-examples

它可以帮助您轻松调用.net函数

就像

function CreateMainForm(): DNForm;
var
LButton: DNButton;
LR: DNRectangle;
//LEdit: DNTextBox;
begin
LR := TDNScreen.DNClass.PrimaryScreen.Bounds;
Result := TDNForm.Create;
Result.Text := 'Delphi .NET Runtime';
LButton := TDNButton.Create;
LButton.Text := 'Hello';
LButton.add_Click(TEventClass.OnButtonClick);
LButton.Location := TDNPoint.DNClass.init(100, 100);
Result.Controls.Add(LButton);
//Result.StartPosition := DNFormStartPosition.Manual;
Result.StartPosition := DNFormStartPosition.CenterScreen;
//Result.Location :=  TDNPoint.DNClass.init((LR.Width - Result.Width) div 2, (LR.Height - Result.Height) div 2);
end;

最新更新