Using Reflection to Assign HttpContent to Class Properties

In my continuing discussion about implementing Multi-part Form AJAX submission I have a need to talk about how I bound the HttpContent to a strongly typed class. One way you could do it is by taking the HttpContent and looping through it and adding them to a class. The problem with this approach is that if you send a form element that you have not create a class property for then you will get an error. The way to prevent this is by taking your class properties and looping through them and find the corresponding item in the HttpContent (Using Reflection). This way if more elements are sent then you would just skip them.

After all if they are not in the class then you probably aren’t going to use them. For the full example in how this is used check out this article on posting multi-part form using AJAX.

	Dim cp = New Applications.General

	'Reads each property and tries to assign a the value to it
	'From the HttpContent.
	'There might be an easier way to bind the form data values to a class but
          'this makes prevents thrown errors.
	'See article called (assigning Multi-part Data elements to a strongly typed class)
	For Each prop In GetType(Applications.General).GetProperties()
	   Dim queryParam = provider.FormData.GetValues(prop.Name)
	   If queryParam IsNot Nothing Then
		   prop.SetValue(cp, queryParam(0), Nothing)
	   End If
	Next

. . . .

Namespace Applications

    Public Class General
        Public Property FirstName As String
        Public Property LastName As String
        Public Property Address1 As String
        Public Property Address2 As String
        Public Property City As String
        Public Property State As String
        Public Property ZipCode As String
        Public Property PhoneCell As String
        Public Property PhoneHome As String
        Public Property Email As String
        Public Property Available As String
        Public Property Salary As String
        Public Property CurrentEmployed As Boolean
        Public Property HighSchool As String
        Public Property College As String
        Public Property Training As String
        Public Property ResumeDoc As String
    End Class
End Namespace

In closing, you can use this type for most anything and once you get it into the class properties you now have a strongly typed object. Happy Coding…

Looking for quality web hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , , , , , , , | Posted under ASP .NET 4.0, Programming/Coding | RSS 2.0

Author Spotlight

David Bauernschmidt

David Bauernschmidt

I live in the historical triangle of Virginia where I am married with two daughters. I have spent over 13 years working for a Fortune 500 company in the computer area. I started in VB 6.0 and by the time I ended my employment I was supervising a development team where we built many web applications. When my first daughter was born I wanted to spend more time with her so I left and became a programmer analyst for local government as well as launch my own company. Since then I have grown James River Webs into a profitable web design and application company helping small businesses create a big presence on the internet. As an employee I have created web application used by citizens and other companies. I enjoy fly fishing, and spending time with my family. I also enjoy learning new approaches and development tools when it comes to developing applications.

Leave a Reply

Your email address will not be published. Required fields are marked *


8 − = 2

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>