Using reflection to read the page properties...

S

Stu

Hi,

I am trying to write a detailed logging system for a site. The client
basically wants us to save the values of the properties on the page every
time someone presses a save button.

I have written the following function that formats the properties of a basic
class and rreturns it as a formatted string - but it does not work when I
try and pass it the Page class - it displays the error 'Object not set to an
instance....']

Anybody any ideas how I should do this?

Thanks in advance,

Stu

--/ snip /--

'THE PAGE
Public Class Test
Inherits System.Web.UI.Page

private m_Test as string="My test text"

Public Property Test1() As String
Get
Test1 = m_Test1
End Get
Set(ByVal Value As String)
m_Test1 = Value
End Set
End Property

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim log As New clsLogAction
Dim log As New clsLogAction
Response.Write(log.LogAction(Me))
End Sub
End Class

'The Log class
Class LogAction
Public Function returnValues(ByVal obj As Object) As String
Dim sb As New System.Text.StringBuilder
For Each iField As System.Reflection.FieldInfo In
obj.GetType.GetFields(BindingFlags.NonPublic Or BindingFlags.Instance)
sb.Append(iField.Name.ToString + "|" +
iField.GetValue(obj).ToString + vbCrLf)
Next
Return sb.ToString
End Function
End Class
 
R

Ravi Ambros Wallau

Errors:
1. For Each iField As System.Reflection.FieldInfo In
obj.GetType.GetFields(BindingFlags.NonPublic Or BindingFlags.Instance)
Call the method that enumerate the fields FIRST, and not on ITERATION;

2. iField.GetValue(obj).ToString + vbCrLf) > The value obtained is probally
null; Check it before calling ToString();
 

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

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top