How to dynamically get a property by name from a C# ExpandoObject?

后端 未结 1 491
不思量自难忘°
不思量自难忘° 2020-12-28 12:27

I have an ExpandoObject and want to make a getter for it that will return a property by name at runtime, where the name is specified in a string instead of hard

相关标签:
1条回答
  • 2020-12-28 12:48

    ExpandoObject provides access both via dynamic and via IDictionary<string,object> - so you could just use the dictionary API:

    var byName = (IDictionary<string,object>)account.features;
    bool val = (bool)byName["isEmailEnabled"];
    

    Or if the name is fixed, just:

    bool val = ((dynamic)account).features.isEmailEnabled;
    
    0 讨论(0)
提交回复
热议问题