c# - 从 C++/CLI 调用 C# dll 时出现 FileNotFoundException

标签 c# .net dll visual-studio-2015 c++-cli

我有一个 C# dll,其中包含一些方法,我正尝试在我的本地项目中使用 /CLR 支持访问这些方法。

我使用 #using 指令引用此 DLL,然后识别 DLL 并编译项目。

但是,在运行时,我得到一个 FileNotFoundException,这很奇怪,因为 DLL 存在于项目的源目录中。

DLLVS 2015 中编译,.NET 版本为 4.5.2。因为我的 C++ 有 CLR 支持,所以我编辑了项目文件使 TargetFrameworkVersion 为 4.5.2,但 runtime 仍然支持不工作。

请告知可能是什么问题?

EIDT - 添加了一些代码

C#

namespace TestManagedLibrary
{
    public class Class1
    {
        public int i;
        public Class1()
        {
            i = 5;
        }

        public int returnValue()
        {
            return i;
        }
    }
}

C++/CLI

#using <TestManagedLibrary.dll>


using namespace System;
using namespace System::Runtime::InteropServices; // Marshal
using namespace TestManagedLibrary;


ref class ManagedFoo
{
public:
    ManagedFoo()
    {
        Console::WriteLine(_T("Constructing ManagedFoo"));
        Class1 ^testObject = gcnew Class1();
        int a = testObject->returnValue();
    }
};

最佳答案

首先您需要确保TestManagedLibrary.dll 文件位于Fusion 所在的位置可以找到它。您的第一次尝试应该是您正在运行的可执行文件的位置。

处理此问题的一种方法是通过引用属性。如果对 TestManagedLibrary.dll 的引用设置了 copy local 标志,那么在构建期间,引用的库将从引用的位置复制到输出目录。

您可以启用内部 fusion logging了解详情:

  1. 以管理权限运行 Developer Command Prompt
  2. 运行fuslogvw
  3. Assembly Binding Log Viewer 中点击设置并设置 Log bind failures to diskLog all binds to disk
  4. 开始您的服务
  5. Assembly Binding Log Viewer 中点击 Refresh,选择您的可执行文件并点击 View Log

关于c# - 从 C++/CLI 调用 C# dll 时出现 FileNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39230290/

相关文章:

c# - 如何使用 C# 聚合重复记录并求和

c# - 在两个大括号结束之前将文本写入 .CS 文件

c# - 为什么 Monitor.PulseAll 在信号线程中导致 "stepping stair"延迟模式?

.net - 使用ManagementObjectSearcher获得Sql Server 2008安装根的SqlServiceAdvancedProperty名称?

c++ - 我将在哪个 Windows dll 中找到 afxwin/CFrameWnd?

c# - 在 C# 应用程序中只能使用 C++ DLL 中的 1/4 函数

c++ - 如何让 Visual Studio 将所需的 .dll 文件复制到发布文件夹中?

c# - 自引用属性数组

c# - Unity项目中的GameCenter

c# - 是否可以模拟 FTDI USB 设备?