简体   繁体   English

具有整数引用C#的元帅结构

[英]Marshal structure with integer references to C#

Hi I'm trying to create and marshal the following structure from C# into C++ and maintain the linked reference. 嗨,我正在尝试创建以下结构并将其从C#编组为C ++,并维护链接的引用。 I'm unsure how this structure should be defined in C#? 我不确定如何在C#中定义此结构? In C++ the structure must look like below, with the const reference maintained. 在C ++中,结构必须如下所示,并保持const引用。

// C++
struct {
        int   a;       // my value
  const int&  b = a;   // my reference to a
}

Does anyone know if this is possible? 有人知道这是否可能吗?

Thanks. 谢谢。

Edit: 编辑:

This is more representative of what I'm trying to accomplish, and as pointed out by @Hans it is not legal C++, but maybe someone can suggest a better path? 这更能代表我要完成的工作,正如@Hans指出的那样,这不是合法的C ++,但也许有人可以提出更好的方法? The system_t is generated in either C++ or C# and passed to C++. system_t是用C ++或C#生成的,并传递给C ++。 My best guess: (if this is even a good design pattern) is to initialize all the references in the system_t constructor in C++. 我最好的猜测:( 如果这甚至是一个好的设计模式)是在C ++中初始化system_t构造函数中的所有引用。 As far as marshaling from C#, it will get complicated. 就从C#封送而言,它将变得复杂。

struct system_t
{
  float    sysSampleRate = 112500.0f;            // Sample rate from receivers.
                                                 // Illegal statement @Hans

  struct tvg_t  // tvg_t is passed to tvg processor 
  {
          float    tvgLinearGain;
    const float&   tvgSampleRate = sysSampleRate;   // Get the rate from system.
                                                    // Illegal statement @Hans
  } tvg;   // Nested tvg_t in system_t.

  //... Many other structures and variables ..//
};

I'd like to find the right design pattern rather than abandoning this and going to a flat structure or passing system_t to every module. 我想找到正确的设计模式,而不是放弃这种设计模式,而system_t使用平面结构或将system_t传递给每个模块。

This should work: 这应该工作:

[StructLayout(LayoutKind.Sequential)]
public struct MyCStruct {
  public int a;
  public IntPtr b;
}

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

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