Skip to content

Instantly share code, notes, and snippets.

@tenfortyeight
Created November 14, 2013 07:32
Show Gist options
  • Save tenfortyeight/7462857 to your computer and use it in GitHub Desktop.
Save tenfortyeight/7462857 to your computer and use it in GitHub Desktop.
How come that the first version doesn't work, but the other do? I am missing something in all the dynamics...
//Version 1 - Don't work
public IEnumerable<string> GetAll()
{
var dataHistory = tbl.All(); //Massive, returns a dynamic object
var history = Deserialize(dataHistory);
return history;
}
private IEnumerable<string> Deserialize(dynamic dataHistory)
{
foreach (dynamic dataMessage in dataHistory) //This will blow up: Massive.DynamicModel.Query(string, params object[])' is a 'method' but is used like a 'type'
{
using (var stream = new MemoryStream(dataMessage.Payload))
{
//do stuff
yield return;
}
}
}
//Version 2: this works just fine. Same thing but not passing in the dynamic object as a parameter
public IEnumerable<string> GetAll()
{
dynamic tbl = new MessageHistory();
var dataHistory = tbl.All();
var history = new List<string>();
foreach (dynamic dataMessage in dataHistory)
{
using (var stream = new MemoryStream(dataMessage.Payload))
{
//do stuff
history.Add(message);
}
}
return history;
}
@victorarias
Copy link

There is a lot going on there. I tried to reproduce the problem here without Massive, but I couldn't without the whole database/data reader infrastructure.

I'm not sure why this is happening, but I guess the C# compiler is getting "confused" with the foreach/IEnumerable + dynamic language runtime. Have you tried accepting an IEnumerable in the Deserialize method instead of just dynamic? I bet it'll make it work.

If you can, paste the IL of the broken code here. I'm curious about what is happening :)

@victorarias
Copy link

Ops, I mean accepting a IEnumerable< dynamic > in the Deserialize method, not just IEnumerable :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment