expandoobject

List<dynamic> elements have fields but I cannot access them. Why?

本小妞迷上赌 提交于 2019-12-04 19:33:52
问题 I need to loop over a List<dynamic> objects. The list's objects all have values, but for some reason, I am not able to access any of the dynamic object fields. Below is a screenshot of my debug window: There you can see the object contains fields (such Alias , Id , Name , etc). I tried both casting it to a IDictionary<string, object> and ExpandoObject , to no avail. I did not face such a thing before: failing to access existing fields in a dynamic object when they exist . What is wrong here?

How can I use collection initializer syntax with ExpandoObject?

徘徊边缘 提交于 2019-12-03 05:17:35
I've noticed that the new ExpandoObject implements IDictionary<string,object> which has the requisite IEnumerable<KeyValuePair<string, object>> and Add(string, object) methods and so it should be possible to use the collection initialiser syntax to add properties to the expando object in the same way as you add items to a dictionary. Dictionary<string,object> dict = new Dictionary<string,object>() { { "Hello", "World" } }; dynamic obj = new ExpandoObject() { { "foo", "hello" }, { "bar", 42 }, { "baz", new object() } }; int value = obj.bar; But there doesn't seem to be a way of doing that.

Add property to ExpandoObject with the same name as a string

寵の児 提交于 2019-12-02 20:24:15
Is there a way to add a property to an ExpandoObject with the same name as a string value? For example, if I have: string propName = "ProductNumber"; dynamic obj = new System.Dynamic.ExpandoObject(); I can create the property ProductNumber like: obj.ProductNumber = 123; But, can I create the property obj.ProductNumber based on the string propName ? So, if I don't know what the name of the property will be in advanced, I can create it based on this input. If this is not possible with ExpandoObject, any other areas of C# I should look into? ExpandoObject implements IDictionary<string, object> :

How to apply an Extension Method on the object having the type of ExpandoObject?

无人久伴 提交于 2019-12-02 07:45:37
问题 Here is my code: public static class DynamicExtensions public static void Add(this ExpandoObject obj, string path){ dynamic _obj = obj; if (_obj == null) throw new ArgumentNullException("obj"); _obj.path = path; } } But I got the error of "'System.Dynamic.ExpandoObject' does not contain a definition for 'Add'", when I call it in this way: dynamic obj = new ExpandoObject(); obj.Add("p1"); How to fix it? Thanks in advance! 回答1: The problem is using dynamic with extension methods - the two just

Periods in the name of c# dynamic ExpandoObjects?

时光毁灭记忆、已成空白 提交于 2019-12-02 04:27:15
问题 Maybe this is a silly question, but I'm working on a project that wants me to generate some JSON that looks like this: {'action.type':'post', 'application':APP_ID} In C#, I'm trying to create this "action.type" attribute, with the value of "post". How would I do that? Here's how I've typlically been creating stuff like: dynamic ActionSpec = new ExpandoObject(); ActionSpec.SomeParam = "something"; ActionSpec.id = 12345; I can't go "ActionSpec.action.type", because that will not output the

C# - Adding objects dynamically (adding dynamic property names)

有些话、适合烂在心里 提交于 2019-12-02 02:47:48
问题 I'm trying to create some dynamic ExpandoObject . I've encountered a certain problem. As I don't know what the name of these different properties in my objects should be, I can't do like this: var list = new ArrayList(); var obj = new ExpandoObject(); obj.ID = 1, obj.Product = "Pie", obj.Days = 1, obj.QTY = 65 list.Add(obj); Let me explain my situation: I wish to get data from a random DB (I don't know which, but building a connection string from the information I get from the UI), therefore

Is there a way to perform a chained null check in a dynamic/expando?

那年仲夏 提交于 2019-12-01 10:39:27
C# has the usefull Null Conditional Operator . Well explained in this answer too. I was wondering if it is possible to do a similar check like this when my object is a dynamic/expando object. Let me show you some code: Given this class hierarchy public class ClsLevel1 { public ClsLevel2 ClsLevel2 { get; set; } public ClsLevel1() { this.ClsLevel2 = new ClsLevel2(); // You can comment this line to test } } public class ClsLevel2 { public ClsLevel3 ClsLevel3 { get; set; } public ClsLevel2() { this.ClsLevel3 = new ClsLevel3(); } } public class ClsLevel3 { // No child public ClsLevel3() { } } If i

Cast ExpandoObject to anonymous type

丶灬走出姿态 提交于 2019-11-30 18:52:30
Can I cast ExpandoObject to anonymous type ? var anoObj = new { name = "testName", email = "testEmail" }; dynamic expandoObj = new System.Dynamic.ExpandoObject(); // Here I'm populating the expandoObj with same property names/types in anonymoustype(anoObj) // Now, how to convert this ExpandoObject to anonymoustype ? var newObj = (typeof(anoObj)expandoObj); // This doesn't work Added Later // This is my entity public class Customer { #region Public Properties [ColumnAttribute(Name = "IdColumn")] public string Id { get; set; } [ColumnAttribute(Name = "NameColumn")] public string Name { get; set;

Deserialize a property as an ExpandoObject using JSON.NET

岁酱吖の 提交于 2019-11-30 17:43:46
For example, there's an object like the next one: public class Container { public object Data { get; set; } } And it's used this way: Container container = new Container { Data = new Dictionary<string, object> { { "Text", "Hello world" } } }; If I deserialize a JSON string obtained from serializing the above instance, the Data property, even if I provide the ExpandoObjectConverter , it's not deserialized as an ExpandoObject : Container container = JsonConvert.Deserialize<Container>(jsonText, new ExpandoObjectConverter()); How can I deserialize a class property assigned with an anonymous object

ExpandoObject, anonymous types and Razor

纵饮孤独 提交于 2019-11-30 15:42:45
I want to use an ExpandoObject as the viewmodel for a Razor view of type ViewPage<dynamic> . I get an error when I do this ExpandoObject o = new ExpandoObject(); o.stuff = new { Foo = "bar" }; return View(o); what can I do to make this work? gram You can do it with the extension method mentioned in this question: Dynamic Anonymous type in Razor causes RuntimeBinderException So your controller code would look like: dynamic o = new ExpandoObject(); o.Stuff = new { Foo = "Bar" }.ToExpando(); return View(o); And then your view: @model dynamic @Model.Stuff.Bar Using the open source Dynamitey (in