How to get property or class member variable value using reflection?

object obj;

 

PropertyInfo pi = obj.GetType().GetProperty("VariableName");

 

YourType value = (YourType)(pi.GetValue(obj, null));


Monday, January 21, 2008 | Comments (5) | Add Comment

Comments

Gravatar

Re:How to get property or class member variable value using reflection?

I need to access a public member of a class using reflection. And then call a method of that member (the member is an instance of another class)

11/3/2008 12:38:13 PM | by Nela
Gravatar

Re:How to get property or class member variable value using reflection?

No comment

11/13/2008 10:01:19 PM | by uday
Gravatar

Re:How to get property or class member variable value using reflection?

I've been looking for the past 2 days for this. Thank you !!!!!

11/30/2008 7:06:14 AM | by Dennis B
Gravatar

Re:How to get property or class member variable value using reflection?

namespace MyNamespace
{
public class MyClass
{
public static string sName;

public static string SName
{
get { return sName; }
set { sName = value; }
}
}

protected void btnAccessProperty_Click(object sender, EventArgs e)
{

MyNamespace.MyClass myCls = new MyNamespace.MyClass();
PropertyInfo pi = myCls.GetType().GetProperty("SName");
string value = (string)(pi.GetValue(null, null));
Response.Write(value);
}

2/22/2010 11:00:08 PM | by Anonymous
Gravatar

Re:How to get property or class member variable value using reflection?

namespace MyNamespace
{
public class MyClass
{
public static string sName;

public static string SName
{
get { return sName; }
set { sName = value; }
}
}

protected void btnAccessProperty_Click(object sender, EventArgs e)
{
MyNamespace.MyClass .SName = "Sheetal";
MyNamespace.MyClass myCls = new MyNamespace.MyClass();
PropertyInfo pi = myCls.GetType().GetProperty("SName");
string value = (string)(pi.GetValue(null, null));
Response.Write(value);
}

2/22/2010 11:02:33 PM | by Anonymous

New Comment

Your Name:
Email (for internal use only):
Subject:
Comment:
 
Code above: