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 I don't know what data I need to get. This could be an example of a DB table
TABLE Sale
- ID: int,
- Product: nvarchar(100),
- Days: int,
- QTY: bigint
This could be another exmaple:
TABLE Foobar
- Id: int,
- Days: int
- QTY: bigint
- Product_Id: int
- Department_Id: int
As you see, I don't know what the DB looks like (this is 100% anonymous, therefore it needs to be 100% dynamic), and the data I want to return should look like a well constructed JSON, like so:
[
{
"ID": 1,
"Product": "Pie"
"Days": 1,
"QTY": 65
},
{
"ID": 2,
"Product": "Melons"
"Days": 5,
"QTY": 12
}
]
Or, with the other example:
[
{
"ID": 1,
"Days": 2,
"QTY": 56,
"Product_Id": 5,
"Department_Id": 2
}
{
"ID": 2,
"Days": 6,
"QTY": 12,
"Product_Id": 2,
"Department_Id": 5
}
]
I've tried working with these ExpandoObject
s, but can't seem to make it work, as I can't do what's illustrated in the top of this question (I don't know the names of the properties). Is there a way for me to say something like:
var obj = new ExpandoObject();
var propName = "Product";
var obj.propName = "Pie"
Console.WriteLine("Let's print!: " + obj.Product);
//OUTPUT
Let's print!: Pie
Does anyone have a solution, og simply guidance to a structure, that might solve this situation?
Rather than creating an
ExpandoObject
or some other dynamic type, you could create aList<Dictionary<string, object>>
where eachDictionary<string, object>
contains the name/value pairs you want to serialize. Then serialize to JSON using Json.NET (orJavaScriptSerializer
, though that is less flexible):The first outputs:
The second outputs the same without the indentation:
As you can see here ExpandoObject Class, the
ExpandoObject
is implementingIDictionary<string, object>
, so you can use that fact likeUse
dynamic
, then cast toIDictionary<string, object>
to loop through your properties:I've made a fiddle: https://dotnetfiddle.net/yFLy2u
Now this is a solution to your question... other answers like @dbc's might be better suited to the problem (which is not the question, really)
While I was writing the answer, I see you already got proper answer. You can use a
Dictionary<string, onject>
or evenTuple
.But as per your original question, you wanted to add properties dynamically. For that you can refer to other answer using
ExpandoObject
. This is just the same solution (usingExpandoObject
to dynamically add properties) with classes similar to your code.Ans usage example:
First part, read this blog post by C# team thoroughly.
Lets see your code
In your code you are using
var obj = new ExpandoObject();
, so you are creating a statically typed object of typeExpandableObject
. In the blog they specifically call outSo if you rewrite your code to use
dynamic obj
, and add the dynamic properties as properties it should work!But for your particular use case you better use Dictionaries as suggested above by @dbc