|The World According to Nick|
|My take on Software, Technology, Politics, and anything else I feel like talking about.|
Monday, March 01, 2004
Here's a C# tip that came out of some thoughts I had after reading an article on Code Guru on how to create properties and delegates in managed C++ called Why Don't I Get Those Keywords? What's interesting about the syntax for defining properties is that you are actually creating two functions with this general form:
Obviously, type should be replaces with int, string, or whatever the type of the property is... and MyProperty is a generic name for whatever you want to call your property.
One of the nice things you can do in .NET, is create a delegate that is basically a type safe function pointer. But can you create a delegate which is a pointer to a property, instead of a method? At first I thought it would be nice if you could have one delegate that would somehow wrap up both the property get and set in one... but that is not possible. But if you can live with having two delegates for a property, then the syntax for doing that in C++ would be relatively straight forward... following this form:
The problem is when you try to do this in C#. C# has a completely different syntax for defining properties, that makes the above method impossible. So does that mean that you can't do this in C#... nope. It just means you need to use a little reflection to gain access to the underlying get and set functions that do exist under the hood:
Home: Wauwatosa, WI, United States
I'm a Software Consultant in the Milwaukee area. Among various geeky pursuits, I'm also an amateur triathlete, and enjoy rock climbing. I also like to think I'm a political pundit.
View My Profile
Previous PostsCodeGuru has a New Look
Rethinking Free Software
Redefining Low and High when it comes to Unemploym...
They Are Illegal... and They Are Aliens
Why Isn't Anyone Asking Her to Resign?
Guarantee This Won't Change... I'm Sorry... I cons...
O'Reilly Launches WindowsDevCenter.com
I'm Voting for Bush Because Kerry Says He'll Creat...
I Don't Dream of Jeannie... or Why I Hate Wizards
When John Kerry's Courage Went M.I.A.
Personal LinksCarnival of the Badger
The Coding Monkey
Blog Critics Reviews
Design By maystar