如何在 C# .NET Core 6 中使用 Java .dll?

发布于 2025-01-11 09:59:29 字数 3100 浏览 0 评论 0原文

初始情况

我今天对我的项目做了一些测试 - 目标:将 .jar 文件作为 .dll 实现到 C# 项目中。我当前的 .java / .jar 文件如下所示。

package ws;

public class Adding
{
    public int Add(int a, int b)
    {
        return a + b;
    }
}

我使用 IKVM 成功将上述内容转换为 .dll (版本:7.5.0.2)。

我现在想在我的 C# 项目中引用此 .dll 并调用 Add(int a, int b) 方法。我已经添加了引用,如下所示:

在此处输入图像描述

无论如何,我无法调用该方法,因为编译器找不到 .dll 引用..

using Adding; // <= Compiler Error CS0246 (Can't find the reference)

Console.WriteLine(Add(1, 2));

有谁知道怎么做我能实现这个目标吗?我非常感谢任何形式的帮助,干杯!


编辑1:反编译

我已经按照评论中的要求,使用 反编译了 .dll ILSpy(版本:ILSpy 7.2),结果如下。

// C:\Users\maikh\Desktop\Adding.dll
// Adding, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
// Global type: <Module>
// Architecture: AnyCPU (64-bit preferred)
// Runtime: v4.0.30319
// Hash algorithm: SHA1

using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using IKVM.Attributes;

[assembly: Debuggable(true, false)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: AssemblyVersion("0.0.0.0")]
[module: SourceFile(null)]
[module: JavaModule(Jars = new string[] { "Adding.jar" })]
[module: PackageList(new string[] { })]

在反编译 .dll 时,我还找到了一些参考资料。我不知道这是否重要,但无论如何我都会提供。

// Detected TargetFramework-Id: .NETFramework,Version=v4.0
// Detected RuntimePack: Microsoft.NETCore.App

// Referenced assemblies (in metadata order):
// IKVM.Runtime, Version=7.5.0.2, Culture=neutral, PublicKeyToken=00d957d768bec828
    // Assembly reference loading information:
    // There were some problems during assembly reference load, see below for more information!
    // Error: Could not find reference: IKVM.Runtime, Version=7.5.0.2, Culture=neutral, PublicKeyToken=00d957d768bec828

// mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
    // Assembly reference loading information:
    // Info: Success - Found in Assembly List



// Assembly load log including transitive references:
// IKVM.Runtime, Version=7.5.0.2, Culture=neutral, PublicKeyToken=00d957d768bec828
    // Error: Could not find reference: IKVM.Runtime, Version=7.5.0.2, Culture=neutral, PublicKeyToken=00d957d768bec828

// mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
    // Info: Success - Found in Assembly List

编辑 2:反编译 V2

我已成功添加缺少的引用 IKVM.Runtime。尽管如此,我找不到任何有关命名空间、类或方法的信息。

输入图片此处描述

Initial situation

I've made a little test for my project today - The goal: Implement .jar files into a C# project as a .dll. My current .java / .jar file looks like the following.

package ws;

public class Adding
{
    public int Add(int a, int b)
    {
        return a + b;
    }
}

I successfully converted the above into a .dll with IKVM (Version: 7.5.0.2).

I now want to reference this .dll in my C# project and call the Add(int a, int b) method. I already added the reference like so:

enter image description here

Anyways I am not able to call the method, because the compiler can't find the .dll reference..

using Adding; // <= Compiler Error CS0246 (Can't find the reference)

Console.WriteLine(Add(1, 2));

Does anybody know how I could achieve this? I highly appreciate any kind of help, cheers!


Edit 1: Decompiling

I've decompiled the .dll, as demanded in the comments, with ILSpy (Version: ILSpy 7.2), which results into the following output.

// C:\Users\maikh\Desktop\Adding.dll
// Adding, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
// Global type: <Module>
// Architecture: AnyCPU (64-bit preferred)
// Runtime: v4.0.30319
// Hash algorithm: SHA1

using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using IKVM.Attributes;

[assembly: Debuggable(true, false)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: AssemblyVersion("0.0.0.0")]
[module: SourceFile(null)]
[module: JavaModule(Jars = new string[] { "Adding.jar" })]
[module: PackageList(new string[] { })]

I've also found some references, while decompiling the .dll. I don't know if this is important to mention, but I'll provide it anyways.

// Detected TargetFramework-Id: .NETFramework,Version=v4.0
// Detected RuntimePack: Microsoft.NETCore.App

// Referenced assemblies (in metadata order):
// IKVM.Runtime, Version=7.5.0.2, Culture=neutral, PublicKeyToken=00d957d768bec828
    // Assembly reference loading information:
    // There were some problems during assembly reference load, see below for more information!
    // Error: Could not find reference: IKVM.Runtime, Version=7.5.0.2, Culture=neutral, PublicKeyToken=00d957d768bec828

// mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
    // Assembly reference loading information:
    // Info: Success - Found in Assembly List



// Assembly load log including transitive references:
// IKVM.Runtime, Version=7.5.0.2, Culture=neutral, PublicKeyToken=00d957d768bec828
    // Error: Could not find reference: IKVM.Runtime, Version=7.5.0.2, Culture=neutral, PublicKeyToken=00d957d768bec828

// mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
    // Info: Success - Found in Assembly List

Edit 2: Decompiling V2

I've managed to add the missing reference IKVM.Runtime. Nevertheless I can't find any informations about the namespace, class or method.

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

盛夏尉蓝 2025-01-18 09:59:29

首先,您使用类作为命名空间,这可能是不正确的。您的方法调用可能看起来像这样:

var adder = new Adding();
Console.WriteLine(adder.Add(1, 2));

如果这不起作用,我会 检查生成的 dll 以验证它是否是符合要求的 .net dll。它还应该显示名称空间、类名和其他信息。像 dotPeek 或 ilSpy 这样的反编译器可能会以更易于阅读的格式显示相同的信息。

First of all, you are using a class as a namespace, and that is probably not correct. Your method call should probably look something like this:

var adder = new Adding();
Console.WriteLine(adder.Add(1, 2));

If that does not work I would inspect the produced dll to verify that it is a conforming .net dll. That should also show the namespaces, class names and other information. A decompiler like dotPeek or ilSpy might show the same information in a format that may be easier to read.

甲如呢乙后呢 2025-01-18 09:59:29

由于您的 Java 类位于 ws 包中,因此您应该在 C# 代码中使用 ws

using ws;

Adding adding = new Adding();
Console.WriteLine(adding.Add(1, 2));

如果您想调用 Add 方法静态地,在 Java 中将其声明为 static

package ws;

public class Adding
{
    public static int Add(int a, int b)
    {
        return a + b;
    }
}
using ws;

Console.WriteLine(Adding.Add(1, 2));

Since your Java class is in the ws package, you should be using ws in your C# code:

using ws;

Adding adding = new Adding();
Console.WriteLine(adding.Add(1, 2));

And if you want to call the Add method statically, declare it static in Java:

package ws;

public class Adding
{
    public static int Add(int a, int b)
    {
        return a + b;
    }
}
using ws;

Console.WriteLine(Adding.Add(1, 2));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文