|
.net
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Using reflection to read the page properties...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 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(); -- Show quoteHide quoteRavi Wallau nospam@nospam.org "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 >
Other interesting topics
PublishState.xml
Need help with GridView and LDAP/GC sorting xml data in server control Clarification on XML schema ????? problem dynamically removing items from a droplist..? Register script block from app_code daab and mysql Default values in a FormView App Directory Problem with ASP.NET Development Server |
|||||||||||||||||||||||