1

I need help for get a object values, this object is in an object list, for example.

list<object> objectList = new list<object>();
objectList.Add( new {  id = 1,  name = "name1"});
objectList.Add( new {  id = 2,  name = "name2"});

the problem is I can't get the values of these objects inside the list, and put their values in a excel file, I tried this.

int row = 2
for(int i = 0; row < objectList ; i++){
 excelRow[row,1].Value = item[i].id
 excelRow[row,2].Value = item[i].name
row++
}
2
  • 4
    it's not a generic object it anonymouse object. Commented Feb 3, 2016 at 21:11
  • Possible duplicate of A generic list of anonymous class Commented Feb 3, 2016 at 21:14

2 Answers 2

8

You can use an implicitly typed array to create an array that's actually typed to the anonymous type that you're using:

var objectList = new []
{
    new {  id = 1,  name = "name1"},
    new {  id = 2,  name = "name2"},
};

Once you've done that when you get an item from the collection it's actually it's real type (which has no name) rather than object.

Sign up to request clarification or add additional context in comments.

Comments

-1

A dynamic object can fit for this

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.