How to find out if a Control has a certain property?

H

Helmut Giese

Hello out there,
I am serializing the state of Controls. Now some properties are common
across a wide range but of course there are loads of differences.
I could of course 'switch' on the type and have e.g.
Button: handle Text & TextAlign
Label: handle BorderStyle, Text & TextAlign
Panel: handle BorderStyle
etc.

If on the other hand it were possible to check wether a given Control
posesses a certain property things would be a lot cleaner, like
if (ctrl.HasProperty(Text))
// handle Text & TextAlign for Label & Button
if (ctrl.HasProperty(BorderStyle))
// handle BorderStyle for Label & Panel
etc.

Reflection should make this possible - I feel :)
Any further info would be greatly appreciated.
Best regards
Helmut Giese
 
P

Peter Duniho

Helmut said:
[...]
If on the other hand it were possible to check wether a given Control
posesses a certain property things would be a lot cleaner, like
if (ctrl.HasProperty(Text))
// handle Text & TextAlign for Label & Button
if (ctrl.HasProperty(BorderStyle))
// handle BorderStyle for Label & Panel
etc.

Reflection should make this possible - I feel :)

It does. Have you looked at the documentation? What, specifically, was
unclear in the documentation?

In fact, rather than checking for specific properties, you can simply
enumerate all the public properties and write them all out. That way,
you don't need your code to encapsulate any a priori knowledge of the
Control sub-classes at all.

Pete
 
B

Ben Voigt [C++ MVP]

Helmut Giese said:
Hello out there,
I am serializing the state of Controls. Now some properties are common
across a wide range but of course there are loads of differences.
I could of course 'switch' on the type and have e.g.
Button: handle Text & TextAlign
Label: handle BorderStyle, Text & TextAlign
Panel: handle BorderStyle
etc.

If on the other hand it were possible to check wether a given Control
posesses a certain property things would be a lot cleaner, like
if (ctrl.HasProperty(Text))
// handle Text & TextAlign for Label & Button
if (ctrl.HasProperty(BorderStyle))
// handle BorderStyle for Label & Panel
etc.

Reflection should make this possible - I feel :)
Any further info would be greatly appreciated.

There isn't a separate HasProperty test... it would be insane from an
efficiency perspective. After all, it has to go looking for a matching
property, and you probably want to do something if it exists, which would
mean once you learn it's there, look it up again.

So instead, there's a GetProperty method which returns an array, potentially
empty. So you get both pieces of information -- the existence of the
property, and the descriptor itself.
Best regards
Helmut Giese

__________ Information from ESET NOD32 Antivirus, version of virus
signature database 4512 (20091015) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com

__________ Information from ESET NOD32 Antivirus, version of virus signature database 4512 (20091015) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com
 

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