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

  • Home
  • SEARCH
  • 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 6729765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:19:45+00:00 2026-05-26T10:19:45+00:00

Using .NET 4, C# Let’s say I have class Info that extends CustomTypeDescriptor .

  • 0

Using .NET 4, C#

Let’s say I have class Info that extends CustomTypeDescriptor. An instance of class Info has a dictionary of <string, object> pairs, loaded at runtime.

I’d like to be able to expose the dictionary keys as properties (so that each instance of Info has different properties). The values of the properties should be the corresponding values in the dictionary.

I got around to exposing properties:

    public override PropertyDescriptorCollection GetProperties()
    {

        var orig = base.GetProperties();
        var newProps = dictionary.Select( kvp => 
                       TypeDescriptor.CreateProperty(
                           this.GetType(), 
                           kvp.key, 
                           typeof(string)));
        return new PropertyDescriptorCollection(
                   orig.Cast<PropertyDescriptor>()
                   .Concat(newProps)
                   .ToArray());
    }

The problem is, how do I get their values?

var info = new Info(new Dictionary<string, object>{{"some_property", 5}};
var prop = TypeDescriptor.GetProperties(i_info)["some_property"];
var val = prop.GetValue(i_info); //should return 5

The only way I found to get control when prop.GetValue() is called was to override GetPropertyOwner(PropertyDescriptor pd), but the way I understand it, it expects me to return the instance of another type that has a matching real (compiled) property.

I’d like to be able to write the actual implementation of the property myself (for this example, return the value in the dictionary whose key matches the property name).

Is this possible?

  • 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-26T10:19:45+00:00Added an answer on May 26, 2026 at 10:19 am

    You need to make your own implementation of the PropertyDescriptor class overriding GetValue method. So instead of TypeDescriptor.CreateProperty you’ll use new MyCoolPropertyDescriptor(dictionary, kvp.Key) or like.

    Here is a sample of how it can be implemented:

    
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Linq;
    
    namespace ConsoleApplication1
    {
        internal sealed class MyCoolPropertyDescriptor : PropertyDescriptor
        {
            private Func<object, object> propertyGetter;
            private Action<object, object> propertySetter;
    
            public MyCoolPropertyDescriptor(
                string name,
                Func<object, object> propertyGetter,
                Action<object, object> propertySetter)
                : base(name, new Attribute[] {})
            {
                this.propertyGetter = propertyGetter;
                this.propertySetter = propertySetter;
            }
    
            public override bool CanResetValue(object component)
            {
                return true;
            }
    
            public override System.Type ComponentType
            {
                get { return typeof(object); }
            }
    
            public override object GetValue(object component)
            {
                return this.propertyGetter(component);
            }
    
            public override bool IsReadOnly
            {
                get { return false; }
            }
    
            public override System.Type PropertyType
            {
                get { return typeof(object); }
            }
    
            public override void ResetValue(object component)
            {
                this.propertySetter(component, null);
            }
    
            public override void SetValue(object component, object value)
            {
                this.propertySetter(component, value);
            }
    
            public override bool ShouldSerializeValue(object component)
            {
                return false;
            }
        }
    
        public sealed class Info : CustomTypeDescriptor
        {
            IDictionary<string, object> properties;
    
            public Info(IDictionary<string, object> properties)
            {
                this.properties = properties;
            }
    
            public override PropertyDescriptorCollection GetProperties()
            {
                var orig = base.GetProperties();
                var newProps = this.properties
                    .Select(kvp => new MyCoolPropertyDescriptor(
                        kvp.Key,
                        o => ((Info)o).properties[kvp.Key],
                        (o, v) => ((Info)o).properties[kvp.Key] = v));
    
                return new PropertyDescriptorCollection(orig
                    .Cast<PropertyDescriptor>()
                    .Concat(newProps)
                    .ToArray());
            }
        }
    
        internal class Program
        {
            private static void Main(string[] args)
            {
                var info = new Info(new Dictionary<string, object>{{"some_property", 5}});
                var prop = TypeDescriptor.GetProperties(info)["some_property"];
                var val = prop.GetValue(info); //should return 5
                Console.WriteLine(val);
            }
        }
    }
    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using .NET regular expressions. Let's say I have the following text: ddddddddddd And I
Let's say I have a .NET user application (.exe) running under Windows that was
I'm using ASP.NET 4.0 with Dynamic Data. Let's say I have an entity Student
Let's say I have a class, which has a NameValueCollection property. public class TestClass
First off, let me clarify the platforms we are using. We have an ASP.NET
Using .NET 1.1, I have a DataGrid that contains three columns for each row.
Let's say, I have a .NET 2 installed. Can I programmatically install version 4
I am using Lucene .NET Let's say I want to only return 50 results
I'm using .net 2.0. This is a project that I have taken over for
I have an sql query (MyQSL DB, using .Net's SqlClient) that returns a dataset.

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.