Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / October 2006

Tip: Looking for answers? Try searching our database.

.NET 2.0 Reflection Problem vs framework 1.1x - how to get the fields of a given Structure in the same order they where declared

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
hugo.braganca@gmail.com - 26 Oct 2006 19:52 GMT
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
Bryan Phillips - 27 Oct 2006 02:57 GMT
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

> Hi,
> I need to iterate the Fields of given Structure though Reflection in
[quoted text clipped - 23 lines]
> Does anyone know how to get the fields ordered at the declared manner ?
> Thanks in advance
hugo.braganca@gmail.com - 27 Oct 2006 11:48 GMT
> 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...

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.