有没有办法迭代结构的属性?

时间:2016-07-12 18:27:54

标签: vb.net data-structures

我有一个数据结构,其中包含29种布尔数据类型。有没有办法在For循环中迭代Struct的属性,而不用在普通属性语法中明确地陈述每个属性名称。

这是我的开始,但这不会奏效。

Public Structure ST_PLCStruct_Bools
    Public testTypeNS As Boolean                '1 byte
    Public testTypeOR As Boolean                '1 byte
    Public torqueTypeBreak As Boolean           '1 byte
    Public torqueTypeFix As Boolean             '1 byte
    Public sheaveHigh As Boolean                '1 byte
    Public sheaveLow As Boolean                 '1 byte
    Public directionCW As Boolean               '1 byte
    Public directionCCW As Boolean              '1 byte
    Public cycleStart As Boolean                '1 byte
    Public cycleStarted As Boolean              '1 byte
    Public cycleStop As Boolean                 '1 byte
    Public cycleStopped As Boolean              '1 byte
    Public pneuActuateAuto As Boolean           '1 byte
    Public pneuActuateMan As Boolean            '1 byte
End Structure

Private plcData_Bools As ST_PLCStruct_Bools

For i = 0 To 28
    plcData_Bools(i) = binaryReader.ReadBoolean
Next

感谢。

2 个答案:

答案 0 :(得分:1)

使用反射,您可以使用FieldInfo.SetValue设置值,而无需编写每个字段的名称。由于值类型的装箱,使用结构与类使事件a little复杂化:

Private plcData_Bools As ST_PLCStruct_Bools

Dim boxed As ValueType = plcData_Bools
For Each f In GetType(ST_PLCStruct_Bools).GetFields()
    f.SetValue(boxed, binaryReader.ReadBoolean())
Next
plcData_Bools = DirectCast(boxed, ST_PLCStruct_Bools)

答案 1 :(得分:0)

在32位模式下,布尔值与4个字节对齐

ember.debug.js:32096 TypeError: this.refresh is not a function
at Class.queryParamsDidChange (ember.debug.js:26286)
at Object.triggerEvent (ember.debug.js:28580)
at Object.trigger (ember.debug.js:53473)
at fireQueryParamDidChange (ember.debug.js:52255)
at Object.queryParamsTransition (ember.debug.js:51983)
at Object.getTransitionByIntent (ember.debug.js:51913)
at Object.transitionByIntent (ember.debug.js:52018)
at doTransition (ember.debug.js:52590)
at Object.transitionTo (ember.debug.js:52087)
at Class._doTransition (ember.debug.js:28291)

来源http://www.codeproject.com/Articles/8967/Marshaling-Structures