Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A List inside an ExpandoObject

dynamic model = new ExpandoObject();
model.Data = "asdf";

List<dynamic> listOfx = new List<dynamic>();
for (int i = 0; i < 3; i++) {
    dynamic x = new ExpandoObject();
    x.ID = i;
    x.Name = "test" + i.ToString();
    listOfx.Add(x);
}
model.listOfx = listOfx;

When I run this, I can see Data inside model, but not listOfx.

Problem: how to get a list(or IEnumerable) inside an ExpandoObject

UPDATE on Solution: enter image description here

Because I couldn't see the lifOfx in the locals window I thought it wasn't working. Here (through y) you can see it is. :-)

like image 497
Dave Mateer Avatar asked Oct 19 '11 22:10

Dave Mateer


People also ask

How do you add a list to ExpandoObject?

To display a List<ExpandoObject>, creating columns automatically based on data, add a DataGridView to your form, do not set any property yet, and execute the next code: // Sample list. List<ExpandoObject> list = new List<ExpandoObject>( );

What is an ExpandoObject?

The ExpandoObject class enables you to add and delete members of its instances at run time and also to set and get values of these members. This class supports dynamic binding, which enables you to use standard syntax like sampleObject. sampleMember instead of more complex syntax like sampleObject.

How do I know if I have ExpandoObject property?

For ExpandoObject, you can simply check whether the property is defined as a key in the underlying dictionary. For other implementations, it might be challenging and sometimes the only way is to work with exceptions.


2 Answers

I can't reproduce similar issues on Mono 2.10:

using System.Dynamic;
using System.Collections.Generic;

using System;

public class Program
{
    public static void Main(string[] args)
    {
        dynamic x = new ExpandoObject();
        x.Data ="test";
        x.Arr = new [] { "test1","test2"};
        x.Lst = new List<string> { "aap", "noot", "mies" };

        Console.WriteLine(string.Join(", ", x.Arr));
        Console.WriteLine(string.Join(", ", x.Lst));
    }
}

Output:

/tmp @ dmcs test.cs && mono test.exe
test1, test2
aap, noot, mies

I'll be retesting on windows shortly.

Update have tested the following:

  • the linux-compiled (dmcs) binary run on Windows with Mono 2.10: OK
  • the linux-compiled (dmcs) binary run on Windows with MS.NET 4.0: OK
  • the windows-compiled (dmcs) binary run on Windows with Mono 2.10: OK
  • the windows-compiled (dmcs) binary run on Windows with MS.NET 4.0: OK
  • the windows-compiled (csc.exe) binary run on Windows with Mono 2.10: OK
  • the windows-compiled (csc.exe) binary run on Windows with MS.NET 4.0: OK

On linux I have only tested the binary compiled by mono itself, but I don't anticipate any problems. Perhaps there is something subtly different about storing dynamics inside the List<>, I'll test that now

like image 192
sehe Avatar answered Sep 24 '22 23:09

sehe


The code you have, above, works perfectly well for setting up the list. For example, adding this after your code will work fine:

// Access value inside list
Console.WriteLine(model.listOfx[1].Name);

// Iterate through list
foreach (var o in model.listOfx)
{
    Console.WriteLine(o.ID);
}

For example, try the following (fully functional example):

using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;

public static class Test
{

    public static void Main()
    {
        dynamic model = new ExpandoObject();
        model.Data = "asdf";

        List<dynamic> listOfx = new List<dynamic>();
        for (int i = 0; i < 3; i++)
        {
            dynamic x = new ExpandoObject();
            x.ID = i;
            x.Name = "test" + i.ToString();
            listOfx.Add(x);
        }
        model.listOfx = listOfx;

        // Access value inside list
        Console.WriteLine(model.listOfx[1].Name);

        // Iterate through list
        foreach (var o in model.listOfx)
        {
            Console.WriteLine(o.ID);
        }

        Console.ReadKey();
    }
}

This uses your exact sample code.

like image 42
Reed Copsey Avatar answered Sep 25 '22 23:09

Reed Copsey