How to add property-level Attribute to the TypeDescriptor at runtime?
Asked Answered
H

4

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

Harbird answered 27/8, 2012 at 14:10 Comment(0)
H
35

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.

Harbird answered 27/8, 2012 at 14:10 Comment(15)
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...Harkins
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?Harbird
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?Harkins
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.Fransis
@Colonel Panic thank you, I've fixed this. It's just the second one that's needed. _settings' type is not required.Harbird
@Tim Bourguignon sorry, can't help you, I'm too far from asp, mvc3.Harbird
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?Floppy
@Floppy could you please post the code? Maybe even create a question, 'coz comments aren't really suited for code posting :)Harbird
I've posted another question with my problem: #17164612Floppy
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.Aerification
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 editslikeitHarbird
@Aerification 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.Distrait
@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)Aerification
@Aerification thank you :) I ended up finding your comment below and using the solution you provided on that other question, worked like a charm!Distrait
@PeterSandor glad it helped, even being old!Aerification
A
3

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).

Aerification answered 30/12, 2015 at 11:34 Comment(0)
F
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://mcmap.net/q/590443/-how-do-i-inject-a-custom-uitypeeditor-for-all-properties-of-a-closed-source-type

Fransis answered 25/9, 2012 at 20:26 Comment(0)
T
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.

Terrarium answered 15/4, 2014 at 8:59 Comment(1)
-1 since as best I can tell from the documentation and actually trying it, TypeDescriptor.AddAttributes adds the attribute at the class level, not the property levelCarmon

© 2022 - 2024 — McMap. All rights reserved.