Home All Groups Group Topic Archive Search About

.NET 2.0 Reflection Problem vs framework 1.1x - how to get the fields of a given Structure in the sa

Author
26 Oct 2006 6:52 PM
hugo.braganca
Hi,
I need to iterate the Fields of given Structure though Reflection in
the same order they where declared.
Note that I only meet the Structure at Runtime.

'ex.:
Structure MyStruct
      Dim Play As Byte
      Dim Level As Byte
      Dim Rate As Long
End Structure

Dim oMyStruct As New MyStruct
Dim Fields As Reflection.FieldInfo() = oMyStruct.GetType().GetFields()

Expected Order:
Fields.Item(0) = Play
Fields.Item(1) = Level
Fields.Item(2) = Rate

I'm using a class that was created based on the above assumption, and I
have the expected behaviour when I use it on the .net framework 1.1x;
but when I use it on the 2.0 framework the order of the fields become
random.

Does anyone know how to get the fields ordered at the declared manner ?
Thanks in advance

Author
27 Oct 2006 1:57 AM
Bryan Phillips
I could not duplicate the problem, but you might try adding the
StructLayoutAttribute to the structure:

Imports System.Runtime.InteropServices

.... snip ...

<StructLayout(LayoutKind.Sequential)> _
Structure MyStruct
      Dim Play As Byte
      Dim Level As Byte
      Dim Rate As Long
End Structure

Bryan Phillips
MCSD, MCDBA, MCSE
Blog:  http://bphillips76.spaces.live.com




Show quote
"hugo.braga***@gmail.com" <hugo.braga***@gmail.com> wrote in message
news:1161888724.344811.226400@h48g2000cwc.googlegroups.com:

> Hi,
> I need to iterate the Fields of given Structure though Reflection in
> the same order they where declared.
> Note that I only meet the Structure at Runtime.
>
> 'ex.:
> Structure MyStruct
>       Dim Play As Byte
>       Dim Level As Byte
>       Dim Rate As Long
> End Structure
>
> Dim oMyStruct As New MyStruct
> Dim Fields As Reflection.FieldInfo() = oMyStruct.GetType().GetFields()
>
> Expected Order:
> Fields.Item(0) = Play
> Fields.Item(1) = Level
> Fields.Item(2) = Rate
>
> I'm using a class that was created based on the above assumption, and I
> have the expected behaviour when I use it on the .net framework 1.1x;
> but when I use it on the 2.0 framework the order of the fields become
> random.
>
> Does anyone know how to get the fields ordered at the declared manner ?
> Thanks in advance
Author
27 Oct 2006 10:48 AM
hugo.braganca
Bryan Phillips escreveu:
> 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...
Author
28 Oct 2006 4:53 PM
Bryan Phillips
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




Show quote
"hugo.braga***@gmail.com" <hugo.braga***@gmail.com> wrote in message
news:1161946093.087733.280400@m7g2000cwm.googlegroups.com:

> Bryan Phillips escreveu:
>
> > 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...

AddThis Social Bookmark Button