PropertyDescriptor & PropertyInfo

PropertyDescriptor & PropertyInfo

Old forum URL: forums.lhotka.net/forums/t/5455.aspx


Justinraff posted on Wednesday, September 24, 2008

Hello,

This post is a bit of a sanity check before I begin tinkering with the CSLA framework. My ultimate goal is this: To programatically define PropertyDescriptor attributes for each property exposed by a CSLA class.

Now, with the addition of the PropertyInfo class in the CSLA framework we have a PropertyInfo.FriendlyName property which is great. But unfortunately this isn't exposed through the PropertyDescriptor.DisplayName attribute - which would be perfection! I see another unanswered post on the forum asking about this from back in May (http://forums.lhotka.net/forums/thread/22599.aspx).

My idea is then to extend the PropertyInfo class to include Category and Description, as well as the existing properties - and then to expose these as PropertyDescriptor attributes.

Is this a reasonable or feasible suggestion?

Regards,
Justin Rafferty.

RockfordLhotka replied on Wednesday, September 24, 2008

Yes, though you don't need to change CSLA to do this. You can create a subclass of PropertyInfo<T> and just use your subclass instead of the CSLA class in all your RegisterProperty() calls:

private static MyPropertyInfo<string> =
  RegisterProperty(new MyPropertyInfo<string>(...));

This is an intentional extensibility technique, allowing you to attach extra metadata to properties.

Justinraff replied on Wednesday, September 24, 2008

Wow, amazingly fast reply - thanks Rocky. So that's the best way to extend the PropertyInfo class explained.. very simple.

Now, I would have thought that exposing the attribute might have been as easy as:

[DisplayName(EmployeeIDProperty.FriendlyName)]
public int EmployeeID
{
get { return GetProperty(EmployeeIDProperty); }
set { SetProperty(EmployeeIDProperty, value); }
}

But that doesn't seem to work. Sorry if this is a dumb .NET question opposed to something CSLA related.

RockfordLhotka replied on Wednesday, September 24, 2008

I don't think .NET allows you to specify attribute parameters like that.

But it occurs to me that maybe you could go the other way, which would tie into this thread too
http://forums.lhotka.net/forums/thread/22599.aspx

Just thinking off the top of my head, but:

[DisplayName("Employee id")]
public int EmployeeId
...

Then have your subclass of PropertyInfo<T>, in its constructor, go get the display name using the PropertyDescriptor, and set the FriendlyName value using the display name.

This would incur the reflection overhead of using PropertyDescriptor when the PropertyInfo<T> object is created - but that happens just once per appdomain, so it shouldn't be that big a deal.

Justinraff replied on Wednesday, September 24, 2008

The problem with going in that direction is that attributes cannot be set at runtime whereas PropertyInfo lends itself to that very well.

Copyright (c) Marimer LLC