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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:36:17+00:00 2026-05-15T23:36:17+00:00

I am using a Dynamic dictionary in C#. The problem I am facing is

  • 0

I am using a Dynamic dictionary in C#. The problem I am facing is the behavior of TryGetMember which I am overriding in the dynamic dictionary class.

Here’s the code of dynamic dictionary.

class DynamicDictionary<TValue> : DynamicObject
{
    private IDictionary<string, TValue> m_dictionary;

    public DynamicDictionary(IDictionary<string, TValue> a_dictionary)
    {
        m_dictionary = a_dictionary;
    }

    public override bool TryGetMember(GetMemberBinder a_binder, out object a_result)
    {
        bool returnValue = false;

        var key = a_binder.Name;
        if (m_dictionary.ContainsKey(key))
        {
            a_result = m_dictionary[key];
            returnValue = true;
        }
        else            
            a_result = null;

        return returnValue;
    }
}

Here, TryGetMember will be called at runtime whenever we refer some key from outside, but it’s strange that binder’s Name member which always gives the key what we refer from outside, it always resolves the key name written as characters of alphabets.

e.g. if the object of DynamicDictionary made as:

Dictionary<string,List<String>> dictionaryOfStringVsListOfStrings; 

//here listOfStrings some strings list already populated with strings
dictionaryOfStringVsListOfStrings.Add("Test", listOfStrings); 
dynamic dynamicDictionary_01 = new 
    DynamicDictionary<List<String>(dictionaryOfStringVsListOfStrings);

string somekey = "Test";

//will be resolve at runtime
List<String> listOfStringsAsValue = dynamicDictionary_01.somekey 

Now what happens here is “somekey” will become the value of a_binder (i.e a_binder.Name=”somekey”). It should be resolved as a_binder.Name = “Test” and then from the dynamic dictionary it will locate listOfStrings against this key (i.e. actually “Test” but it resolves not the value but actual variable name as key).

Is there a way around this?

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

    The point of dynamic typing is to make the member names themselves get resolved from the source code member access.

    Dynamic typing is working exactly as it’s meant to here – it’s not designed to retrieve the value of the variable and use that as the member name – it’s designed to use the member name you used in your source code (i.e. “somekey”).

    It sounds like you really don’t need dynamic typing at all here – just use Dictionary<string,List<String>> as normal:

    List<String> listOfStringsAsValue = dictionary[somekey];
    

    EDIT: It sounds like you actually want to encapsulate a dictionary like this:

    public class Foo // TODO: Come up with an appropriate name :)
    {
        private readonly Dictionary<string, List<string>> dictionary =
            new Dictionary<string, List<string>>();
    
        public List<string> this[string key]
        {
            get
            {
                List<string> list;
                if (!dictionary.TryGetValue(key, out list))
                {
                    list = new List<string>();
                    dictionary[key] = list;
                }
                return list;
            }
        }
    }
    

    Then you can do:

    foo["first"].Add("value 1");
    foo["second"].Add("value 2")
    foo["first"].Add("value 1.1");
    

    If you want to be able to attempt to fetch a list without creating a new one if it doesn’t exist, you could add a method to do that.

    It really doesn’t sound like you need DynamicObject here.

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

Sidebar

Related Questions

This is an Amazon interview Question.I have solved this problem in O(n) using dynamic
I've got a standard 'dynamic dictionary' type class in C# - class Bucket :
I'm using a custom implementation of a DynamicObject which works perfectly for my application,
I have the following code which creates a dynamic object that is assigned to
i'm currently using a dictionary to associate a boolean to my (non-dynamic) sprites, but
I'm using RavenDB & need to store dynamic data like: public class User {
I'm using dynamic multilevel hashes from which I read data but also writes data.
The Problem I am using dynamic matrix groups as in Dynamic Grouping from Chris
Currently im facing the problem I need to create an instance of Dictionary with
i am trying to create a dynamic multioption select using dynamic values for a

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.