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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:31:31+00:00 2026-05-13T06:31:31+00:00

Yes, title sounds a bit confusing, so I’ll explain what I mean: suppose you

  • 0

Yes, title sounds a bit confusing, so I’ll explain what I mean: suppose you have a C# 4.0 ‘dynamic’ object, and the name of a property. How would you retrieve that property from the dynamic object?

In other words, how would you implement:

public static object GetDynamicValue(dynamic o, string name) { ... }

Another way to put it is that I’m trying to treat a dynamic object as an IDictionary.

Note that reflection is likely not an option here, since the dynamic object could be a custom implementation which is not reflection based (e.g. by extending DynamicObject and doing its own thing).

  • 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-13T06:31:31+00:00Added an answer on May 13, 2026 at 6:31 am

    You would have to build a call site, create a binder etc.

    The easiest way to see what happens is to compile this:

    public static object GetDynamicValue(dynamic o, string name)
    {
        return o.Foo;
    }
    

    Then decompile it with Reflector and work out what it’s doing. It’ll be pretty complicated, mind you – and you’ll need to change it from being a single, static, cached call site to creating a new one on each invocation.

    Here’s an example which does work… but whether it’s entirely correct or not is a different matter 🙂 (I got this going by doing exactly what I suggested above.)

    using Microsoft.CSharp.RuntimeBinder;
    using System;
    using System.Dynamic;
    using System.Runtime.CompilerServices;
    
    class Test
    {
        public static object GetDynamicValue(dynamic o, string name)
        {
            CallSite<Func<CallSite, object, object>> site 
                = CallSite<Func<CallSite, object, object>>.Create
                (Binder.GetMember(CSharpBinderFlags.None, name, 
                 typeof(Test), new CSharpArgumentInfo[] 
                 { CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null) }));
            return site.Target(site, o);
        }
    
        static void Main()
        {
            Console.WriteLine(GetDynamicValue("hello", "Length"));
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Yes, I know the title sounds a little suspicious. I will try to explain
The title sounds confusing.. but im sure this is possible! I have one button,
This sounds like homework, yes it is (of someone else) , I asked a
Yes, I know it sounds weird, but it doesn't, the question is why, and
Yes, I know. The existence of a running copy of SQL Server 6.5 in
...Yes I've seen: Best Resources for Learning JavaFX? but it doesn't really answer the
Yes, There's More Than One Way To Do It but there must be a
Yes, there is a similar question here . However, that question doesn't seem to
Yes, I know you could use regular objects as associative arrays in JavaScript, but
Yes we're talking about ASCII codes. My appologies I'm not the Delphi dev here.

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.