Reflection on instance returned from W/Service

  • Thread starter Thread starter Angelos Karantzalis
  • Start date Start date
A

Angelos Karantzalis

Hi y'all ...

I've just come across a rather weird problem.

i'm using the following code to get the names of the public properties of an
object:

public static string[] GetPropertyNames(object instance){
Type objType = instance.GetType();
PropertyInfo[] props = objType.GetProperties();
string[] strPropNames = new string[props.Length];
for(int i=0; i<props.Length; i++)
strPropNames = props.Name;
return strPropNames;
}

... that worked fine on framework class instances, and it also worked on my
own classes, even after I'd loaded an assembly dynamically from the file
system.

HOWEVER, now I'm using it to query an instance of a class returned from a
web service. I know that the class has several public properties [duh! I
wrote it !], but the code above doesn't work. It returns a zero-length
string array. A similar function that returns the method names, returns the
classic "object" methods, which is again not correct [quering for the method
names should also return the "get_<public property name>" methods for each
public property ... ]

Does anyone have a clue on what might be happening here ??? Did i mention
that querying the Type name of the instance returns the class name in the
namespace created by VS for the Web Reference ? [ as it should ]

Cheers,
Angel
O:]
 
Angelos Karantzalis said:
I've just come across a rather weird problem.

i'm using the following code to get the names of the public properties of an
object:

public static string[] GetPropertyNames(object instance){
Type objType = instance.GetType();
PropertyInfo[] props = objType.GetProperties();
string[] strPropNames = new string[props.Length];
for(int i=0; i<props.Length; i++)
strPropNames = props.Name;
return strPropNames;
}

.. that worked fine on framework class instances, and it also worked on my
own classes, even after I'd loaded an assembly dynamically from the file
system.

HOWEVER, now I'm using it to query an instance of a class returned from a
web service. I know that the class has several public properties [duh! I
wrote it !], but the code above doesn't work. It returns a zero-length
string array. A similar function that returns the method names, returns the
classic "object" methods, which is again not correct [quering for the method
names should also return the "get_<public property name>" methods for each
public property ... ]

Does anyone have a clue on what might be happening here ??? Did i mention
that querying the Type name of the instance returns the class name in the
namespace created by VS for the Web Reference ? [ as it should ]


I *believe* that types returned by web services don't have properties -
they just have public fields. Look at the code VS generated for you for
the type to check.
 
The objects created by Visual Studio doesn't have properties, it only has
public fields.
You'll either have to change your reflection code to access these fields or
you will have create your own proxy using the WSDL tool and then change the
code to use properties instead.

Anders Norås
blog: http://dotnetjunkies.com/weblog/anoras
 
god .. how could I be so stupid & not check for public fields ... ? That was
it allright ...

Thanks a milion guys, i guess sometimes technology IS a substitute for
stupidity :D

O:]

Anders Norås said:
The objects created by Visual Studio doesn't have properties, it only has
public fields.
You'll either have to change your reflection code to access these fields or
you will have create your own proxy using the WSDL tool and then change the
code to use properties instead.

Anders Norås
blog: http://dotnetjunkies.com/weblog/anoras

Angelos Karantzalis said:
Hi y'all ...

I've just come across a rather weird problem.

i'm using the following code to get the names of the public properties of
an
object:

public static string[] GetPropertyNames(object instance){
Type objType = instance.GetType();
PropertyInfo[] props = objType.GetProperties();
string[] strPropNames = new string[props.Length];
for(int i=0; i<props.Length; i++)
strPropNames = props.Name;
return strPropNames;
}

.. that worked fine on framework class instances, and it also worked on my
own classes, even after I'd loaded an assembly dynamically from the file
system.

HOWEVER, now I'm using it to query an instance of a class returned from a
web service. I know that the class has several public properties [duh! I
wrote it !], but the code above doesn't work. It returns a zero-length
string array. A similar function that returns the method names, returns
the
classic "object" methods, which is again not correct [quering for the
method
names should also return the "get_<public property name>" methods for each
public property ... ]

Does anyone have a clue on what might be happening here ??? Did i mention
that querying the Type name of the instance returns the class name in the
namespace created by VS for the Web Reference ? [ as it should ]

Cheers,
Angel
O:]

 
Back
Top