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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:07:58+00:00 2026-06-10T06:07:58+00:00

Assuming I cannot use an ExpandoObject and have to roll my own like so

  • 0

Assuming I cannot use an ExpandoObject and have to roll my own like so :-

class MyObject : DynamicObject {
    dictionary<string, object> _properties = dictionary<string, object>();

    public override bool TryGetMember(GetMemberBinder binder, out object result) {
        string name = binder.Name.ToLower();

        return _properties.TryGetValue(name, out result);
    }

    public override bool TrySetMember(SetMemberBinder binder, object value) {
        _properties[binder.Name.ToLower()] = value;

        return true;
    }
}

and further down the class hierarchy I have

class MyNewObject : MyObject {
    public string Name {
        get {
            // do some funky stuff
        }
        set {
            // ditto
        }
    }
}

which is quite nice as now I can do the follow :-

dynamic o = MyNewObject();

o.Age = 87;     // dynamic property, handled by TrySetMember in MyObject
o.Name = "Sam"; // non dynamic property, handled by the setter defined in MyNewObject

But the above assumes I know the properties (e.g. Age, Name) at compile time.

Suppose I don’t know what they will be until run time.

How can I change the above to support properties I will only know at run time?

Basically I think I am asking is how I can call the code that calls TrySetMember directly so that it will either create a new property or use a getter/setter if one has been defined.

Final Solution as follows :-

using System.Dynamic;
using Microsoft.CSharp.RuntimeBinder;
using System.Runtime.CompilerServices;

class MyObject : DynamicObject {
    Dictionary<string, object> _properties = new Dictionary<string, object>();

    public object GetMember(string propName) {
        var binder = Binder.GetMember(CSharpBinderFlags.None,
              propName, this.GetType(),
              new List<CSharpArgumentInfo>{
                       CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null)});
        var callsite = CallSite<Func<CallSite, object, object>>.Create(binder);

        return callsite.Target(callsite, this);
    }

    public void SetMember(string propName, object val) {
        var binder = Binder.SetMember(CSharpBinderFlags.None,
               propName, this.GetType(),
               new List<CSharpArgumentInfo>{
                       CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null),
                       CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null)});
        var callsite = CallSite<Func<CallSite, object, object, object>>.Create(binder);

        callsite.Target(callsite, this, val);
    }

    public override bool TryGetMember(GetMemberBinder binder, out object result) {
        string name = binder.Name.ToLower();

        return _properties.TryGetValue(name, out result);
    }

    public override bool TrySetMember(SetMemberBinder binder, object value) {
        _properties[binder.Name.ToLower()] = value;

        return true;
    }
}
  • 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-10T06:07:59+00:00Added an answer on June 10, 2026 at 6:07 am

    Although the c# compiler is translating dynamic keyword usage to invocation with the dlr using a string name, those Apis are difficult to use directly without the compilers help. The open source framework Dynamitey (available through nuget as a PCL library) encapsulates the dlr API, to make it easy so that you can just call Impromptu.InvokeSet(target, name, value).

    using Dynamitey;
    ...
    
    dynamic o = MyNewObject();
    
    Dynamic.InvokeSet(o,"Age" ,87); 
    Dynamic.InvokeSet(o,"Names" ,"Sam");   
    

    Getters and Setters are the least complicated to use the actual Microsoft API directly, so if you don’t want to use the 3rd party framework going to the source is an option too.

    using Microsoft.CSharp.RuntimeBinder;
    using System.Runtime.CompilerServices;
    ...
    
      dynamic o = MyNewObject();
      var binder = Binder.SetMember(CSharpBinderFlags.None,
                       "Age",
                       typeof(object),
                       new List<CSharpArgumentInfo>{
                               CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null),
                               CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null)
                                                   });
    
      var callsite = CallSite<Func<CallSite, object, object, object>>.Create(binder);
    
      callsite.Target(callsite,o,87);
    
      var binder2 =Binder.SetMember(CSharpBinderFlags.None,
                       "Name",
                       typeof(object),
                       new List<CSharpArgumentInfo>{
                               CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null),
                               CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null)
                                                   });
      var callsite2 = CallSite<Func<CallSite, object, object, object>>.Create(binder2);
    
      callsite2.Target(callsite2,o,"Sam");
      
    

    No StackOverflow, there is nothing to improve

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

Sidebar

Related Questions

Assuming T as a class type parameter, why cannot I use T.class I was
Assuming that I cannot run something like this with Fabric: run(svn update --password 'password'
Assuming I have a numerical string: var foo = 0; Assume I want to
Assuming I have a XML like so: <a> <b> <i>data</i> <ii>data</ii> <iii>data</iii> </b> <b>
I have a file uploader on my website, since i cannot use php I'm
If i have a private class Class A { public static string foo; }
Python 2.4.x (cannot install any non-stock modules). Question for you all. (assuming use of
Assuming I have a string $str = "abc*efg*hij*"; and an array $arr = array("123","456","789");
Can you have a class which implements an interface, and choose whether to use
Assuming that I have this: enum { A = 0x2E, B = 0x23, C

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.