简体   繁体   English

C#Struct No无参数构造函数? 看看我需要完成什么

[英]C# Struct No Parameterless Constructor? See what I need to accomplish

I am using a struct to pass to an unmanaged DLL as so - 我使用结构传递给非托管DLL,因为 -

[StructLayout(LayoutKind.Sequential)]
        public struct valTable
        {
            public byte type;
            public byte map;
            public byte spare1;
            public byte spare2;
            public int par;
            public int min;
            public byte[] name;
            public valTable()
            {
                name = new byte[24];
            }
        }

The code above will not compile because VS 2005 will complain that "Structs cannot contain explicit parameterless constructors". 上面的代码将无法编译,因为VS 2005会抱怨“Structs不能包含显式无参数构造函数”。 In order to pass this struct to my DLL, I have to pass an array of struct's like so valTable[] val = new valTable[281]; 为了将这个结构传递给我的DLL,我必须传递一个struct的数组,如valTable[] val = new valTable[281];

What I would like to do is when I say new , the constructor is called and it creates an array of bytes like I am trying to demonstrate because the DLL is looking for that byte array of size 24 in each dimension. 我想要做的是当我说new ,构造函数被调用并且它创建了一个像我试图演示的字节数组,因为DLL正在寻找每个维度中大小为24的字节数组。

How can I accomplish this? 我怎么能做到这一点?

You can use a fixed size buffer - which I suspect you really want anyway, so as to get the data "inline" in the struct (rather than a reference to an array elsewhere). 您可以使用固定大小的缓冲区 - 我怀疑您确实想要它,以便在结构中获取“内联”数据(而不是在其他地方引用数组)。

public fixed byte name[24];

You'll need to declare the struct as unsafe as well though. 你需要将结构声明为不安全的。

Note that any "solution" which requires calling a static method or providing any kind of custom constructor will fail with your explicit goal of being able to create an array of these structs. 请注意,任何需要调用静态方法或提供任何类型的自定义构造函数的“解决方案”都将失败,您的明确目标是能够创建这些结构的数组。

I would recommend to write this code. 我建议写这段代码。

[StructLayout(LayoutKind.Sequential)]
public struct valTable
{
    public byte type;
    public byte map;
    public byte spare1;
    public byte spare2;
    public int par;
    public int min;
    public byte[] name;

    static public valTable Create()
    {
        valTable vT = new valTable();
        vT.name = new byte[24];
        return vT;
    }
}

Struct constructors are similar to class constructors, except for the following differences: 结构构造函数类似于类构造函数,但以下差异除外:

  • Structs cannot contain explicit parameterless constructors. 结构不能包含显式无参数构造函数。 Struct members are automatically initialized to their default values. Struct成员会自动初始化为其默认值。
  • A struct cannot have an initializer in the form: base (argument-list). 结构不能具有以下形式的初始化程序:base(argument-list)。

This means that 这意味着

A default(parameterless) constructor for a struct could set different
values than the all-zeroed state which would be unexpected behavior. The
.Net Runtime therefore prohabits default constructors for struct.

The typical way to get around this scenario is to create a static method that will create your new instance, initialize it the way you want, and return it. 解决此问题的典型方法是创建一个静态方法,该方法将创建新实例,以您希望的方式初始化它并返回它。 This is the way it is done in .NET to get structures initialized with specific values. 这是在.NET中完成使用特定值初始化结构的方式。


ref; REF; Structs cannot contain explicit parameterless constructors. 结构不能包含显式无参数构造函数。 WHY? 为什么?

Building on Asad Butt's answer , you can create a static method to perform the work of your constructor like so: Asad Butt的回答的基础上 ,您可以创建一个静态方法来执行构造函数的工作,如下所示:

[StructLayout(LayoutKind.Sequential)]
public struct valTable
{
    public byte type;
    public byte map;
    public byte spare1;
    public byte spare2;
    public int par;
    public int min;
    public byte[] name;
    public valTable()

    public static valTable NewTable()
    {
        valTable tbl = new valTable();
        tbl.name = new byte[24];
        return tbl;
    }
}

You'll see classes in the .NET Framework following this pattern already. 您将看到.NET Framework中的类已经遵循此模式。 Guid.NewGuid() immediately springs to mind. Guid.NewGuid()立即Guid.NewGuid()在脑海中。 There may be others. 可能还有其他人。

Not the cleanest fix, but you could just add a parameter and never use it? 不是最干净的修复,但你可以添加一个参数,从不使用它?

[StructLayout(LayoutKind.Sequential)]
    public struct valTable
    {
        public byte type;
        public byte map;
        public byte spare1;
        public byte spare2;
        public int par;
        public int min;
        public byte[] name;
        public valTable(int x)
        {
            name = new byte[24];
        }
    }

For what you need to do you really need a class I think. 对于你需要做的事,你真的需要一个我认为的课程。 A struct already implements a parameterless constructor by default which is why you cant define another one. 结构体默认情况下已经实现了无参数构造函数,这就是为什么你不能定义另一个。

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

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