使用命名空间时出错?

时间:2012-03-16 11:58:36

标签: c# c++ .net

我正在研究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?)"

有人可以告诉我如何解决它。

非常感谢

1 个答案:

答案 0 :(得分:6)

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