Wednesday, March 21, 2012

Passing a object to a reflection class

Hello,

I would like to be able to pass an object from my code behind to another class and return a string with all the object properties and values.

IE Book has Book.Title= "The grinch" Book.Author="Tom Jones"

I need to be able to do something like

GetObjectsProperties(book) book is an instance of the class Book

This would returnTitle= "The grinch" Author="Tom Jones"

I undertand I can use System.Reflection for this, however I could not find a good example.


give this a try:

Protected Sub Page_Load(ByVal senderAs Object,ByVal eAs System.EventArgs)Handles Me.Load'one of the page properties we're going to read requires us to validate first... Page.Validate()'just as a sample, we'll pass the current page object to inspect the properties Response.Write(GetProps(Me))End Sub Private Function GetProps(ByVal targetAs Object)As String Dim propInfoArray()As System.Reflection.PropertyInfoDim resultAs New System.Text.StringBuilder'get array of propertyinfo objects for our targets Type propInfoArray = target.GetType.GetProperties'iterate through these propertyinfo objectsFor Each propInfoAs System.Reflection.PropertyInfoIn propInfoArrayDim sAs String Try s = Convert.ToString(propInfo.GetValue(target,Nothing))Catch exAs Exception s ="Error reading property"End Try result.AppendFormat("{0}=""{1}"" ", propInfo.Name, s)Next Return result.ToStringEnd Function

Hey, thanks for your help.

When converting this to C# , one line of code did not work for me. Please advise.

using System;
using System.Text;
using System.Data;
using System.Collections.Generic;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NGB.Mobilization.Util;
using System.Reflection;

public partial class Mobilization_Default2 : NGB.GKO.WebUI.GKOPage
{
protected void Page_Load(object sender, EventArgs e)
{
userLinksControl.UserName = this.CurUser.UserName;

ArrayList excludeColumns = new ArrayList();
excludeColumns.Add("UserId");
Response.Write(GetProps(this.CurUser, excludeColumns));
}

private stringGetProps(Object target, ArrayList excludeColumns)
{
//PropertyInfo propInfoArray = new PropertyInfo();
StringBuilder result = new StringBuilder();
//get array of propertyinfo objects for our targets Type
PropertyInfo[] propInfoArray = target.GetType().GetProperties();
// iterate through these propertyinfo objects

foreach(PropertyInfo propInfo in propInfoArray)
{
// Do Not Add Excluded Columns
if (excludeColumns.Contains(propInfo.Name) == false)
{
string s;

try
{
s = propInfo.GetValue(propInfo, null).ToString();
// s = Convert.ToString(propInfo.GetValue(target, Nothing));
}
catch
{
// na
s = "Error reading property";
}
result.AppendFormat("<b>{0}</b> = {1} ", propInfo.Name, s);
}
}

return result.ToString();

}

This prints:

UserName = Error reading propertyFirstName = Error reading propertyLastName = Error reading propertyEmail = Error reading propertyState = Error reading propertyBranch = Error reading propertyDebug = Error reading property

Thanks for your helpmbanavige!



}

change it to this:

s = propInfo.GetValue(target, null).ToString();


awesome it worked. thanks man!

I'm going to make this a "helper" function for my project.

i'll post the code when it's complete

0 comments:

Post a Comment