简体   繁体   English

C#const结构列表

[英]C# list of const struct

I want to create a look-up table that can be referenced from other classes, so I am a attempting to create a list of const structures as follows: 我想创建一个可以从其他类引用的查找表,所以我试图创建一个const结构列表,如下所示:

    public struct DataRouting1
{
    public struct tParameters
    {
            private readonly bool prvFieldbus;
            private readonly int prvAddressMax;
            private readonly string prvTypeLabel;
            private readonly byte prvXTPtype;
            private readonly string prvPointLabel;
            private readonly int prvPointMin;
            private readonly int prvPointMax;
            private readonly int prvPointValue;
            private readonly int prvQuantityMax;

            public tParameters(bool Fieldbus, int AddressMax, string TypeLabel, byte XTPtype, string PointLabel, int PointMin, int PointMax, int PointValue, int QuantityMax)
            {
                this.prvFieldbus = Fieldbus;
                this.prvAddressMax = AddressMax;
                this.prvTypeLabel = TypeLabel;
                this.prvXTPtype = XTPtype;
                this.prvPointLabel = PointLabel;
                this.prvPointMin = PointMin;
                this.prvPointMax = PointMax;
                this.prvPointValue = PointValue;
                this.prvQuantityMax = QuantityMax;
            }
            public bool Fieldbus { get { return Fieldbus; } }
            public int AddressMax { get { return AddressMax; } }
            public string TypeLabel { get { return TypeLabel; } }
            public byte XTPtype { get { return XTPtype; } }
            public string PointLabel { get { return PointLabel; } }
            public int PointMin { get { return PointMin; } }
            public int PointMax { get { return PointMax; } }
            public int PointValue { get { return PointValue; } }
            public int QuantityMax { get { return QuantityMax; } }
    }
    public static readonly List<tParameters> Parameter = new List<tParameters>
    {

        new tParameters (true,  250,    "Fieldbus Digital Input",           0x80,   "Number",   1,      65535,  0,  255),
        new tParameters (true,  250,    "Fieldbus Digital Output",          0x81,   "Number",   1,      65535,  0,  255),
        new tParameters (true,  250,    "Fieldbus Input Register",          0x82,   "Number",   1,      65535,  0,  255),
              .
              .
              .
    };
}

I use it as follows: 我用它如下:

    if (DataRouting1.Parameter[Index].Fieldbus == false)
      .
      .

This compiles correctly, but when I ran it the program crashed, saying it had a memory overflow. 这个编译正确,但是当我运行它时程序崩溃,说它有内存溢出。 I stepped through the program and found that when I stepped into to above line the application called the line 我逐步完成了程序,发现当我走到上面时,应用程序调用了该行

                public bool Fieldbus { get { return Fieldbus; } }

and remained there indefinitely. 并无限期地留在那里。 Stepping toggles between highlighting the '{' after 'get' and 'return Fieldbus', so it apparently enters an infinite loop. 在“获取”和“返回现场总线”之后突然显示“{”之间的步进切换,因此它显然进入无限循环。

Any ideas what I am doing wrong? 我有什么想法我做错了吗?

You need to return your private backup variable this.prvFieldbus instead. 您需要返回私有备份变量this.prvFieldbus This is also true for your other properties. 对于您的其他属性也是如此。

public bool Fieldbus { get { return prvFieldbus; } }
public bool Fieldbus { get { return Fieldbus; } }

creates infinite recursion. 创建无限递归。 Fieldbus returns Fieldbus returns Fieldbus etc..., until the call stack flows over. Fieldbus返回Fieldbus返回Fieldbus等...,直到调用堆栈流过。

If the purpose of a structure is to encapsulate a fixed collection of independent variables, simply exposing the fields directly will allow one to define much more concisely a structure which will allow one could do everything one could do if it exposed properties, in exactly the same way. 如果一个结构的目的是封装一个固定的自变量集合,简单地直接暴露这些字段将允许人们更简洁地定义一个结构,这将允许人们可以做任何事情,如果它暴露属性,完全相同办法。 Further, storage locations of the resulting structure's type will be mutable in exactly the same circumstances as they would be if one wrapped its fields in read-only properties; 此外,结果结构类型的存储位置将在与在只读属性中包含其字段的情况完全相同的情况下是可变的。 the primary semantic difference is that an exposed-field struct will allow some things to be done efficiently and in thread-safe fashion that a so-called immutable struct would only allow to be done in an inefficient and non-thread-safe fashion. 主要的语义差异在于,一个公开的字段结构将允许一些事情有效地以线程安全的方式完成,即所谓的不可变结构只允许以低效和非线程安全的方式完成。 The fact that struct methods which allow mutation via methods are poorly handled has led to a proclamation that "mutable structs are evil", even though the problems associated with struct-mutating methods do not occur with exposed struct fields. 允许通过方法进行变异的结构方法处理得很差这一事实导致了一个宣称“可变结构是邪恶的”,尽管与结构变异方法相关的问题不会发生在暴露的结构域中。

Others have noted that the problem in your case is due to the use of a struct property name when the backing field name was required. 其他人已经注意到,在您的情况下,问题是由于在需要支持字段名称时使用结构属性名称。 Certainly correcting that would allow your code to work. 当然,纠正这将使您的代码工作。 I would suggest, however, that the simplest way to avoid such problems is to have the struct use public fields to encapsulate its state. 但是,我建议避免此类问题的最简单方法是让struct使用公共字段来封装其状态。 Doing so would halve the length of your struct definition, allow its semantics to be seen at a glance, and eliminate the possibility that a struct which at a glance looks like it follows your intended pattern actually does something else. 这样做会使结构定义的长度减半,允许一目了然地看到它的语义,并消除一个结构看起来像是按照你想要的模式实际做了其他事情的可能性。

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

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