getting values from reflection ( repost )

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

Guest

I am sorry, I am not able to get the reply window open and I had the flu.
The error I get is the Object does not match target type

FTE_PathManager.PathClass vbPATH = new FTE_PathManager.PathClass();
Type tt= vbPATH.GetType();
PropertyInfo[] ppInfo = tt.GetProperties() ;
foreach(PropertyInfo p in ppInfo)
{
if (p.GetIndexParameters().Length==0)
{ Console.WriteLine(p.ToString());
// throws an excpetion
object pObj = new object();
property_value = p.GetValue(pObj,null);

// what should I pass into p.GetValue ??
 
andrewcw said:
I am sorry, I am not able to get the reply window open and I had the flu.
The error I get is the Object does not match target type

FTE_PathManager.PathClass vbPATH = new FTE_PathManager.PathClass();
Type tt= vbPATH.GetType();
PropertyInfo[] ppInfo = tt.GetProperties() ;
foreach(PropertyInfo p in ppInfo)
{
if (p.GetIndexParameters().Length==0)
{ Console.WriteLine(p.ToString());
// throws an excpetion
object pObj = new object();
property_value = p.GetValue(pObj,null);

// what should I pass into p.GetValue ??

The instance of PathClass you want to retrieve values from.

Note that to just get the Type associated with a type, you can use
typeof(...) instead of creating an instance and then calling GetType.
 
Thanks Jom,

It strikes me as odd that I would re-reference the object as
p.GetValue(vbPATH) when p is also from the same object,
but it did work. Thanks again !
 
The issue is you might not have got hold of the type instance from the object itself - you might, for example, have used the typeof operator. The Type instance is simply a view on to that *classes* type information, its not associated with any instance of that class. So when you invoke functionality late bound you have to tell it what instance you'd like it to act upon.

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

Thanks Jom,

It strikes me as odd that I would re-reference the object as
p.GetValue(vbPATH) when p is also from the same object,
but it did work. Thanks again !
 
Back
Top