getting values from object with reflection ?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a snippet of code that feeds a tree view, the properties show up BUT
if I try and obtain the values of the property the code throws an error.

// throws an excpetion
//property_value = p.GetValue(this,null); //
// the VN .NET code for a similar project has
// property_value = .GetValue(Me, Nothing)
// i

Of course the object would not have values unless this object is
instantiated. So maybe that is a problem too. Please advise here's the
trouble:

objAssembly=System.Reflection.Assembly.LoadFrom(arAssemblyPath );
arOfTypes=objAssembly.GetTypes();
object property_value;

//Get Properties
try
{
PropertyInfo[] pInfo = t.GetProperties() ;
tn2=tn.Nodes.Add("Properties");
foreach(PropertyInfo p in pInfo)
{
if (p.GetIndexParameters().Length==0)
{
// throws an excpetion
// property_value = p.GetValue(this,null); //
// if (property_value ==null)
// {
// Console.WriteLine( "Object has no
value" );
// }
// else
// {
// Console.WriteLine( property_value.ToString() );
// }
}tn2.Nodes.Add(p.ToString());
}
}
 
andrewcw said:
I have a snippet of code that feeds a tree view, the properties show up BUT
if I try and obtain the values of the property the code throws an error.

// throws an excpetion
//property_value = p.GetValue(this,null); //
// the VN .NET code for a similar project has
// property_value = .GetValue(Me, Nothing)
// i

Of course the object would not have values unless this object is
instantiated. So maybe that is a problem too. Please advise here's the
trouble:

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
Thanks for Jon's quick response!

Hi Andrew,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you got an exception when trying to get
the property value using reflection. If there is any misunderstanding,
please feel free to let me know.

Besides posting a short but complete program here, could you also let us
know the exact Exception type and exception message? It will be helpful for
us to troubleshooting on this issue. Thanks!

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Back
Top