如何从结构中使用结构类型的参数 [英] How can I use a parameter of a struct type from a struct

查看:120
本文介绍了如何从结构中使用结构类型的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我在结构中声明了结构类型的参数,当我尝试从结构中使用该参数时,我得到错误NullReferencePointer。



我尝试了什么:



另外我知道你不能初始化结构中的结构。

有没有办法使用该参数而不会出现错误?



Hello,
I have declared a parameter of a struct type in a struct and when I try to use that parameter from the struct I get the error NullReferencePointer.

What I have tried:

Also I know that you can't initialize a struct in a struct.
Is there a way to use that parameter without getting that error?

[StructLayout(LayoutKind.Sequential)]
public struct TCommandParam
{
    public int iValue;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4096)]
    public byte[] sValue;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)]
    public byte[] FrameFD;

}

[StructLayout(LayoutKind.Sequential)]
public struct TCommandBuffer
{
    public int Command;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]
    public TCommandParam[] Param;
    public int ReturnValue;
}

推荐答案

好的 - 它不是导致问题的结构,它是struct。

.NET中任何东西的数组都是引用类型,即使它是struct(或任何值类型,如int或byte)的数组 - 如果你没有初始化引用类型,当你尝试使用它时会得到一个空引用错误。

在你的结构中,你有三个数组:sValue,FrameFD和Param,你没有显示它们被初始化。请记住,当您开始将信息传递给非托管代码时,MarshalAs属性只会产生影响:如果您没有将数组实例分配给这些阵列中的任何一个,那么每当您尝试使用它们时,都会得到一个空引用错误。

首先看看你实例化结构的位置,看看你是否为它们分配了数组空间。

只实例化TCommandBuffer结构,也是我没有为它们分配数组空间。我尝试在struct中创建一个构造函数并分配一个数组大小,但它不起作用。我如何为所有这些都分配数组空间,以便我可以使用这些struct参数?



我强烈建议您使用类而不是结构,部分原因是因为您可以提供初始值设定项(禁止使用哪些结构)以及无参数构造函数:

OK - it's not the struct that is causing the problem, it's the contents of the struct.
An array of anything in .NET is a reference type, even if it's an array of struct (or any value type such as int, or byte) - and if you don't initialize a reference type, you get a null reference error when you try to use it.
In your structs, you have three arrays: sValue, FrameFD, and Param none of which you show being initialized. Bear in mind that the MarshalAs attribute only has an effect when you start transferring information to unmanaged code: if you don't assign an array instance to any one of those arrays, you will get a null reference error whenever you try to use them.
So start by looking at where you instantiate the structs, and see if you do allocate array space for them all.
" only instantiate the TCommandBuffer struct, also I don't allocate array space for them. I tried creating a constructor inside the struct and allocate an array size but it didn't work. How can I allocate array space for all of them that so I can use those struct parameters?"

I'd strongly suggest you use a classes instead of structs, partly because you can provide initializers (which structs are forbidden to do) and also parameterless constructors:
[StructLayout(LayoutKind.Sequential)]
public sealed class TCommandParam
    {
    public int iValue;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4096)]
    public byte[] sValue = new Byte[4096];
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)]
    public byte[] FrameFD = new byte[64];

    }

[StructLayout(LayoutKind.Sequential)]
public sealed class TCommandBuffer
    {
    public int Command;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]
    public TCommandParam[] Param = new TCommandParam[10];
    public int ReturnValue;
    public TCommandBuffer()
        {
        for (int i = 0; i < Param.Length; i++)
            {
            Param[i] = new TCommandParam();
            }
        }
    }

无论如何,拥有大型结构是一个坏主意;它会严重影响性能:使用结构和类 - 这是什么所有关于? [ ^ ] - MS建议结构不要超过16个字节,如果编译为64位环境,则TCommandParam会执行,因为引用是8个字节。

Having large structs is a bad idea anyway; it can really badly affect performance: Using struct and class - what's that all about?[^] - MS recommendations are that structs shoudln't exceed 16 bytes, and your TCommandParam does if you compile for a 64 bit environment, since a reference is 8 bytes.


这篇关于如何从结构中使用结构类型的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