Home All Groups Group Topic Archive Search About

Using reflection to read the page properties...

Author
21 Feb 2006 1:06 PM
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

Author
21 Feb 2006 1:41 PM
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();

--
Ravi Wallau
nospam@nospam.org
Show quoteHide quote
"Stu" <s.l***@cergis.com> wrote in message
news:uPJQucuNGHA.648@TK2MSFTNGP14.phx.gbl...
> 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
>

Bookmark and Share