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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T03:12:57+00:00 2026-05-15T03:12:57+00:00

I’ve got an IDictionary field that I would like to expose via a property

  • 0

I’ve got an IDictionary field that I would like to expose via a property of type IDictionary<string, dynamic> the conversion is surprisingly difficult since I have no idea what I can .Cast<>() the IDictionary to.

Best I’ve got:

IDictionary properties;
protected virtual IDictionary<string, dynamic> Properties {
  get { 
        return _properties.Keys.Cast<string>()
              .ToDictionary(name=>name, name=> _properties[name] as dynamic); 
      }
    }
  • 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-15T03:12:58+00:00Added an answer on May 15, 2026 at 3:12 am

    If the underlying type of the IDictionary does not implement IDictionary<string, dynamic>, you cannot cast the object, period. If it does, a simple cast via (IDictionary<string, dynamic>)localVar will suffice.

    If it’s not, there are two things you can do:

    1. Copy the IDictionary to your generic type.
    2. Build an Adapter class that accepts the IDictionary as a dependency and implements the generic IDictionary you want, mapping calls from one to the other.

    Edit: The sample code you’ve just posted will copy the dictionary every time it gets called! I will edit again in a moment with some suggested code.

    Option 1

    your sample code approach is solid as a means of copying the data, but the copy should be cached or you’re going to copy lots of times. I’d suggest you put the actual translation code into a separate method and call that from your property the first time it’s used. For example:

    private IDictionary dataLayerProperties; 
    private IDictionary<string, dynamic> translatedProperties = null;
    protected virtual IDictionary<string, dynamic> Properties
    {
        if(translatedProperties == null)
        {
            translatedProperties = TranslateDictionary(dataLayerProperties);        
        }  
        return translatedProperties;
    }
    
    public IDictionary<string, dynamic> TranslateDictionary(IDictionary values)
    {
        return values.Keys.Cast<string>().ToDictionary(key=>key, key => values[key] as dynamic);            
    }
    

    Now, there are obvious cons to this approach… what if dataLayerProperties needs to be refreshed? You have to go setting translatedProperties to null again, etc.

    Option 2

    This is my preferred approach.

    public class TranslatedDictionary : IDictionary<string, dynamic>
    {
        private IDictionary Original = null;
    
        public TranslatedDictionary(IDictionary original)
        {
            Original = original;
        }
        public ICollection<string> Keys
        {
            get
            {
                return Original.Keys.Cast<string>().ToList();
            }
        }
    
        public dynamic this[string key]
        {
            get
            {
                return Original[key] as dynamic;
            }
            set
            {
                Original[key] = value;
            }
        }
        // and so forth, for each method of IDictionary<string, dynamic>
    }
    
    //elsewhere, using your original property and field names:
    Properties = new TranslatedDictionary(properties);
    

    Now, there are obvious cons to this approach as well, the most glaring is the fact that the Keys (and Value and anything else that returns ICollection on IDictionary has to return a new array for every call. But this still allows the most flexible approach, since it ensures the data is always up to date.

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

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I would like to count the length of a string with PHP. The string
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
i got an object with contents of html markup in it, for example: string
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.