0

My code is like this

var eventDocs = new List<dynamic>();     
foreach (var node in eventtypeNode.GetDescendantNodes())
{
    string files = node.GetProperty("document").Value;
    eventDocs.Add(new { Id = node.Id, Name = node.Name, CreatedOn = node.CreateDate, Path = files });
}

This works good. Now I am trying to fetch the data out of this dynamic list

foreach (var eventDoc in eventDocs)
{
     eventDoc.----  //nothing comes on intellisence
}

Nothing comes on IntelliSense? Am I doing anything wrong?

2
  • 2
    Why are you using dynamic anyway? You have a clearly defined type there. Create a class and use it. Commented May 15, 2014 at 6:08
  • @SimonWhitehead Agree. Total abuse of dynamic. Commented May 15, 2014 at 6:08

3 Answers 3

5

You won't get anything from Intellisense precisely because you've got a List<dynamic>. You're saying, "I don't know at compile-time what this list will contain. When I access members of the elements, just bind that dynamically at execution-time."

Given that you're deferring binding to execution time, why would you be surprised that Intellisense can't tell what will be in the list?

It looks to me like you should change your code to use a LINQ query to start with - then you can have a list with a known element type, which will be an anonymous type.

var eventDocs = eventtypeNode.GetDescendantsNodes()
      .Select(node => new { Id = node.Id, 
                            Name = node.Name, 
                            CreatedOn = node.CreateDate,
                            Path = node.GetProperty("document").Value })
      .ToList();
Sign up to request clarification or add additional context in comments.

Comments

2

You cannot access dynamic members like this, try GetDynamicMemberNames() and GetMetaObject method

Comments

2

Intellisense will not show suggestions, since the data is dynamic, and it doesn't know what to show.

But you know what it contains. So just code and you'll be fine.

Also, you don't need dynamic objects here. Since what you want is well-defined just define your class with necessary properties and methods and then create the list:

List<YourClass> list = new List<YourClass>();

And then Intellisense will become intelligent enough to show the suggestions ;)

4 Comments

...why use dynamic then? The OP has a clearly defined type. Make it so..
@SimonWhitehead, what type is it? Can you tell me so that I can edit? I've not come across Node etc.. What is the name of the type that OP is using?
You don't need to know that. The OP is storing an anonymous object into a list of dynamic. The OP just needs to define an actual object (not an anonymous one) and type the list as such - boom, intellisense.
@SimonWhitehead, is my edit ok?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.