Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5960703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:51:47+00:00 2026-05-22T18:51:47+00:00

I have a class which the programmer can use to dynamically add new properties.

  • 0

I have a class which the programmer can use to dynamically add new properties. For that it implements the ICustomTypeDescriptor to be able to override GetProperties() method.

public class DynamicProperty
{
    public object Value { get; set; }

    public Type Type { get; set; }

    public string Name { get; set; }

    public Collection<Attribute> Attributes { get; set; }
}

public class DynamicClass : ICustomTypeDescriptor
{
    // Collection to code add dynamic properties
    public KeyedCollection<string, DynamicProperty> Properties
    {
        get;
        private set;
    }

    // ICustomTypeDescriptor implementation
    PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties()
    {
        // Properties founded within instance
        PropertyInfo[] instanceProps = this.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);

        // Fill property collection with founded properties
        PropertyDescriptorCollection propsCollection = 
            new PropertyDescriptorCollection(instanceProps.Cast<PropertyDescriptor>().ToArray());

        // Fill property collection with dynamic properties (Properties)
        foreach (var prop in Properties)
        {
            // HOW TO?
        }

        return propsCollection;
    }
}

Is it possible to iterate over the Properties list to add each property to PropertyDescriptorCollection?

Basically I want the programmer to be able to add a DynamicProperty to a collection which will be handled by GetProperties. Something like:

new DynamicClass()
{
    Properties = {
        new DynamicProperty() {
            Name = "StringProp",
            Type = System.String,
            Value = "My string here"
        },

        new DynamicProperty() {
            Name = "IntProp",
            Type = System.Int32,
            Value = 10
        }
    }
}

Now those Properties would be setted to instance properties whenever GetPropertiesis called. Am I thinking this the right way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T18:51:47+00:00Added an answer on May 22, 2026 at 6:51 pm

    You are already creating a collection like this:

    PropertyDescriptorCollection propsCollection = 
                new PropertyDescriptorCollection(instanceProps.Cast<PropertyDescriptor>().ToArray());
    

    But the collection you are creating only has the existing properties, not your new properties.

    You need to supply a single array concatenated from the existing properties and your new properties.

    Something like this:

    instanceProps.Cast<PropertyDescriptor>().Concat(customProperties).ToArray()
    

    Next problem: you need customProperties which is a collection of PropertyDescriptor. Unfortunately PropertyDescriptor is an abstract class so you don’t have an easy way to create one.

    We can fix this though, just define your own CustomPropertyDescriptor class by deriving from PropertyDescriptor and implementing all the abstract methods.

    Something like this:

    public class CustomPropertyDescriptor : PropertyDescriptor
    {
        private Type propertyType;
        private Type componentType;
    
        public CustomPropertyDescriptor(string propertyName, Type propertyType, Type componentType)
            : base(propertyName, new Attribute[] { })
        {
            this.propertyType = propertyType;
            this.componentType = componentType;
        }
    
        public override bool CanResetValue(object component) { return true; }
        public override Type ComponentType { get { return componentType; } }
        public override object GetValue(object component) { return 0; /* your code here to get a value */; }
        public override bool IsReadOnly { get { return false; } }
        public override Type PropertyType { get { return propertyType; } }
        public override void ResetValue(object component) { SetValue(component, null); }
        public override void SetValue(object component, object value) { /* your code here to set a value */; }
        public override bool ShouldSerializeValue(object component) { return true; }
    }
    

    I haven’t filled in the calls to get and set your properties; those calls depend on how you’ve implemented the dynamic properties under the hood.

    Then you need to create an array of CustomPropertyDescriptor filled in with information appropriate to your dynamic properties, and concatenate it to the basic properties as I described initially.

    The PropertyDescriptor not only describes your properties, it also enables client to actually get and set the values of those properties. And that’s the whole point, isn’t it!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class which implements UserControl. In .NET 2005, a Dispose method is
I have a class which I'm serialising to send over a unix socket and
I have a class which is marked with a custom attribute, like this: public
I have a class which has the following constructor public DelayCompositeDesigner(DelayComposite CompositeObject) { InitializeComponent();
I have a class which looks something like this: public class Test { private
I have a class which constructor takes a Jakarta enums . I'm trying to
I have a class which inherits from QTreeWidgetItem and I intercept the click event.
I have a class which is not thread safe: class Foo { /* Abstract
I have a class which has many small functions. By small functions, I mean
I have an class which has a enum property and a boolean property, based

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.