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
11
votes
1 answer

Creating a list of ExpandoObjects with properties read from an array

I'm trying to create a dynamic list of objects because I don't know what properties my objects will have until I read them from a file. So suppose I have the properties of my object inside an array (e.g. FirstName, LastName,Email). I want to create…
disasterkid
  • 6,948
  • 25
  • 94
  • 179
10
votes
4 answers

"Keyword 'this' is not valid in a static property, static method, or static field initializer" when adding methods to an ExpandoObject

I am try to add a dynamic method to ExpandoObject which would return the properties (added dynamically) to it, however it's always giving me error. Is something wrong I am doing here? using System; using System.Collections.Generic; using…
humblecoder
  • 499
  • 2
  • 5
  • 15
10
votes
1 answer

Dynamically read properties from c# expando object

Currently I have the following method- public void CreateWorkbook(List ItemList) { PropertyInfo[] properties= typeof(T).GetProperties(); foreach (PropertyInfo prop in properties) { } } I would like to replace T with…
s.k.paul
  • 7,099
  • 28
  • 93
  • 168
10
votes
3 answers

ExpandoObject vs. Dictionary from a performance point of view?

A rather simple question really. I'm working on a project where I need to store and retrieve property values dynamically from a kind of context storage. The values will be written now and then and read multiple times. Speed of retrieval is the top…
CodingInsomnia
  • 1,843
  • 2
  • 15
  • 19
10
votes
2 answers

How can I convert an ExpandoObject to Dictionary in C#?

I'm using Jint to execute JavaScript in a Xamarin app. Jint is converting an associative array into an ExpandoObject. How do I use this object? Ideally, I'd like to get a dictionary of the data out of it. JavaScript returns: return {blah:abc,…
Chris Williams
  • 11,647
  • 15
  • 60
  • 97
10
votes
2 answers

check to see if a property exists within a C# Expando class

I would like to see if a property exist in a C# Expando Class. much like the hasattr function in python. I would like the c# equalant for hasattr. something like this... if (HasAttr(model, "Id")) { # Do something with model.Id }
eiu165
  • 6,101
  • 10
  • 41
  • 59
9
votes
1 answer

How to databind a gridview to an ExpandoObject

When I try to databind an ASP.NET GridView to an IEnumerable using an ObjectDataSource, I get the following exception. System.Web.HttpException (0x80004005): DataBinding: 'System.Dynamic.ExpandoObject' does not contain a property…
EtienneT
  • 5,045
  • 6
  • 36
  • 39
9
votes
1 answer

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

I need to loop over a List 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…
Veverke
  • 9,208
  • 4
  • 51
  • 95
8
votes
2 answers

C# deep/nested/recursive merge of dynamic/expando objects

I need to "merge" 2 dynamic objects in C#. All that I've found on stackexchange covered only non-recursive merging. But I am looking to something that does recursive or deep merging, very much the same like jQuery's $.extend(obj1, obj2)…
Dynalon
  • 6,577
  • 10
  • 54
  • 84
8
votes
2 answers

how to convert Dictionary to Dictionary using Colllection.ToDictionary()

I am using Dapper to fetch a 2 column resultset into a dictionary. I noticed that intellisense shows me a .ToDictionary() when I hover over the resultset but I cannot get it to work since dapper uses dynamic…
Gullu
  • 3,477
  • 7
  • 43
  • 70
8
votes
5 answers

How to set ExpandoObject's dictionary as case insensitive?

given the code below dynamic e = new ExpandoObject(); var d = e as IDictionary; for (int i = 0; i < rdr.FieldCount; i++) d.Add(rdr.GetName(i), DBNull.Value.Equals(rdr[i]) ? null : rdr[i]); Is there a way to make it case…
Kumar
  • 10,997
  • 13
  • 84
  • 134
7
votes
1 answer

Implementing ExpandoObject in Scala

I am trying to implement C#'s ExpandoObject-like class in Scala. This is how it is supposed to work: val e = new ExpandoObject e.name := "Rahul" // This inserts a new field `name` in the object. println(e.name) // Accessing its value. Here is what…
missingfaktor
  • 90,905
  • 62
  • 285
  • 365
7
votes
1 answer

.NET4 ExpandoObject usage leaking memory

I have an inherited .NET 4.0 application that runs as a Windows service. I'm not a .NET expert by any stretch, but after writing code for 30+ years I know how to find my way around. When the service first starts it clocks in at around 70MB private…
7
votes
2 answers

Casting Dynamic Object and Passing Into UnitOfWork and Repository Pattern. Throws Exception

This is a very specific problem. Not quite sure how to even word it. Basically I am implementing the unit of work and repository pattern, I have a dynamic object that I convert to an int, but if I use var it will throw an exception when trying to…
7
votes
1 answer

Reflect on an ExpandoObject

I have written a nifty function that will accept a system.object, reflect on its properties and serialize the object into a JSON string. It looks like this: public class JSONSerializer { public string Serialize(object obj) Now, I want to be…
Water Cooler v2
  • 32,724
  • 54
  • 166
  • 336
1 2
3
23 24