getting the data type for a class field

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Larry

    getting the data type for a class field

    I'm having trouble finding the right method to determine at runtime the data
    type of the fields in a class.

    here's what I've got so far:
    Public Class ImgSubmissionRe cord

    Public fileName As String

    Public thumbnail As Bitmap

    Public fileSize As Int64

    Public fileDate As DateTime

    Public disposition As Int16

    End Class

    Imports System.Reflecti on

    Public Class debugControl

    Inherits System.Web.UI.U serControl

    #Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.

    <System.Diagnos tics.DebuggerSt epThrough()> Private Sub InitializeCompo nent()

    End Sub

    Protected WithEvents lblDebug As System.Web.UI.W ebControls.Labe l

    'NOTE: The following placeholder declaration is required by the Web Form
    Designer.

    'Do not delete or move it.

    Private designerPlaceho lderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
    System.EventArg s) Handles MyBase.Init

    'CODEGEN: This method call is required by the Web Form Designer

    'Do not modify it using the code editor.

    InitializeCompo nent()

    End Sub

    #End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArg s) Handles MyBase.Load

    Dim anObject As New ImgSubmissionRe cord

    Dim MemInfo, memInfoArray() As MemberInfo

    Dim aType As Type = anObject.GetTyp e

    memInfoArray = aType.GetMember s(BindingFlags. Public Or
    BindingFlags.In stance)

    For Each MemInfo In memInfoArray

    Me.lblDebug.Tex t = Me.lblDebug.Tex t + "<br>" + MemInfo.Name + "/" +
    System.Type.Get Type(MemInfo.Ge tType.ToString) .ToString

    Next

    End Sub

    End Class

    I get the name for each member of the class ok, but for the type I get

    "System.Reflect ion.RuntimeFiel dInfo"

    instead of string or int16, or bitmap, or whatever type the member is. How
    do I get this information at runtime?



    -Larry






  • Scott Allen

    #2
    Re: getting the data type for a class field

    Hi Larry:

    On Wed, 5 Oct 2005 21:59:08 -0700, "Larry" <hughes00@comca st.net>
    wrote:
    [color=blue]
    >I'm having trouble finding the right method to determine at runtime the data
    >type of the fields in a class.
    >[/color]

    ....
    [color=blue]
    >
    >memInfoArray = aType.GetMember s(BindingFlags. Public Or
    >BindingFlags.I nstance)
    >[/color]

    The problem is GetMembers will return fields and methods (all
    members). Since methods don't have a type the MemberInfo objects don't
    have a property to tell you the type of the member.

    What you really want to use is GetFields, which returns an array of
    FieldInfo. A FieldInfo object has a FieldType property, which can tell
    you the type of the field (String, Int32, Hashtable, etc).

    HTH,

    --
    Scott

    Comment

    • Larry

      #3
      Re: getting the data type for a class field

      Thanks. I'll give that a try.

      -Larry

      "Scott Allen" <scott@nospam.o detocode.com> wrote in message
      news:9r9ak1hs68 ti5btlnmn3cv72s jinmje5f6@4ax.c om...[color=blue]
      > Hi Larry:
      >
      > On Wed, 5 Oct 2005 21:59:08 -0700, "Larry" <hughes00@comca st.net>
      > wrote:
      >[color=green]
      >>I'm having trouble finding the right method to determine at runtime the
      >>data
      >>type of the fields in a class.
      >>[/color]
      >
      > ...
      >[color=green]
      >>
      >>memInfoArra y = aType.GetMember s(BindingFlags. Public Or
      >>BindingFlags. Instance)
      >>[/color]
      >
      > The problem is GetMembers will return fields and methods (all
      > members). Since methods don't have a type the MemberInfo objects don't
      > have a property to tell you the type of the member.
      >
      > What you really want to use is GetFields, which returns an array of
      > FieldInfo. A FieldInfo object has a FieldType property, which can tell
      > you the type of the field (String, Int32, Hashtable, etc).
      >
      > HTH,
      >
      > --
      > Scott
      > http://www.OdeToCode.com/blogs/scott/[/color]


      Comment

      Working...