A little reflection helper

These are 2 classes that I use for control objects properties by reflection...

using System;

using System.Collections.Generic;

using System.Text;

namespace Utils

{

public class ReflectionHelper

{

public static string GetStringPropertyValue(object obj,string property)

{

try{ return obj.GetType().GetProperty(property).GetValue(obj, null).ToString();

}catch(Exception){

return null;

}

}

public static object GetObjectPropertyValue(object obj, string property)

{

return obj.GetType().GetProperty(property).GetValue(obj, null);

}

public static int GetIntPropertyValue(object obj, string property)

{

object tmp= obj.GetType().GetProperty(property).GetValue(obj, null);

int val=int.MinValue;

if(tmp!=null)

int.TryParse(tmp.ToString(), out val);

return val;

}

}

public class ReflectionHelper<T>

{

public static T GetPropertyValue(object obj, string property)

{

return (T)obj.GetType().GetProperty(property).GetValue(obj, null);

}

}

}

Antonio

Published 20 February 2008 02:12 PM by antrad
Filed under:

Comments

No Comments

This site

Search

Go

This Blog

Syndication

Sponsors

  • MaximumASP
  • Social Bookmarking
    Online Shopping
    asp.net hosting
    UK online local dating