c# - 如何在 C CLI 中从数组 <unsigned short> 复制到 unsigned short[]?

标签 c# c++ c arrays

我正在为 C API 开发 C++ CLI 包装器。 C API 的结构包含一个包含四个无符号短整型的数组和一个包含四个整数的数组。因此,我为调用包装函数时使用的 C# 代码创建了一个类似的类。

// C Structure
typedef struct c_Struct_
{
  unsigned short   uShorts[4];
  int     ints[4];
} c_Struct;


// C++ CLI Class
public ref class CliClass
{
public:
  property array<unsigned short>^ UnsignedShorts
  {
    array<unsigned short>^ get()
    {
      return _unsignedShorts;
    }
  }
  property array<int>^ Ints
  {
    array<int>^ get()
    {
      return _ints;
    }
  }

  CliClass(array<unsigned short>^ us, array<int> i)
  {
    _unsignedShorts = us;
    _ints = i;
  }
private:
  array<unsigned short>^ _unsignedShorts;
  array<int>^ _ints;
}

现在我们来回答我的问题。我在 CLI 类中添加了一个内部方法来创建一个结构:

internal:
  c_Struct ToStruct()
  {
    c_Struct results;
    results.uShorts[0] = UnsignedShorts[0];
    results.uShorts[1] = UnsignedShorts[1];
    results.uShorts[2] = UnsignedShorts[2];
    results.uShorts[3] = UnsignedShorts[3];
    results.ints[0] = Ints[0];
    results.ints[1] = Ints[1];
    results.ints[2] = Ints[2];
    results.ints[3] = Ints[3];
    return results;
  }

但我收到错误:IntelliSense:对于每个赋值,“System::Object ^”类型的值不能分配给“unsigned short”类型的实体。正确的语法是什么?

最佳答案

首先拆箱引用类型,试试这个:

results.uShorts[0] = (unsigned short)UnsignedShorts[0];

关于c# - 如何在 C CLI 中从数组 <unsigned short> 复制到 unsigned short[]?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19916757/

相关文章:

c# - 如何使用参数在 c# 中调用 powershell 脚本

c# - MongoDB中的默认ObjectId

c - fork tree C,子进程PID

c# - Visual Studio 2010 不断改变我的 winforms 控件

c# - 启动启动进程 B 的 Win32 进程 A——获取 B 的 ID/HWND

c++ - 临时对象的成员函数的左值引用返回是悬挂引用吗?

c++ - 在 UI Thread 中创建对话框导致崩溃

c++ - QT:QFileInfo().exists() 不起作用

android - 将音乐播放器守护进程 (MPD) 移植到 Android

c - 什么是 openwall crypt_r 的数据参数?