Using mono to call C# from C/C++(使用 mono 从 C/C++ 调用 C#)
问题描述
我需要在 Linux 上编写 docx 文件,所以我正在编译 Open-XML-SDK 与 mono.我正在尝试做 这个网站建议.
I need to write docx files on Linux so I'm compiling the Open-XML-SDK with mono. I'm trying to do what this website suggests.
我能做些什么来理解为什么我不能为构造函数获取 MonoMethod* 对象,或者更好的是,让它工作?
What can I do to understand why I can't get the MonoMethod* object for the constructor, or better, get this to work?
这是我的示例程序和输出:
Here are my sample programs and output:
// hello.cs
using System;
namespace HelloWorld
{
class Hello
{
public Hello(string s) { _s = s; }
public void DoSomething(string s) { _s = s; }
public string _s;
}
}
<小时>
// hello.cpp
#include <mono/jit/jit.h>
#include <mono/metadata/assembly.h>
#include <mono/metadata/debug-helpers.h>
int main(int argc, char* argv[]) {
MonoDomain* domain = mono_jit_init("hello.dll");
MonoAssembly* assembly = mono_domain_assembly_open(domain, "hello.dll");
MonoImage* image = mono_assembly_get_image(assembly);
MonoClass* helloClass = mono_class_from_name(image, "HelloWorld", "Hello");
MonoMethodDesc* ctorDesc = mono_method_desc_new("HelloWorld.Hello:Hello(string)", false);
MonoMethod* ctorMethod = mono_method_desc_search_in_class(ctorDesc, helloClass);
printf("ctorDesc from mono_method_desc_new() is %p
", ctorDesc);
printf("ctorMethod from mono_method_desc_search_in_class() is %p <----
", ctorMethod);
MonoMethodDesc* doDesc = mono_method_desc_new("HelloWorld.Hello:DoSomething(string)", false);
MonoMethod* doMethod = mono_method_desc_search_in_class(doDesc, helloClass);
printf("doDesc from mono_method_desc_new() is %p
", doDesc);
printf("doMethod from mono_method_desc_search_in_class() is %p
", doMethod);
mono_jit_cleanup(domain);
}
<小时>
$ mcs /nologo /warn:4 /debug:pdbonly /o /out:hello.dll /target:library hello.cs /reference:WindowsBase.dll
$ gcc hello.cpp -g3 `pkg-config --cflags --libs mono-2` -o hello
$ ./hello
ctorDesc from mono_method_desc_new() is 0x22b1920
ctorMethod from mono_method_desc_search_in_class() is (nil) <----
doDesc from mono_method_desc_new() is 0x22b2590
doMethod from mono_method_desc_search_in_class() is 0x224ae38
$ uname -a
Linux U14-OOXML 3.16.0-37-generic #51~14.04.1-Ubuntu SMP Wed May 6 15:23:14 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
推荐答案
Hello类的构造函数是Hello(string)是C#语言的谎言, 让 C++ 和 Java 程序员更熟悉它.
That the constructor of class Hello is Hello(string) is a lie told by the C# language, to make it more familiar to C++ and Java programmers.
但 Mono 运行时,如 Microsoft .NET CLR,使用编译后的代码,而不是 C#.每个实例构造函数的真实名称是 .ctor,独立于类型名称,而 .cctor 是类型初始化器(又名静态构造函数").如果您搜索 .ctor(string) 而不是 Hello(string),您应该会成功.有一些 工作 例子 在线,一旦你知道要寻找什么就更容易找到.
But the Mono runtime, like the Microsoft .NET CLR, works with compiled code, not C#. The true name of every instance constructor is .ctor, independent of the type name, and .cctor for a type initializer (aka "static constructor"). If you search for .ctor(string) instead of Hello(string), you should meet with success. There are some working examples online, easier to find once you know what to look for.
C# 关于名称的其他地方是默认索引器、嵌套类型、闭包......实际上不少.在所有这些情况下,您都可以通过使用反汇编程序(.NET 中的 ildasm,不确定等效 Mono 工具的名称)来查看元数据中存在的真正命名约定.
Other places where C# lies about names are for default indexers, nested types, closures.... quite a few actually. In all these cases you can see the true naming convention present in the metadata by using a disassembler (ildasm in .NET, not sure about the name of the equivalent Mono tool).
这篇关于使用 mono 从 C/C++ 调用 C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 mono 从 C/C++ 调用 C#
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- 输入按键事件处理程序 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
