14

I want to add some custom PropertyGrid-centric Attributes to the object's properties, to provide richer editing, hide some values and group them in categories, because that class I'm working with doesn't provide such functionality and I can't do anything about it.

Really, it's for MS's Application Settings that generates code, so you can't extend it in any way property-wise. See my other question: Runtime AppSettings.settings editor dialog

Community
  • 1
  • 1
Gman
  • 1,781
  • 1
  • 23
  • 38

4 Answers4

34

Unlike others have suggested, it's quite possible, and also not that hard. For example, you want to add some new attributes to some properties, which you can select at runtime based on some criteria.

There're two helper classes we'll need to implement this.

First goes PropertyOverridingTypeDescriptor, it allows us to supply our own property descriptors for some properties, while keeping others intact:

public class PropertyOverridingTypeDescriptor : CustomTypeDescriptor
    {
        private readonly Dictionary<string, PropertyDescriptor> overridePds = new Dictionary<string, PropertyDescriptor>();

        public PropertyOverridingTypeDescriptor(ICustomTypeDescriptor parent)
            : base(parent)
        { }

        public void OverrideProperty(PropertyDescriptor pd)
        {
            overridePds[pd.Name] = pd;
        }

        public override object GetPropertyOwner(PropertyDescriptor pd)
        {
            object o = base.GetPropertyOwner(pd);

            if (o == null)
            {
                return this;
            }

            return o;
        }

        public PropertyDescriptorCollection GetPropertiesImpl(PropertyDescriptorCollection pdc)
        {
            List<PropertyDescriptor> pdl = new List<PropertyDescriptor>(pdc.Count+1);

            foreach (PropertyDescriptor pd in pdc)
            {
                if (overridePds.ContainsKey(pd.Name))
                {
                    pdl.Add(overridePds[pd.Name]);
                }
                else
                {
                    pdl.Add(pd);
                }
            }

            PropertyDescriptorCollection ret = new PropertyDescriptorCollection(pdl.ToArray());

            return ret;
        }

        public override PropertyDescriptorCollection GetProperties()
        {
            return GetPropertiesImpl(base.GetProperties());
        }
        public override PropertyDescriptorCollection GetProperties(Attribute[] attributes)
        {
            return GetPropertiesImpl(base.GetProperties(attributes));
        }
    }

Few remarks:

  • Constructor takes ICustomTypeDescriptor, no worries here, we can get one for any type or it's instance with the TypeDescriptor.GetProvider(_settings).GetTypeDescriptor(_settings) where _settings can be either Type or object of that type.
  • OverrideProperty does just what we need, more on it later.

The other class we need is the TypeDescriptionProvider that will return our custom type descriptor instead of the default one. Here it is:

public class TypeDescriptorOverridingProvider : TypeDescriptionProvider
    {
        private readonly ICustomTypeDescriptor ctd;

        public TypeDescriptorOverridingProvider(ICustomTypeDescriptor ctd)
        {
            this.ctd = ctd;
        }

        public override ICustomTypeDescriptor GetTypeDescriptor (Type objectType, object instance)
        {
            return ctd;
        }
    }

Fairly simple: you just supply the type descriptor instance on construction and here you go.

And finally, processing code. For example, we want all properties ending with ConnectionString in our object (or type) _settings to be editable with the System.Web.UI.Design.ConnectionStringEditor. To achieve that, we can use this code:

// prepare our property overriding type descriptor
PropertyOverridingTypeDescriptor ctd = new PropertyOverridingTypeDescriptor(TypeDescriptor.GetProvider(_settings).GetTypeDescriptor(_settings));

// iterate through properies in the supplied object/type
foreach (PropertyDescriptor pd in TypeDescriptor.GetProperties(_settings))
{
    // for every property that complies to our criteria
    if (pd.Name.EndsWith("ConnectionString"))
    {
        // we first construct the custom PropertyDescriptor with the TypeDescriptor's
        // built-in capabilities
        PropertyDescriptor pd2 =
            TypeDescriptor.CreateProperty(
                _settings.GetType(), // or just _settings, if it's already a type
                pd, // base property descriptor to which we want to add attributes
                    // The PropertyDescriptor which we'll get will just wrap that
                    // base one returning attributes we need.
                new EditorAttribute( // the attribute in question
                    typeof (System.Web.UI.Design.ConnectionStringEditor),
                    typeof (System.Drawing.Design.UITypeEditor)
                )
                // this method really can take as many attributes as you like,
                // not just one
            );

        // and then we tell our new PropertyOverridingTypeDescriptor to override that property
        ctd.OverrideProperty(pd2);
    }
}

// then we add new descriptor provider that will return our descriptor instead of default
TypeDescriptor.AddProvider(new TypeDescriptorOverridingProvider(ctd), _settings);

That's it, now all properties ending with ConnectionString will be editable through ConnectionStringEditor.

As you can see, we just override some functionality of the default implementation every time, so the system should be fairly stable and behave as expected.

Andrew Walker
  • 40,984
  • 8
  • 62
  • 84
