> I could not duplicate the problem, but you might try adding the
> StructLayoutAttribute to the structure:
> Imports System.Runtime.InteropServices
> <StructLayout(LayoutKind.Sequential)> _
> Structure MyStruct
> Dim Play As Byte
> Dim Level As Byte
> Dim Rate As Long
> End Structure
Sorry, I forgot to refer that I'm already using the
<StructLayout(LayoutKind.Sequential)> _ tag above every single
structure and I'm getting the same random behaviour.
After hours of search effort for related information, I've some
comments about..
"(...)
When it came to the order items were being returned from reflection we
couldn't provide a model where a given field would always be in a
specific position in the list, so we moved to a model where the return
order was somewhat randomized over the course of the application and
thus make it more difficult to take dependencies on this.
In the past we haven't don't such a good job documenting best
practices for avoiding these types of issues, but by the time we
release the final version of the NetFX 2.0 we will have more
documentation that will help you avoid these types of problems in the
future.
"
http://devauthority.com/blogs/johnwood/archive/2005/06/29/79.aspx
Jesse Kaplan @Microsoft
Well, the time as come and I'm wondering where is that documentation.
For the next steps I will rename the structures variables in order to
sort them alphabetically with a SortedList object (pos1Play, pos2Level,
pos3Rate) ... what a messy code...
Bryan Phillips - 28 Oct 2006 17:53 GMT
One more idea: If you are the one creating the structure, you could use
an attribute to manually number the structure's fields. Example:
Imports System
Imports System.Reflection
Public Module MyModule
<AttributeUsage((AttributeTargets.Field Or AttributeTargets.Property),
AllowMultiple:=False)> _
Public Class OrdinalAttribute
Inherits Attribute
Private _ordinal As Integer = -1
Public Sub New(ByVal memberOrdinal As Integer)
_ordinal = memberOrdinal
End Sub
Public ReadOnly Property Ordinal() As Integer
Get
Return _ordinal
End Get
End Property
End Class
Structure MyStruct
<Ordinal(0)> _
Dim Play As Byte
<Ordinal(1)> _
Dim Level As Byte
<Ordinal(2)> _
Dim Rate As Long
End Structure
Sub Main
Dim t As Type = GetType(MyStruct)
Dim fieldNames(t.GetFields().Length - 1) As String
For Each info As FieldInfo In t.GetFields()
If info.IsDefined(GetType(OrdinalAttribute), false) Then
Dim a As OrdinalAttribute =
CType(info.GetCustomAttributes(GetType(OrdinalAttribute), false)(0),
OrdinalAttribute)
fieldNames(a.Ordinal) = info.Name
End If
Next
For Each fieldName As String In fieldNames
Console.WriteLine(fieldName)
Next
Console.ReadLine()
End Sub
End Module
Bryan Phillips
MCSD, MCDBA, MCSE
Blog: http://bphillips76.spaces.live.com
> > I could not duplicate the problem, but you might try adding the
> > StructLayoutAttribute to the structure:
[quoted text clipped - 35 lines]
> sort them alphabetically with a SortedList object (pos1Play, pos2Level,
> pos3Rate) ... what a messy code...