Referencing object properties

  • Thread starter Thread starter michelqa
  • Start date Start date
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...
 
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();
 
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();
}
 
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.
 
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!
 
"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
 
"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.
 
Back
Top