使用命名空间时出错

Error when using namespace?

本文关键字:出错 命名空间      更新时间:2023-10-16

我正在研究C++和C#之间的dllexport和dllimport。

我读过这篇文章http://functionx.com/csharp2/libraries/cppcli.htm

我的C++库代码:

// Business.h
#pragma once
using namespace System;
namespace Business {
    public ref class Finance
    {
    public:
    double CalculateDiscount(double MarkedPrice,
                             double DiscountRate)
    {
        return MarkedPrice * DiscountRate / 100;
    }
    };
}

这里是C#代码:

using System;
using System.Runtime.InteropServices;
using Business;
namespace DepartmentStore
{
    class Exercise
    {
        [DllImport("Business.dll")]
        public static extern double CalculateDiscount(double price,
                                                      double discount)
        static int Main()
        {
            Finance fin = new Finance();
        double markedPrice  = 275.50;
        double discountRate =  25.00; // %
            double discountAmount = fin.CalculateDiscount(markedPrice,
                                                          discountDate);
            double netPrice = markedPrice - discountAmount);
            Console.WriteLine("Marked Price:    {0:C}", markedPrice);
            Console.WriteLine("Discount Rate:   {0:P}", discountRate / 100);
            Console.WriteLine("Discount Amount: {0:C}", discountAmount);
            Console.WriteLine("Net Price:       {0:C}n", netPrice);
        return 0;
        }
    }
}

但是它在构建时遇到了错误"The type or namespace name 'Business' could not be found (are you missing a using directive or an assembly reference?)"

有人可以告诉我怎么修。

非常感谢

DLL函数导入和导出使用C ABI。这个二进制接口对名称空间一无所知。此外,您创建的DLL使用C++/CLI,而不是C ABI。要使用它,您不会使用DLLImport,而是在项目设置中引用DLL。