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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:12:00+00:00 2026-06-10T13:12:00+00:00

I have a DynamicObject and I want it to be castable to IDictionary, exactly

  • 0

I have a DynamicObjectand I want it to be castable to IDictionary, exactly the same way as ExpandoObject. For example, casting an ExpandoObject to IDictionary is perfectly valid:

dynamic ex = new ExpandoObject ();
ex.Field = "Foobar";
IDictionary<string, object> dict = ex as IDictionary<string, object>;
Console.WriteLine (dict["Field"]);

Now I try to implement this into my own DynamicObject:

public class MyDynamicObject : DynamicObject
{
    public Dictionary<string, object> members = new Dictionary<string, object> ();

    public override bool TryGetMember (GetMemberBinder binder, out object result)
    {
        if (members.ContainsKey (binder.Name)) {
            result = members[binder.Name];
            return true;
        }
        else {
            result = null;
            return false;
        }
    }
    public override bool TrySetMember (SetMemberBinder binder, object value)
    {
        this.members.Add (binder.Name, value);
        return true;
    }
    public static implicit operator IDictionary<string, object> (MyDynamicObject mydo) 
    {
        return (IDictionary<string, object>) mydo.members;
    }
}

But the compiler will fail on the public static implicit operator IDictionary<string, object>line, giving the error: “Cannot convert to or from an interface type”. If I change the implicit operator to Dictionary, I can cast MyDynamicObject to Dictionary without any Problems, but not IDictionary.

How does ExpandoObject does 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-06-10T13:12:01+00:00Added an answer on June 10, 2026 at 1:12 pm

    ExpandoObject simply implements IDictionary<string, object>.

    If you want to have the same behaviour, you also need to implement this interface and delegate all calls to your members field.

    Something like this:

    public class MyDynamicObject : DynamicObject, IDictionary<string, object>
    {
        public Dictionary<string, object> members = new Dictionary<string, object>();
    
        public IEnumerator<KeyValuePair<string, object>> GetEnumerator()
        {
            return members.GetEnumerator();
        }
    
        public void Add(KeyValuePair<string, object> item)
        {
            members.Add(item.Key, item.Value);
        }
    
        // ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to have dynamic parameters, so I made these models : class DynamicObject(models.Model):
I'm wanting to have a simple duck typing example in C# using dynamic objects.
I have use the type dynamic, a new type in .NET 4.0. I want
I have been struggling to get a simple DynamicObject example working in .NET 3.5.
So I have a dynamic object and I want to execute a method from
Have deployed numerous report parts which reference the same view however one of them
I have this scenario... 1.- I'm providing a Dynamic Table for wich users can
I have a dynamic object built inside IronPython and I would like to build
I have the following code, I want to refactor the duplication out of: public
Let's say I have this: dynamic foo = new Foobar(); And I have this:

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.