-1

I am trying to do a simple data series test andeverything is working as intended, but I want to switch

List<DateTime> events = new List<DateTime>();

to

List<dynamic> events = new List<dynamic>();

When I switch the list to dynamic I am getting these 2 errors. "One or more types required to compile dynamic expression cannot be found. Are you missing a reference?" and these two errors are occuring on the Console.Writeline

Any idea how I can output the List with dynamic?

Here's my code:

 [TestMethod]
        public void TimeSeriesData()
        {
            List<dynamic> events = new List<dynamic>();
            var stream = new
            {
                Id = 15,
                Name = "StreamA" + 15,
                TypeId = "TypeA"
            };
            _server.PostStream(stream);

            DateTime startTime = DateTime.Today;  //  12:00
            DateTime endTime = DateTime.Today; // 12:00 +1 sec
            endTime = endTime.AddDays(1);
            endTime=endTime.AddSeconds(1);

            for (int i = 0; i < 3000; i++)
            {
                if (i % 2 == 0) //is even
                {
                    var dataStart = new
                    {
                        TimeId = startTime.AddSeconds(i),
                        ValueStart = i
                    };

                    events.Add(startTime.AddSeconds(i));
                }

                else  //is odd
                {
                    var dataEnd = new
                    {
                        TimeEnd = endTime.AddSeconds(-i),
                        ValueEnd = i

                    };
                    events.Add(endTime.AddSeconds(-i));
                }


            }

            foreach (var item in events)
                Console.WriteLine("Time: {0} ", item.ToString());
}
Mdjon26
  • 2,145
  • 4
  • 19
  • 29
  • 1
    While you shouldn't really make a generic list and instead use interfaces or something that represents behavior, you could use `object` instead of `dynamic` there and it will work. – Travis J Jun 27 '14 at 19:46
  • Is "Microsoft.CSharp" referenced in your test project? Are you seeing that error in the `Console.WriteLine` call? – Mario J Vargas Jun 27 '14 at 19:48
  • I have to use dynamic for a call later on in the code, Travis... or else I would use object – Mdjon26 Jun 27 '14 at 19:52
  • I am seeing that in the Console.WriteLine call, Mario. – Mdjon26 Jun 27 '14 at 19:53
  • What were wrong with the answers here http://stackoverflow.com/questions/24453865/outputting-a-c-sharp-object-list – L.B Jun 27 '14 at 19:54
  • That's what I thought. When I was about to post my answer for fixing it, the post had been closed. Reference `Microsoft.CSharp.dll` in your unit test project and it will compile. The error I got when building was "Predefined type 'Microsoft.CSharp.RuntimeBinder.Binder' is not defined or imported" – Mario J Vargas Jun 27 '14 at 19:58

1 Answers1

-1

Not sure, this works just fine:

        var l = new List<dynamic>();
        l.Add(new {dt=DateTime.Now});
        l.Add(DateTime.Now);

        foreach (var o in l)
        {
            Console.WriteLine(o.ToString());
        }

Your code compiles and runs fine on my machine.

Darek
  • 4,687
  • 31
  • 47
  • Did you try the issue in a VS Test Project? Did you add additional references? I tried it and the issue had to do with Console.WriteLine within the test project. The "Microsoft.CSharp" assembly needed to be referenced for the code to compile. – Mario J Vargas Jun 27 '14 at 19:51
  • Ah, that would explain it, usually I don't use Console.WriteLine() in a unit test. I prefer Trace.WriteLine() which captures the output. – Darek Jun 27 '14 at 19:52