Referencing object properties

M

michelqa

Hi,

I'm wondering what is the correct way to build a function to retrieve
properties from an object.

Ex:
public string GetControlProperty(string PropertyName)
{
// this function must return the specified propertyName from the
control object.
int Hwnd=66666; // handle is already set somewhere else ;o)
ctl = Control.FromHandle(Hwnd);
return ctl.[*PropertyName*];
}

Calling example :
GetControlProperty("Text") will return the text of the object.

Note : in my example the property returned is always a string but this
will not be the case.

Thanks for any help...
 
A

Anthony Jones

michelqa said:
Hi,

I'm wondering what is the correct way to build a function to retrieve
properties from an object.

Ex:
public string GetControlProperty(string PropertyName)
{
// this function must return the specified propertyName from the
control object.
int Hwnd=66666; // handle is already set somewhere else ;o)
ctl = Control.FromHandle(Hwnd);
return ctl.[*PropertyName*];
}

Calling example :
GetControlProperty("Text") will return the text of the object.

Note : in my example the property returned is always a string but this
will not be the case.

Thanks for any help...

Type t = typeof(ctl);

return t.GetProperty(PropertyName).GetValue(ctl, null).ToString();
 
J

Jeff Johnson

I'm wondering what is the correct way to build a function to retrieve
properties from an object.

Ex:
public string GetControlProperty(string PropertyName)
{
// this function must return the specified propertyName from the
control object.
int Hwnd=66666; // handle is already set somewhere else ;o)
ctl = Control.FromHandle(Hwnd);
return ctl.[*PropertyName*];
}

Calling example :
GetControlProperty("Text") will return the text of the object.

Note : in my example the property returned is always a string but this
will not be the case.

Similar to an answer I just posted for tshad (who wanted to SET property
values), try this:

public string GetControlProperty(string PropertyName)
{
// this function must return the specified propertyName from the control
object.
int Hwnd=66666; // handle is already set somewhere else ;o)
ctl = Control.FromHandle(Hwnd);
return ctl.GetType().InvokeMember(PropertyName, BindingFlags.Instance |
BindingFlags.Public | BindingFlags.GetProperty, null, ctl,
null).ToString();
}
 
M

michelqa

I'm wondering what is the correct way to build a function to retrieve
properties from an object.
Ex:
public string GetControlProperty(string PropertyName)
{
   // this function must return the specified propertyName from the
control object.
   int Hwnd=66666;   //  handle is already set somewhere else;o)
   ctl = Control.FromHandle(Hwnd);
   return ctl.[*PropertyName*];
}
Calling example :
GetControlProperty("Text") will return the text of the object.
Note : in my example the property returned is always a string but this
will not be the case.
Thanks for any help...

Type t = typeof(ctl);

return t.GetProperty(PropertyName).GetValue(ctl, null).ToString();


Thanks...that is exactly what I need :) Thanks also to Jeff for his
alternative solution.
 
M

michelqa

Is there any trick for getting properties like "Location.X" ??

The Jeff's solution return an error "method not found" when
propertyName is like "Location.X"

Thanks!
 
M

michelqa

"Location.X" is not a property name.  It's two properties.  One named"X",  
found in the type returned by another named "Location".

(This assumes, of course, you are referring to a property "Location" such 
as that found in the Control class, type Point).

Pete

Ok so there is no short way to do that? the only solution I have now
is to split the string by the "." character to get location and then
the X

Yes it's a Control class, Type point
 
A

Anthony Jones

"Location.X" is not a property name. It's two properties. One named "X",
found in the type returned by another named "Location".

(This assumes, of course, you are referring to a property "Location" such
as that found in the Control class, type Point).

Pete
Ok so there is no short way to do that? the only solution I have now
is to split the string by the "." character to get location and then
the X

Yes it's a Control class, Type point
<<<<<<<<<<<<<

There are no short-cuts in the language and there are no property path
parsing APIs in the framework. Hence you do need to do this yourself with
split etc.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top