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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T23:00:22+00:00 2026-05-20T23:00:22+00:00

I want to create a method that allows me to change arbitrary properties of

  • 0

I want to create a method that allows me to change arbitrary properties of classes that derive from my base class, the result should look like this: SetPropertyValue("size.height", 50); – where size is a property of my derived class and height is a property of size.

I’m almost done with my implementation but there’s one final obstacle that I want to solve before moving on, to describe this I will first have to explain my implementation a bit:

  • Properties that can be modified are decorated with an attribute
  • There’s a method in my base class that searches for all derived classes and their decorated properties
  • For each property I generate a “property modifier”, a class that contains 2 delegates: one to set and one to get the value of the property.
  • Property Modifiers are stored in a dictionary, with the name of the property as key
  • In my base class, there is another dictionary that contains all property-modifier-dictionaries, with the Type of the respective class as key.

What the SetPropertyValue method does is this:

  • Get the correct property-modifier-dictionary, using the concrete type of the derived class (<- yet to solve)
  • Get the property modifier of the property to change (e.g. of the property size)
  • Use the get or set delegate to modify the property’s value

Some example code to clarify further:

private static Dictionary<RuntimeTypeHandle, object> EditableTypes; //property-modifier-dictionary

protected void SetPropertyValue<T>(EditablePropertyMap<T> map, string property, object value) {
  var property = map[property]; // get the property modifier
  property.Set((T)this, value); // use the set delegate (encapsulated in a method)
}

In the above code, T is the Type of the actual (derived) class. I need this type for the get/set delegates. The problem is how to get the EditablePropertyMap<T> when I don’t know what T is.

My current (ugly) solution is to pass the map in an overriden virtual method in the derived class:

public override void SetPropertyValue(string property, object value) {
  base.SetPropertyValue((EditablePropertyMap<ExampleType>)EditableTypes[typeof(ExampleType)], property, value);
}

What this does is: get the correct dictionary containing the property modifiers of this class using the class’s type, cast it to the appropiate type and pass it to the SetPropertyValue method.

I want to get rid of the SetPropertyValue method in my derived class (since there are a lot of derived classes), but don’t know yet how to accomplish that. I cannot just make a virtual GetEditablePropertyMap<T> method because I cannot infer a concrete type for T then. I also cannot acces my dictionary directly with a type and retrieve an EditablePropertyMap<T> from it because I cannot cast to it from object in the base class, since again I do not know T.

I found some neat tricks to infere types (e.g. by adding a dummy T parameter), but cannot apply them to my specific problem. I’d highly appreciate any suggestions you may have for me.

EDIT:
Although this is already a wall of text, I have to add some more notes:

  • Performance is important. I can (and do) use reflection once on startup to build my dictionary and the delegates, but during runtime it’s not acceptable (unless you can convince me that it is as fast as a delegate/method call 😉
  • I store the EditablePropertyMaps as objects because I found no other way (different generic type parameters) – perhaps this can be refined as well.
  • I cannot use the Curiously recurring template pattern as Mehrdad suggested. My base class cannot be generic for other reasons (has nothing to do with the problem at hand).

To clarify what EditableProperty / EditablePropertyMap does:

public class EditableProperty<T> {
  private Func<T, object> getter;
  private Action<T, object> setter;
  ...
  public object Get(T obj) {  return getter(obj); }
  public void Set(T obj, object value) { setter(obj, value);}

The map is a mere wrapper for a Dictionary<string, EditableProperty<T>>.

  • 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-20T23:00:23+00:00Added an answer on May 20, 2026 at 11:00 pm

    http://msdn.microsoft.com/en-us/library/system.object.gettype.aspx – have a map of EditablePropertyMaps keyed by Type, in SetPropertyValue get the map for the derived type by GetType(). Am I missing something?

    Additionally:

    public abstract class EditablePropertyMap { }
    public class EditablePropertyMap<T> : EditablePropertyMap { }
    

    Edit: I guess I was poorly suggesting to just ditch the generics and pass just objects, but now I see you want to keep the delegates generic. How about the following – I rearranged some stuff but it doesn’t change the gist:

    abstract class EditableProperty { }
    
    class EditableProperty<T> : EditableProperty {
        public void Set(T obj, object value) { Console.WriteLine(value); }
    }
    
    abstract class EditablePropertyMap {
        private static Dictionary<Type, EditablePropertyMap> maps = new Dictionary<Type, EditablePropertyMap>();
    
        abstract public void AddProperty(string name, EditableProperty property);
        abstract public void SetPropertyValue(object obj, string name, object value);
    
        static public EditablePropertyMap Get(Type t) {
            EditablePropertyMap map;
    
            if(!maps.TryGetValue(t, out map)) {
                map = (EditablePropertyMap)Activator.CreateInstance(
                    typeof(EditablePropertyMap<>)
                        .MakeGenericType(t));
    
                maps.Add(t, map);
            }
    
            return map;
        }
    }
    
    class EditablePropertyMap<T> : EditablePropertyMap {
        private Dictionary<string, EditableProperty<T>> properties = new Dictionary<string, EditableProperty<T>>();
    
        public override void AddProperty(string name, EditableProperty property) {
            properties.Add(name, (EditableProperty<T>)property);
        }
    
        public override void SetPropertyValue(object obj, string name, object value) {
            EditableProperty<T> property = properties[name];
            property.Set((T)obj, value);
        }
    }
    
    class DynamicObjectBase {
        public void SetPropertyValue(string name, object value) {
            EditablePropertyMap map = EditablePropertyMap.Get(GetType());
            map.SetPropertyValue(this, name, value);
        }
    }
    
    class SomeDynamicObject : DynamicObjectBase {
    
    }
    
    class Program {
        static void Main(string[] args) {
            EditablePropertyMap map = EditablePropertyMap.Get(typeof(SomeDynamicObject));
            map.AddProperty("wut", new EditableProperty<SomeDynamicObject>());
    
            SomeDynamicObject o = new SomeDynamicObject();
            o.SetPropertyValue("wut", 1);
        }
    }
    

    I also didn’t do the part of making the EditableProperties from just a type at init, but I assume you’ve already done all that anyway.

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

Sidebar

Related Questions

I have a method that's about ten lines of code. I want to create
I want to create checkbox in my preference Activity that allows user to toggle
I want to create a delegate type in C# inside a method for the
I want to create a list of methods to execute. Each method has the
I want to create a function that performs a function passed by parameter on
I want to create a simple http proxy server that does some very basic
I want create a drop shadow around the canvas component in flex. Technically speaking
I want to create a Java application bundle for Mac without using Mac. According
I want to create a client side mail creator web page. I know the
I want to create a draggable and resizable window in JavaScript for cross browser

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.