c# - 将 STL/C++/CLI 容器传递给 .Net

标签 c# .net vector stl c++-cli

我有一个向量 cv::Point2f。我想要的是将此向量从 C++/CLI 传递给 C#。

因为我不能只将 cv::Point2f 存储到 std::vector 中,所以我使用了 cliext::vector。 然后,我不确定如何从 C# 中获取它...

这是我的 C++/CLI 代码:

points 是已在其他地方定义和初始化的 cv::Point2f 向量。

在 C++/CLI 中,

cliext::vector<System::Drawing::Point> ManagedCPP::Points::get() {
    cliext::vector<System::Drawing::Point> cliext_points;

    for (auto &point : points) {
      cliext_points.push_back(System::Drawing::Point(points.x, points.y));
    }
    return corners;
}

在 C# 中,

ManagedCPP mcpp = new ManagedCPP();
??? = mcpp.get_Points();  // what should be ??? 

或者是否需要任何类型转换?

最佳答案

您不能返回类型 cliext::vector< T > 因为它没有公开声明。但是,您可以将其转换为 IEnumerable< T >:

   IEnumerable<System::Drawing::Point>^ ManagedCPP::Points::get() {

     auto corners = gcnew cliext::vector<ystem::Drawing::Point>();
      for (auto &point : points) {
         corners->push_back(System::Drawing::Point(points.x, points.y));
     }
     return corners;
  }

或者,您可以返回一个标准的 .NET 容器(列表或数组)。

array<System::Drawing::Point>^ ManagedCPP::Points::get() {

    auto list = gcnew List<System::Drawing::Point>(points.size());
    for(auto & point: points) {
       list->Add(System::Drawing::Point(point.x, point.y));
    }

    return list->ToArray();
}

关于c# - 将 STL/C++/CLI 容器传递给 .Net,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33557707/

相关文章:

c# - 从国家名称或 RegionInfo 对象获取 CultureInfo 对象

c# - 如何: Create a Key In the Registry (Visual C#)?

c++ - 如何使用迭代器?

c++ - vector 均匀切片的算法

c++ - 当我尝试打印 vector 的内容时编译器出错?

c# - 查找字符串(或子字符串)在大字符串中出现多少次的最佳方法 C#

C# 圣诞树

c# - Azure KeyVault - 签署 JWT token

java - 比较树节点

c# - 即时窗口是否正在逐步取消?