July 22, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Dumping an Object's State with Reflection and Extension Methods

  • October 17, 2008
  • By Paul Kimmel
  • Send Email »
  • More Articles »

Introduction

Programmers have a lot of problems to manage. Programmers have to manage their time by getting the most out of the least amount of code. Programmers also have to write code that appeals to users in terms of its responsiveness, correctness, and utility. Balancing writing reusable general-purpose code to meet deadlines and budgets with the end users' needs is part of what makes software development challenging.

Knowing how to balance competing tasks is an important skill. Having many tools in your arsenal is the key to success. Look at one side of the coin, writing a general-purpose algorithm for dumping the state of any collection of any objects. You'll use extension methods, a new feature in .NET, to get the job done.

Defining a Test Class

Code that is used often can be dragged into the Toolbox. I use the Customer class for so many demos that it is one of those things. For your purposes, you could use any class, but I have used a simple entity class from the Customers table in the Northwind database (see Listing 1).

Listing 1: A sample customer class.

Public Class Customer

   ''' <summary>
   ''' Initializes a new instance of the Customer class.
   ''' </summary>
   ''' <param name="customerID"></param>
   Public Sub New(ByVal customerID As String)
      FCustomerID   = customerID
      FCompanyName  = "Company " & customerID.ToString()
      FContactName  = "George Contact"
      FContactTitle = "Dr."
   End Sub

   Private FCustomerID As String = ""
   Public Property CustomerID() As String
      Get
         Return FCustomerID
      End Get
      Set(ByVal Value As String)
         FCustomerID = Value
      End Set
   End Property

   Private FCompanyName As String
   Public Property CompanyName() As String
      Get
         Return FCompanyName
      End Get
      Set(ByVal Value As String)
         FCompanyName = Value
      End Set
   End Property

   Private FContactName As String = ""
   Public Property ContactName() As String
      Get
         Return FContactName
      End Get
      Set(ByVal Value As String)
         FContactName = Value
      End Set
   End Property

   Private FContactTitle As String = ""
   Public Property ContactTitle() As String
      Get
         Return FContactTitle
      End Get
      Set(ByVal Value As String)
         FContactTitle = Value
      End Set
   End Property

   Private FAddress As String = ""
   Public Property Address() As String
      Get
         Return FAddress
      End Get
      Set(ByVal Value As String)
         FAddress = Value
      End Set
   End Property

   Private FCity As String = ""
   Public Property City() As String
      Get
         Return FCity
      End Get
      Set(ByVal Value As String)
         FCity = Value
      End Set
   End Property

   Private FRegion As String = ""
   Public Property Region() As String
      Get
         Return FRegion
      End Get
      Set(ByVal Value As String)
         FRegion = Value
      End Set
   End Property

   Private FPostalCode As String = ""
   Public Property PostalCode() As String
      Get
         Return FPostalCode
      End Get
      Set(ByVal Value As String)
         FPostalCode = Value
      End Set
   End Property

   Private FCountry As String = ""
   Public  Property Country() As String
      Get
         Return FCountry
      End Get
      Set(ByVal Value As String)
         FCountry = Value
      End Set
   End Property

   Private FPhone As String = ""
   Public Property Phone() As String
      Get
         Return FPhone
      End Get
      Set(ByVal Value As String)
         FPhone = Value
      End Set
   End Property

   Private FFax As String = ""
   Public Property Fax() As String
      Get
         Return FFax
      End Get
      Set(ByVal Value As String)
         FFax = Value
      End Set
   End Property
End Class




Page 1 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel