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 8879439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:57:03+00:00 2026-06-14T19:57:03+00:00

An object has some properties , now at runtime — when a condition is

  • 0

An object has some properties , now at runtime — when a condition is met .. I want to add new properties to this object .

“DynamicObject” cant be ustilised since i wont be knowing the property Names beforehand .

I came acroos PropertyBuilder http://msdn.microsoft.com/en-us/library/system.reflection.emit.propertybuilder.aspx

But i cant find help regarding how to use the propertBuilder for adding properties to an existing object of a defined-existing class.

  • 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-06-14T19:57:04+00:00Added an answer on June 14, 2026 at 7:57 pm

    You cannot add real (reflection) properties to an object or type at runtime.

    If the context here is data-binding, then you can all artificial properties, by implementing one or more of ICustomTypeDescriptor, TypeDescriptionProvider, TypeConverter, ITypedList – and providing your own PropertyDescriptors for the extra properties.

    • ICustomTypeDescriptor is per-object and tied to that object
    • TypeDescriptionProvider is per-object or per-type, and is separate to the object
    • TypeConverter is per-type and is used in particular by PropertyGrid
    • ITypedList is used by a list (IList) to describe the properties of the child objects

    Example:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Windows.Forms;
    static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            FooConverter.AddProperty("Time", typeof(DateTime));
            FooConverter.AddProperty("Age", typeof(int));
            using (var grid = new PropertyGrid { Dock = DockStyle.Fill, SelectedObject = new Foo() })
            using (var form = new Form { Controls = { grid } })
            {
                Application.Run(form);
            }
        }
    }
    class FooConverter : ExpandableObjectConverter
    {
        private static readonly List<Tuple<string, Type>> customProps = new List<Tuple<string, Type>>();
        public static void AddProperty(string name, Type type)
        {
            lock (customProps) customProps.Add(Tuple.Create(name, type));
        }
        public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, System.Attribute[] attributes)
        {
            var orig = base.GetProperties(context, value, attributes);
            lock(customProps)
            {
                if(customProps.Count == 0) return orig;
    
                PropertyDescriptor[] props = new PropertyDescriptor[orig.Count + customProps.Count];
                orig.CopyTo(props, 0);
                int i = orig.Count;
                foreach (var prop in customProps)
                {
                    props[i++] = new SimpleDescriptor(prop.Item1, prop.Item2);
                }
                return new PropertyDescriptorCollection(props);
            }
        }
        class SimpleDescriptor : PropertyDescriptor
        {
            private readonly Type type;
            public SimpleDescriptor(string name, Type type)
                : base(name, new Attribute[0])
            {
                this.type = type;
            }
            public override Type PropertyType { get { return type;} }
            public override bool SupportsChangeEvents { get { return false; } }
            public override void ResetValue(object component) { SetValue(component, null); }
            public override bool CanResetValue(object component) { return true; }
            public override bool ShouldSerializeValue(object component) { return GetValue(component) != null; }
            public override bool IsReadOnly { get { return false; } }
            public override Type ComponentType { get { return typeof(Foo); } }
            public override object GetValue(object component) { return ((Foo)component).GetExtraValue(Name); }
            public override void SetValue(object component, object value) { ((Foo)component).SetExtraValue(Name, value); }
            public override string Category { get { return "Extra values"; } }
        }
    }
    [TypeConverter(typeof(FooConverter))]
    public class Foo
    {
        Dictionary<string, object> extraValues;
        internal object GetExtraValue(string name)
        {
            object value;
            if (extraValues == null || !extraValues.TryGetValue(name, out value)) value = null;
            return value;
        }
        internal void SetExtraValue(string name, object value)
        {
            if (extraValues == null && value != null) extraValues = new Dictionary<string, object>(StringComparer.InvariantCultureIgnoreCase);
            if (value == null) extraValues.Remove(name);
            else extraValues[name] = value;
        }
        public int Id { get; set; }
        public string Name { get; set; }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an object config which has some properties. I can export this ok,
I want to create a function object, which also has some properties held on
I wrote some wrapper which has another object as an attribute. This wrapper proxies
EDIT:: This might be simple for some but right now it has me confused.
I'm doing some unit testing, and mocking some properties using Moq . Now, this
In this hypothetical example, imagine I have an object FooSet that has five properties
the window.open() object has some funky parameter list... is there a way to do
Salesforce has some built in meta-data fields to describe each Field of each Object.
I have an object called Layer that has some attributes and some methodes. i
I'm getting AttributeError: 'NoneType' object has no attribute 'encode' error when parsing out some

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.