如何使用代理类操作打包结构中的数据?

发布于 2024-08-22 15:13:59 字数 1026 浏览 6 评论 0原文

我有一个项目,其中我使用 System.Runtime.InteropServices 按以下方式定义一个结构,以便将其打包到字节边界并准备发送到串行端口并从那里发送到嵌入式系统。 (业务敏感名称已被删除)

public class ControlCommandClass 
{
  [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
  public struct ControlCommandData
  {
    public Uint32 Field1;
    public Uint16 Field2;
    public Sbyte  Field3;
    public Uint32 Field4;
  }; // this struct is 11 bytes in memory!

  private ControlCommandData rawdata;

  public UTCTime Field1;

  public ControlCommandClass()
  {
    this.Field1 = new UTCTime(ref this.rawdata.Field1);
  }
}

我想做的是使用构造函数将对这些字段的引用分配给代理类,使用将

Field1 = new UTCTime(ref this.rawdata.Field1)

结构中的原始数据包装到一个类,该类允许在之前进行更高级的操作计算对应于时间的 32 位整数。我的代理类是

public class UTCTime : Field
{
  private Uint32 dataReference;

  public UTCTime(ref rawData)
  {
    // code to do reference assignment here?
  }
}

有没有办法让 dataReference 作为对 Field1 的引用,以便我的代理类能够操作打包结构中的数据?

提前致谢, 托马斯.

i have a project wherein i am using System.Runtime.InteropServices to define a struct in the following manner such that it is packed to byte boundaries and ready to send to a serial port and from there to an embedded system. (business sensitive names have been removed)

public class ControlCommandClass 
{
  [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
  public struct ControlCommandData
  {
    public Uint32 Field1;
    public Uint16 Field2;
    public Sbyte  Field3;
    public Uint32 Field4;
  }; // this struct is 11 bytes in memory!

  private ControlCommandData rawdata;

  public UTCTime Field1;

  public ControlCommandClass()
  {
    this.Field1 = new UTCTime(ref this.rawdata.Field1);
  }
}

What i am trying to do is to do is to use the constructor to assign references to those fields to a proxy class using

Field1 = new UTCTime(ref this.rawdata.Field1)

to wrap the raw data in the structure to a class which allows more advanced operations before calculating the 32 bit integer which corresponds to the time. my proxy class is

public class UTCTime : Field
{
  private Uint32 dataReference;

  public UTCTime(ref rawData)
  {
    // code to do reference assignment here?
  }
}

Is there any way to have dataReference as a reference to Field1 such that my proxy class is able to manipulate the data in the packed structure?

Thanks in advance,
Thomas.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤芳又自赏 2024-08-29 15:13:59

这是不可能的。

一般经验法则是,您不能存储对托管对象的引用。

通过引用传递允许您使用任意数量的引用。但是您必须切换到带有 IntPtr不安全 块才能完成您想要做的事情。

That is not possible as is.

General rule of thumb is that you cannot store a reference to a managed object.

Passing by reference allows you to use a reference as much as you want. But you'd have to switched to an unsafe block with IntPtr's to accomplish what you're trying to do.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文