Introduction
I write about real problems that real programmers have, and in so doing I try to show you, the reader, alternate solutions that provide you with options. Hearing back from you that the article and solution helped makes it worth writing.
All of us use Reflection implicitly when objects are serialized. Many use Reflection explicitly to do things like deep object copies or dumping object state. The upside of Reflection is it that it supports writing a general purpose algorithm, albeit an underperforming one. In this article, you will learn to write a general purpose emitter that you need write only one time, and the emitter outputs a specific-purpose algorithm. In this example, the algorithm emits code that dumps the object state for a specific list of objects. Pass in the list of objects and the emitter writes the equivalent of a property-by-property state dump. A side benefit of working at this level, besides writing highly performant code, is that you can learn a little more about how .NET works under the hood.
Here Is the Sample Main Subroutine…
The sample Main subroutine is listed here without much fanfare. It is very simple but you need it to try the emitter. Listing 2 contains the definition of the Customer class.
Listing 1: A sample Main loop that invokes and tests the emitter.
Module Module1 Sub Main() ' Create customer list Dim customers As List(Of Customer) = New List(Of Customer) For i As Integer = 0 To 50 customers.Add(New Customer(i.ToString())) Next DynamicAssemblyCreator.CreateAssembly(customers) Console.ReadLine() End Sub End Module
Listing 2: The Customer class represents a custom coded entity that mirrors the Northwind.Customers table.
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
You will need Main and the Customer class to try the demo. You can just copy and paste them, but if you are just getting started writing the code yourself will provide you with some good practice.