Questions tagged [expandoobject]

ExpandoObject is a .NET type whose members can be added and removed at runtime.

From msdn: Represents an object whose members can be dynamically added and removed at run time.

Instances are typically declared with the dynamic keyword:

dynamic sampleObject = new ExpandoObject();

This type is part of System.Dynamic and was introduced with .NET 4.

356 questions
294
votes
5 answers

Dynamically adding properties to an ExpandoObject

I would like to dynamically add properties to a ExpandoObject at runtime. So for example to add a string property call NewProp I would like to write something like var x = new ExpandoObject(); x.AddProperty("NewProp", System.String); Is this easily…
Craig
  • 36,306
  • 34
  • 114
  • 197
203
votes
12 answers

How to detect if a property exists on an ExpandoObject?

In javascript you can detect if a property is defined by using the undefined keyword: if( typeof data.myProperty == "undefined" ) ... How would you do this in C# using the dynamic keyword with an ExpandoObject and without throwing an exception?
Softlion
  • 12,281
  • 11
  • 58
  • 88
198
votes
4 answers

Differences between ExpandoObject, DynamicObject and dynamic

What are the differences between System.Dynamic.ExpandoObject, System.Dynamic.DynamicObject and dynamic? In which situations do you use these types?
M4N
  • 94,805
  • 45
  • 217
  • 260
100
votes
12 answers

How to flatten an ExpandoObject returned via JsonResult in asp.net mvc?

I really like the ExpandoObject while compiling a server-side dynamic object at runtime, but I am having trouble flattening this thing out during JSON serialization. First, I instantiate the object: dynamic expando = new ExpandoObject(); var d =…
TimDog
  • 8,758
  • 5
  • 41
  • 50
77
votes
7 answers

Why can't I do this: dynamic x = new ExpandoObject { Foo = 12, Bar = "twelve" }

Am I doing something wrong, or is the following code really not possible? dynamic x = new ExpandoObject { Foo = 12, Bar = "twelve" }; If this really isn't possible, is there another one-line way to instantiate an ExpandoObject with two…
devuxer
  • 41,681
  • 47
  • 180
  • 292
57
votes
2 answers

Why can't I index into an ExpandoObject?

Something caught me by surprise when looking into C# dynamics today (I've never used them much, but lately I've been experimenting with the Nancy web framework). I found that I couldn't do this: dynamic expando = new ExpandoObject(); expando.name =…
Richiban
  • 5,569
  • 3
  • 30
  • 42
53
votes
1 answer

C# 4.0 Dynamic vs Expando... where do they fit?

I am trying to learn all the new goodies that come with C# 4.0. I am failing to understand the differences between the DynamicObject and ExpandoObject types. It seems like DynamicObject is used e.g. when you want to access variables from Python…
Perpetualcoder
  • 13,501
  • 9
  • 64
  • 99
51
votes
5 answers

Adding unknown (at design time) properties to an ExpandoObject

just exploring c# 4. Trying to get my head around all this dynamic stuff. Sorry if this question is silly, no experience in this domain. If I have an ExpandoObject and want to add public properties (with get and set) to it at runtime, how would I go…
virtualmic
  • 3,173
  • 6
  • 26
  • 34
51
votes
7 answers

How to create a new unknown or dynamic/expando object in Python

In python how can we create a new object without having a predefined Class and later dynamically add properties to it ? example: dynamic_object = Dynamic() dynamic_object.dynamic_property_a = "abc" dynamic_object.dynamic_property_b =…
Jimmy Kane
  • 16,223
  • 11
  • 86
  • 117
36
votes
4 answers

How do I dynamically generate columns in a WPF DataGrid?

I am attempting to display the results of a query in a WPF datagrid. The ItemsSource type I am binding to is IEnumerable. As the fields returned are not determined until runtime I don't know the type of the data until the query is…
dkackman
  • 15,179
  • 13
  • 69
  • 123
34
votes
4 answers

In C#, how do I remove a property from an ExpandoObject?

Say I have this object: dynamic foo = new ExpandoObject(); foo.bar = "fizz"; foo.bang = "buzz"; How would I remove foo.bang for example? I don't want to simply set the property's value to null--for my purposes I need to remove it altogether. …
Matt Cashatt
  • 23,490
  • 28
  • 78
  • 111
32
votes
2 answers

Add property to ExpandoObject with the same name as a string

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…
Paul
  • 3,725
  • 12
  • 50
  • 86
31
votes
3 answers

Can I serialize an ExpandoObject in .NET 4?

I'm trying to use a System.Dynamic.ExpandoObject so I can dynamically create properties at runtime. Later, I need to pass an instance of this object and the mechanism used requires serialization. Of course, when I attempt to serialize my dynamic…
Michael Levy
  • 13,097
  • 15
  • 66
  • 100
31
votes
2 answers

Binding to ExpandoObject. PropertyChanged not working

In my Windows Store app I have a list populated with ExpandoObjects. Data binding works fine for the initial values, but not for an image property I set asyncronously after a file has been downloaded: public static async void Set(this…
user958578
  • 311
  • 3
  • 5
26
votes
2 answers

How do you convert any C# object to an ExpandoObject?

I've read a lot about how ExpandoObject can be used to dynamically create objects from scratch by adding properties, but I haven't yet found how you do the same thing starting from a non-dynamic C# object that you already have. For instance, I have…
Gigi
  • 28,163
  • 29
  • 106
  • 188
1
2 3
23 24