What if you cannot annotate properties with attributes?

Oct 21, 2011 at 9:05 PM

I understand that this may be out-of-band question regarding this particular project, but I am sure some people here have faced this issue.

So I have a class but I cannot add attributes like [Category("Information")] to its properties. What can I do to still use editors?

My first thought is using Reflection emit to create a new class, using the class that I cannot modify as the template. In the process I would create and attach desired attributes. Once edited, I would have to update changed values from editable to the original class. This looks involved and laborious. I wonder what are better, easier ways to solve this problem? 

Oct 21, 2011 at 9:07 PM

You can still use editors without having attributes on your properties.  Attributes just add additional information about a property.  They are not required.

Oct 21, 2011 at 10:58 PM
Edited Oct 21, 2011 at 11:13 PM
brianlagunas wrote:

You can still use editors without having attributes on your properties.  Attributes just add additional information about a property.  They are not required.

And that is great.

But then I will have to live with display names like "FirstName" instead of "First Name" and I can forget about categories, just for example.

I was wondering if I can somehow have my cake and eat it :)  ?

Like, some extension to PropertyDefinition? Some mapping that can be specified on PropertyGrid? Just random thoughts, as 'playing' with Reflection Emit really doesn't seem like it is going to be much fun.

Oct 22, 2011 at 5:21 AM
Edited Oct 22, 2011 at 5:36 AM

I started digging through my WPF books in hope of finding some magic to help and I actually did find it.

It started from one "Digging Deeper" note in Adam Nathan's book which says that, in the case of data binding to plain .NET properties, as opposed to DPs, WPF uses reflection. However, if it exists, it will use ICustomTypeDescriptor interface. This interface:

http://msdn.microsoft.com/en-us/library/system.componentmodel.icustomtypedescriptor(v=vs.80).aspx

is from .NET 1.x, 2.0 days and I wasn't familiar with it. Turns out it was also used when displaying classes in Windows Forms PropertyGrid and can act as the proxy to any class, exposing its properties in any way user wants. This article by Stephen Toub in MSDN Magazine shows the example where class with fields is made to look like it has properties that Forms PropertyGrid can see (because Forms PropertyGrid doesn't reflect on fields):

http://msdn.microsoft.com/en-us/magazine/cc163816.aspx

So my question is: wouldn't it make sense to leverage this interface also in case of Extended WPF PropertyGrid? 

Oct 23, 2011 at 7:00 PM

Brian,

if you think adding the support for, and thus leveraging ICustomTypeDescriptor, is worthy of consideration, let me know if you need and are willing to accept any help. I can do some coding, prototyping or testing under your direction and guidance, if you wish.  

Oct 23, 2011 at 10:13 PM

The PropertyGrid already checks for ICustomTypeDescriptor.

Oct 24, 2011 at 3:00 PM

Great! I'll try it right away.

Oct 24, 2011 at 8:00 PM

Works like a charm. Thanks Brian for a great product!

Oct 24, 2011 at 8:01 PM

No problem.  You can thank me by rating the project with your impressions.