简体   繁体   English

如何从阵列中复制 <unsigned short> 在C CLI中使用unsigned short []?

[英]How do I copy from an array<unsigned short> to an unsigned short[] in C CLI?

I am working on a C++ CLI wrapper for a C API. 我正在为C API开发C ++ CLI包装器。 The C API has a structure that contains an array of four unsigned shorts and an array of four ints. C API的结构包含四个无符号短路的数组和四个整数的数组。 So I have created a similar class for the C# code to use when calling into the wrapper function. 所以我为调用包装函数时使用的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;
}

Now we come to my question. 现在我们来回答我的问题。 I have added an internal method to the the CLI class to create a struct: 我已经在CLI类中添加了一个内部方法来创建一个struct:

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;
  }

but I get the error: IntelliSense: a value of type "System::Object ^" cannot be assigned to an entity of type "unsigned short" for each assignment. 但我得到错误: IntelliSense:类型“System :: Object ^”的值不能分配给每个赋值的“unsigned short”类型的实体 What is the proper syntax for this? 这个的正确语法是什么?

首先取消装箱参考类型,试试这个:

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

The code is correct, you'll see that your program compiles just fine. 代码是正确的,你会看到你的程序编译得很好。 Nothing is being boxed. 没有任何东西被装箱。 This is a bug in the IntelliSense parser. 这是IntelliSense解析器中的错误。 A rather strange one, hard to imagine how it fumbles this. 一个相当奇怪的,很难想象它是如何摸索这一点。 Not entirely uncommon, the parser was made by another company. 并非完全不常见,解析器是由另一家公司制作的。 The Edison Design Group, pretty famous for writing the only compiler that was ever able to implement the C++03 standard correctly. Edison Design Group,因编写能够正确实现C ++ 03标准的唯一编译器而闻名。 C++/CLI gives them heartburn though. 但是C ++ / CLI让他们心痛。

Two basic workarounds, you could use the field instead of the property: 两个基本的解决方法,您可以使用字段而不是属性:

   c_Struct ToStruct() {
        c_Struct results;
        results.uShorts[0] = _unsignedShorts[0];
        // etc...
   }

But that doesn't fix a problem you'd have with code that uses the class. 但这并不能解决您使用该类的代码所遇到的问题。 You could make them indexed properties instead: 您可以改为使用索引属性:

property unsigned short UnsignedShorts[int]
{
    unsigned short get(int index) {
        return _unsignedShorts[index];
    }
}
// Same for the Ints property.

Another work-around is to assign to a temporary local variable first. 另一种解决方法是首先分配一个临时局部变量。

array<int> ^temp = ArrayOfIntsProperty;
int j = temp[0];

This only affects properties - it appears that functions returning managed arrays work as expected when the call is indexed. 这只影响属性 - 看起来返回托管数组的函数在索引调用时按预期工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM