C# uses iKvm advanced technology to seamlessly integrate into the JVM ecosystem.
C# developers using iKVM to mooch off the JVM ecosystem? It's convenient, but dependency handling still requires manual effort. Seamless integration? It's just converting JARs to DLLs, saving the trouble of migrating code. Lacking AI tools? Just invoke HanLP, but dependency management... heh.
目录7 个章节
Preface
Time flies, and before I knew it, the National Day holiday was almost over. If I didn’t update my blog soon, I’d really be slacking off...
Recently, while working on AIHub, I was looking for a Named Entity Recognition (NER) library that I could use in C#, but I couldn’t find one. In terms of the AI ecosystem, C# isn’t exactly rich—it’s still more of a Python thing. However, I didn’t want to go down the gRPC route like with LLMs; it felt a bit cumbersome.
That’s when I realized that the JVM ecosystem actually has quite a few NLP tools, like Stanford NLP and HanLP. So, I thought of IKVM that I’d seen online before—I could just borrow the JVM ecosystem directly! 😃
About iKvm
Check the official introduction
IKVM is an implementation of Java for the Microsoft .NET platform. It can be used to quickly and easily:
- Execute compiled Java code (bytecode) on .NET Framework or .NET Core
- Convert bytecode to a .NET assembly to directly access its API in a .NET project
These tasks can be done without porting source code to .NET.
There are two working methods:
- Directly call the jar package to execute in C#
- Translate the jar package into a .NET platform dll, then reference and execute it
Generally, the second method is sufficient. The first one involves dynamic invocation and essentially provides no code hints, so I don't want to consider that approach.
iKvm is actually a comprehensive system. It includes a complete JDK standard library, runtime, and more. I gave it a quick look and found it has everything from Swing, XML, to media support. It even supports JDK reflection.
Furthermore, it comes with the modern build tool Maven!
PS: I'm not sure about Gradle; I haven't tried that yet.
Regarding Dependency Handling
While iKvm offers convenient Maven support, it cannot handle dependencies within a package!
For example, if you reference the StarAI package, which depends on the Transformer library, Maven would automatically download all dependencies for the build.
However, iKvm's Maven integration cannot automatically resolve dependencies. Therefore, you must manually add both the StarAI and Transformer libraries to the configuration.
Getting Started
This article uses HanLP as an example
Prerequisites
First, add two NuGet dependencies.
dotnet add package IKVM
dotnet add package IKVM.Maven.Sdk
Or directly edit the project file.
<ItemGroup>
<PackageReference Include="IKVM" Version="8.6.4" />
<PackageReference Include="IKVM.Maven.Sdk" Version="1.5.5" />
</ItemGroup>
Then, add the Maven dependency in the project file. You can just copy it directly from the Maven repository—very convenient!
For those unfamiliar with Java, here's a link: https://mvnrepository.com/
<ItemGroup>
<MavenReference Include="hanlp">
<groupId>com.hankcs</groupId>
<artifactId>hanlp</artifactId>
<version>portable-1.8.4</version>
</MavenReference>
</ItemGroup>
After saving, the IDE will automatically perform operations. It will download the dependencies IKVM needs, the JDK and runtimes for various platforms, and will also automatically download the JAR from Maven and transpile (transpile) it into a .NET platform DLL.
This process will take some time. Please wait patiently.
If it doesn't execute automatically, run the following manually.
dotnet restore
dotnet build
Getting Started with Coding
Here we use HanLP's sentence component analysis feature as an example.
using com.hankcs.hanlp.model.crf;
using com.hankcs.hanlp.model.perceptron;
using com.hankcs.hanlp.seg;
using com.hankcs.hanlp.seg.common;
namespace AIHub.Algo.HanLP;
public class NER {
private readonly string _modelPath;
public NER(string modelPath) {
_modelPath = modelPath;
}
public void Recognize(string input) {
PerceptronLexicalAnalyzer analyzer = new PerceptronLexicalAnalyzer(
Path.Combine(_modelPath, "cws.bin"),
Path.Combine(_modelPath, "pos.bin"),
Path.Combine(_modelPath, "ner.bin")
);
var result = analyzer.analyze(input);
Console.WriteLine(result);
}
}
For testing, simply call the Recognize method.
References
_EOF_
C# uses iKvm advanced technology to seamlessly integrate into the JVM ecosystem.
- 本文作者:
- 程序设计实验室
- 版权声明:
- 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
相关文章
一次小而美的重构:使用 C# 在 Avalonia 中生成真正好看的词云
重构StarBlogPublisher中词云展示功能的过程。作者对比了两个流行的C#词云库,最终选择了功能更强大的Sdcb.WordCloud。文章详细说明了如何使用该库获取词频数据并生成词云图像,并展示了重构前后的效果对比。
2025-04-26 · C# · 约 1 分钟
让 C# 也拥有 gofmt 般的体验:用 CSharpier + Roslyn 实现 csfmt
C# 代码长到像答辩?用 CSharpier 自动折行,再用 Roslyn 修正 K&R 花括号,Taskfile 一键收尾。再补上 format-check,省得 CI 反复制造 Git diff。两步流水线并非原生 gofmt,顺序和版本仍得老实锁住。
2026-10-22 · C# · 约 1 分钟
C#学习(一)委托的概念和使用
前言 C#学习系列是根据我以前的笔记整理出来复习,顺便发一下文章做个记录。 先引用一个介绍: C# 中的委托(Delegate)类似于 C 或 C++ 中函数的指针。委托(Delegate) 是存有对某个方法的引用的一种引用类型变量。引用可在运行时被改变。 委托(Delegate)特别用于实现事件和回调方法。所有的委托(Delegate)都派生自 System.Delegate 类。 根据使用其他
2021-12-18 · C# · 约 1 分钟
C# 扩展方法只会写 this 吗?C# 14 新语法直接把扩展方法玩出了花
.NET 10 的扩展方法语法糖?哦,用 `extension` 块定义扩展,支持属性和方法了,以前不行,现在行,真稀奇。静态扩展成员?太晚了点,给 `DateTime` 添加静态方法,终于能优雅了。升级吧,不然怎么享受这些新玩具?
2026-03-05 · C# · 约 1 分钟
讨论
共 0 条加载评论中…