Yahwe Raj
Yahwe Raj

Reputation: 2057

How to get the property information from dynamic type generic list?

I have a dynamic type generic list and I tried to get the property information from the list. but it throws null reference exception.

List<dynamic> dy = new List<dynamic>();
dy.Add(new { OrderID = 11, EmployeeID = 5, CustomerID = "ALFKI" });
dy.Add(new { OrderID = 12, EmployeeID = 4, CustomerID = "BSDEE" });
dy.Add(new { OrderID = 13, EmployeeID = 6, CustomerID = "VDSAW" });

var prop = dy.GetType().GetProperty("EmployeeID");

It will throw the null reference exception.

how to get the property details from the list of dynamic object?

Upvotes: 2

Views: 136

Answers (1)

M.kazem Akhgary
M.kazem Akhgary

Reputation: 19179

Because you are using anonymous type. first you have to get the type of anonymous type.

prop = dy.FirstOrDefault()?.GetType().GetProperty("EmployeeID");

Upvotes: 1

Related Questions