Gman
  • 1,781
  • 1
  • 23
  • 38
  • I'm using your logic in a slightly different way: I'm trying to add a `DisplayName` attribute on a property at runtime. The creation logic you wrote seems to work fine, but the transformation of this attribute into metadata does not seem to happen. I created a `CustomModelMetadataProvider` and when the `CreateMetadata()` function is called for this property, my `DisplayName` attributes is not in the attributes list nor is the metadata set. I'm not sure what I'm missing here... – Timothée Bourguignon Sep 20 '12 at 09:04
  • Hello! I didn't really understand, what's different about your case. You just need to change `EditorAttribute` to `DisplayNameAttribute` in the last multiline code example. I've checked, it works for me. Don't know anything about Metadata, what do you need it for? – Gman Sep 20 '12 at 12:24
  • The difference is that I'm not using a PropertyGrid ; but as far as I can tell, it shouldn't make a difference. Here's the problem I'm trying to solve: [How to add MetaData to a dynamically build MVC3 ViewModel?](http://stackoverflow.com/questions/12426620/how-to-add-metadata-to-a-dynamically-build-mvc3-viewmodel) – Timothée Bourguignon Sep 20 '12 at 13:30
  • 1
    Hi. In the last line of the third code block (beginning `TypeDescriptor.AddProvider`), you use a two parameter constructor for `TypeDescriptorOverridingProvider`, yet in the second code block you only wrote a one parameter constructor. – Colonel Panic Sep 25 '12 at 12:56
  • 1
    @Colonel Panic thank you, I've fixed this. It's just the second one that's needed. _settings' type is not required. – Gman Sep 25 '12 at 16:15
  • @Tim Bourguignon sorry, can't help you, I'm too far from asp, mvc3. – Gman Sep 25 '12 at 16:17
  • I've followed all your steps and if I use Reflection to check the existence of the new attribute it exists. But with LINQ to SQL doesn't call TypeDescriptorOverridingProvider. Any idea? – lorddarkangel Jun 17 '13 at 11:50
  • @lorddarkangel could you please post the code? Maybe even create a question, 'coz comments aren't really suited for code posting :) – Gman Jun 18 '13 at 06:47
  • I've posted another question with my problem: http://stackoverflow.com/questions/17164112/how-to-add-property-level-attribute-to-map-columnattribute – lorddarkangel Jun 18 '13 at 08:32
  • 2
    FWIW, this solution will only work if there are no derived types defined, since the `TypeDescriptor` will work for all derived classes, but will return only the properties in the base class. I've worked it out finding all subclasses and adding their own typedescriptorprovider for each... probably a bit overkill, but doesn't seem to slow down design-time, and I personally don't need this for runtime. – Jcl Dec 29 '15 at 16:51
  • There was a rejected edit to this post with comment: In TypeDescriptorOverridingProvider, return the base TypeDescriptor when a null instance is passed into GetTypeDescriptor, otherwise only properties of parent classes (and none of the child class' properties) will be retrieved when GetTypeDescriptor is called for a child class. The edit replaced a line `return ctd;` with `return instance == null ? base.GetTypeDescriptor(objectType, instance) : ctd;`. Can't check if it's valid or not, but wanted to let readers know. Of course, I've set this post to community wiki mode to allow for editslikeit – Gman Mar 23 '16 at 10:00
  • @Jcl do you remember how you did that? I've hit the same problem (child classes not showing their properties, only the parent ones). I tried explicitly specifying the TypeDescriptors for all sub-classes both before and after I declare this custom one but it doesn't seem to work. – Peter Sandor Aug 30 '23 at 15:33
  • @PeterSandor very old, but IIRC, I did it by finding (via reflection) all derived classes and adding their own TypeDescriptor with all properties of that class (as I said in my comment, bit overkill but it did work for my needs) – Jcl Sep 01 '23 at 17:37
2

If you want rich custom PropertyGrid, an alternative design is to make your wrap your type in a class inheriting from CustomTypeDescriptor. You can then override GetProperties, annotating the properties of the underlying class with the attributes needed for PropertyGrid.

Detailed description in answer to a related question https://stackoverflow.com/a/12586865/284795

Community
  • 1
  • 1
Colonel Panic
  • 132,665
  • 89
  • 401
  • 465
2

The accepted answer does work, but it has a flaw: if you assign the provider to a base class, it'll also work for derived classes, however, since the PropertyOverridingTypeDescriptor parent (from which it'll get its properties) is for the base type, the derived type will only find the base class properties. This causes havok in e.g., the winforms designer (and may cause you to lose data if you are using the TypeDescriptor for serializing the data).

Just for the record, I've made a generic solution based on @Gman's answer, and I've posted it here as a solution to my own question (which was a different question, although the solution worked using this one).

Community
  • 1
  • 1
Jcl
  • 27,696
  • 5
  • 61
  • 92
1

If you need to add attributes like [ExpandableObject] or [Editor] to properties of an object which class you can't edit you can add the attributes to the type of the property. So you can use reflection to inspect the object and use

TypeDescriptor.AddAttributes(typeof (*YourType*), new ExpandableObjectAttribute());

Then it behaves like you decorated all the properties of type YourType with the attribute.

veb
  • 145
  • 6
  • -1 since as best I can tell from the [documentation](https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.typedescriptor.addattributes) and actually trying it, `TypeDescriptor.AddAttributes` adds the attribute at the class level, not the property level – John Cummings Feb 11 '22 at 22:04