May Contain Nuts
Recent Entries 
5th-Nov-2006 10:07 am - Fun and games with reflection
Calvin's Brain, Aaaaaardvark, Sex, fish bicycle, Smiley, Soccer Archery, running with fire, unintended consequences, how awesome I am, Livejournal, Teddy of Borg, kitten crying, 2012, cats and dogs, Shade, slogans, House with a silly face, Hold Me, roleplaying HP, cards of love, lesbian tea, Eightball, The Question is not "Is She Gay?", smoking horse, KittenPenguin, obey, plasticine, Humanity, psychodrama, wikipedia, Attack!, Fight Calvin, Portal!, Eschaton, Needs More Robots, multimedia errors, witch, Animated, Cartoon, circular reasoning, book power, Focus!, Experience, Santa, Monkey in charge, swirly ball of doom!, Join Darth, Back slowly away, obey the penguin, cat chases butterfly, Dr Who, bubble, minifesto, Academically speaking, Unless I'm wrong, south park, Whoa!, time to live, movie review, devil, conspiracy theories, It's a trap!, pickup lines, Exciting, hairy, Cutest Kitten, mononoke thingy, android fisting, Serious, kitty, Sexy, Java, Vaudeville for the next five miles, bullshit detector, Wibbledy Weep, Flying Squirrel, The Hair!, Juggling, Big Grin, headshot, default, screaming hedgehog, Jesus!, dating curve, wanking, Master and Doctor, HP Spoilers, sheldon, ZOMG!, Big Neil, cute, Says Tom, Monkey and Me, lady face, calvin dancing, reaper, Evil Pizza, how big?, livejournal blackout, vulture vomit, Alone without the stupid people, running lego man, STFU says the doctor, Made of Love, Batman goes back to the closet, Find X, sleeping doggy, overwhelming firepower, bombed to freedom, Made of Win, Offensive, whoever invented boredom..., Tentacular, Lack of Pants
I originally wrote this for the c# discussion forum at work, and if you don't use c# you'll be wanting to press the page-down button any second now...

Let's say you want to check whether a control is set to ReadOnly. The simple way is to say :
if(control.ReadOnly) {}

But, I hear you say, not all controls have a ReadOnly property. In fact, only TextBoxBase (and its descendants) have a ReadOnly property. Which is entirely true, most of the time. Not at all true, however, if the control has been subclassed and a ReadOnly property has been added. In which case what would be handy is a way of checking whether a control has a boolean ReadOnly property, and if it does returning it.

Which is where reflection comes in:

private PropertyType GetProperty(Object myObject, string
propertyName)
{
      Type typeInfo = myObject.GetType();
      PropertyInfo propertyInfo = typeInfo.GetProperty(propertyName);
      if (propertyInfo != null)
      {
            object property = propertyInfo.GetValue(myObject, null);
            if (property is PropertyType)
                  return (PropertyType)property;
      }
      return default(PropertyType);
}
which may look complex, but all it's doing is getting the Type Information for your object, checking to see if it has a property with the correct name and checking to see if it matches the correct return type. If it does then you get the information back, if not you get a default value (null for objects, zero for numbers, false for booleans).

You can call it to retrieve the Text property like so:
string title = CheckProperty(myForm, "Text");
or as a replacement for the original if statement at the top:
if(CheckProperty(control, "ReadOnly")) {}

And I wouldn't advise you to use it all the time (there being an overhead for reflection, and it being less clear than straightforward property checking), but it can be very handy on occasion.
This page was loaded May 17th 2008, 5:30 pm GMT.