[英]Could not load assembly System when using C++ and embedded mono to call to C# DLL
I have a C# sample code which is compiled into DLL using xbuild
on Linux, eg: 我有一个C#示例代码,该代码在Linux上使用
xbuild
编译为DLL,例如:
using System;
using System.IO;
using System.Collections.Generic;
// ...
namespace MySamples
{
public class MyExample
{
public static void test()
{
SortedSet<int> ss = new SortedSet<int>();
}
// main function calls for test()
}
}
I can easily compile the sample code in a command line using xbuild MyExample.csproj
into exe
or dll
, and then run using mono MyExample.exe
- everything works fine and the sample code returns an expected result. 我可以使用
xbuild MyExample.csproj
在命令行中轻松地将示例代码编译为exe
或dll
,然后使用mono MyExample.exe
运行-一切正常,示例代码返回预期结果。
Now I want to make a call for the sample code from my C++ code, in particular to the test()
function. 现在,我想从我的C ++代码中调用示例代码,尤其是
test()
函数。 I'm using mono runtime for that, and this is my C++ code: 我为此使用了单声道运行时,这是我的C ++代码:
#include <mono/jit/jit.h>
#include <mono/metadata/object.h>
#include <mono/metadata/environment.h>
#include <mono/metadata/assembly.h>
#include <mono/metadata/debug-helpers.h>
#include <mono/metadata/mono-config.h>
// ...
MonoDomain* domain = mono_jit_init("MyExample.dll");
MonoAssembly* assembly = mono_domain_assembly_open(domain, "MyExample.dll");
mono_config_parse("MyExample.dll.config");
// mono is not installed in default locations:
mono_set_dirs("mypath/lib/mono", "mypath/etc/mono");
MonoImage* image = mono_assembly_get_image(assembly);
MonoClass* klass = mono_class_from_name(image, "MySamples", "MyExample");
MonoObject* object = mono_object_new(domain, klass);
mono_runtime_object_init(object);
// call test()
MonoMethodDesc* mdesc = mono_method_desc_new(":test()", false);
MonoMethod* method = mono_method_desc_search_in_class(mdesc, klass);
mono_runtime_invoke(method, object, NULL, NULL);
// shutdown the runtime
mono_jit_cleanup (domain);
The C++ code, when run, returns an error of type: C ++代码在运行时返回错误类型:
Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=...' or one of its dependencies.
未处理的异常:System.IO.FileNotFoundException:无法加载文件或程序集'System,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = ...'或其依赖项之一。 ...
...
Since I know that if I run the same code using mono
through the command line and it works, I feel like there is a problem with my C++ code and how I set up the mono runtime. 因为我知道如果我通过命令行使用
mono
运行相同的代码并且可以正常工作,那么我感觉C ++代码以及如何设置Mono运行时存在问题。 I fixed a bunch of other errors by adding mono_set_dirs
since my Mono is not installed into the default directory (it's built from source). 由于我的Mono未安装到默认目录(它是从源代码构建的)中,因此我通过添加
mono_set_dirs
修复了许多其他错误。
I'm on Ubuntu 16.04, if it matters, mono is built from source, CMake is used to compile my C++ code, and the MyExample.dll.config
content: 我在Ubuntu 16.04上,如果很重要,mono是从源代码构建的,CMake用于编译我的C ++代码,以及
MyExample.dll.config
内容:
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
</configuration>
The *.csproj
file include System
in the following manner: *.csproj
文件以以下方式包括System
:
<Reference Include="System" />
Once again, I'm able to run the test()
when using mono
from command line (not from C++ project) with the C# settings as they are now. 再一次,当我从命令行(而不是从C ++项目)使用
mono
和现在的C#设置时,我能够运行test()
。
Here is one more strange thing : if I replace the SortedSet<int>
with List<int>
, it runs without throwing any errors. 这是另外一件奇怪的事情 :如果我用
List<int>
替换SortedSet<int>
List<int>
,它将运行而不会引发任何错误。 How can it be? 怎么可能? - they belong to the same namespace .
- 它们属于同一名称空间 。 I checked other types, and when initializing most of them, an exception gets thrown.
我检查了其他类型,并在初始化大多数类型时引发了异常。
Any ideas what is wrong here? 有什么主意在这里吗? I've been checking the mono embedded docs to try to find what could be wrong with my set up, but this is as far as I can reach.
我一直在检查单声道嵌入式文档,以尝试找出设置方面的问题,但这是我所能及的。 I'm beginner with C#, mono runtime and embedded mono.
我是C#,Mono运行时和嵌入式Mono的初学者。
The problem was so trivial - I messed up the mono lib path in my C++ code. 问题是如此的微不足道-我在C ++代码中弄乱了mono lib路径。 Apparently, you need to provide a prefix, and not the full path.
显然,您需要提供一个前缀,而不是完整路径。 So, in my case, I had to use:
因此,就我而言,我必须使用:
mono_set_dirs("mypath/lib", "mypath/etc");
without using mono
at the end. 最后不使用
mono
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.