문제

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?

도움이 되었습니까?

해결책

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();

다른 팁

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

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 ;)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top