getting the data type for a class field

L

Larry

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 ImgSubmissionRecord

Public fileName As String

Public thumbnail As Bitmap

Public fileSize As Int64

Public fileDate As DateTime

Public disposition As Int16

End Class

Imports System.Reflection

Public Class debugControl

Inherits System.Web.UI.UserControl

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.

<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

End Sub

Protected WithEvents lblDebug As System.Web.UI.WebControls.Label

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

'Do not delete or move it.

Private designerPlaceholderDeclaration As System.Object

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

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

'Do not modify it using the code editor.

InitializeComponent()

End Sub

#End Region

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

Dim anObject As New ImgSubmissionRecord

Dim MemInfo, memInfoArray() As MemberInfo

Dim aType As Type = anObject.GetType

memInfoArray = aType.GetMembers(BindingFlags.Public Or
BindingFlags.Instance)

For Each MemInfo In memInfoArray

Me.lblDebug.Text = Me.lblDebug.Text + "<br>" + MemInfo.Name + "/" +
System.Type.GetType(MemInfo.GetType.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.Reflection.RuntimeFieldInfo"

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



-Larry
 
S

Scott Allen

Hi Larry:

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

....


memInfoArray = aType.GetMembers(BindingFlags.Public Or
BindingFlags.Instance)

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,
 
L

Larry

Thanks. I'll give that a try.

-Larry

Scott Allen said:
Hi Larry:



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,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top