在对 SO 和 google 进行了相当彻底的浏览后,我在问这个问题,大多数答案让我了解了大约 80% 的情况,但它仍然有点令人困惑,所以请告诉我出路。
我有一些 Visual C++ 函数定义如下:
我的DLL.h
#ifdef FUNCTIONS_EXPORTS
#define FUNCTIONS_API __declspec(dllexport) 
#else
#define FUNCTIONS_API __declspec(dllimport) 
#endif
namespace Functions {
    class MyFunctions {
    public:
        static FUNCTIONS_API int Add(int a, int b);
        static FUNCTIONS_API int Factorial(int a);
    };
}
我的DLL.cpp
namespace Functions {
    int MyFunctions::Add (int a, int b)
    {
        return a+b;
    }
    int MyFunctions::Factorial (int a)
    {
        if(a<0)
            return -1;
        else if(a==0 || a==1)
            return 1;
        else
            return a*MyFunctions::Factorial(a-1);
    }
}
现在,我想将此构建生成的 DLL 导入到我的 C# 程序中,如下所示:
程序.cs
using System;
using System.Collections.Generic;    
using System.Runtime.InteropServices;
namespace DLLTester
{
    class Program
    {
        [DllImport("path\\to\\the\dll\\myDLL.dll")]
        public static extern int Factorial(int a);
        static void Main(string[] args) {
            int num;
            num = int.Parse(Console.ReadLine());
            Console.WriteLine("The factorial is " + Factorial(num));
        }
    }
}
我试过编写没有类的函数(static定义时没有关键字),但即使这样也不起作用并给出错误。
这一切我哪里错了?