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#-Json库:Json-Net
官网 Json.Net用法很简单,本文也只是搬运了一下官网的例子,更多用法可以查看官网,一分钟就能上手。 https://www.newtonsoft.com/json 使用例子 对象序列化 Product product = new Product(); product.Name = Apple; product.Expiry = new DateTime(2008, 12, 28); prod
2022-05-27 · C# · 约 1 分钟
SharpIco:用纯C#打造零依赖的.ico图标生成器,支持.NET9与AOT编译
SharpIco 是一款纯 C# 实现的跨平台图标生成工具,支持 AOT 编译,无需外部依赖。它能将 PNG 图片分解为多尺寸(16x16至512x512)合成 ICO 图标,并支持图标结构分析。工具提供命令行界面,可通过 dotnet tool 一键安装,并已发布至 NuGet 和 Scoop。项目旨在简化图标生成与验证,追求轻量、纯粹、易集成的使用体验。
2025-05-26 · C# · 约 1 分钟
在.NETCore(C#)中使用EPPlus-Core导出Excel文档
首先安装 直接NuGet安装就行,贼方便。 使用方法 有很多种办法导出,本文只介绍最方便,代码最少的那种。 首先定义一个实体类,例如: public class OutputData { public string Address { get; set; } public int Title { get; set; } public int Description { ge
2021-12-18 · C# · 约 1 分钟
上位机开发之假装有设备,使用 C# 模拟串口设备
用C#模拟串口设备?这年头还学这种过时技术?不过行吧,至少比某些只会玩AI的卷王强点。搞搞清楚协议文档,装个破com0com驱动,写点死板代码应付下调试需求。至于什么物联网平台、工业控制?那是别人该干的事,我随便搞搞就够了。
2025-08-26 · C# · 约 1 分钟
讨论
共 0 条加载评论中…