Hi I have created a List of objects and when I try to acces the properties I get an error saying that the object does not contain a definition for that property.Here is my code:
This is is where I create the List of objects:
public List<object> returnProductData(SetProduct product)
{
List<object> productData = new List<object>();
var type = product.GetType();
var properties = type.GetProperties();
foreach (var propertyInfo in properties)
{
if( propertyInfo.GetValue(product , null) != null && propertyInfo.Name != "ProductName" &&
propertyInfo.Name != "Brand" && propertyInfo.Name != "ProductPrice" && propertyInfo.Name != "ProductImagePath" )
{
productData.Add(new { propertyName = propertyInfo.Name , propertyValue = propertyInfo.GetValue(product , null).ToString()}) ;
}
}
return productData;
}
This is where I try to acces it:
@foreach( var productData in ion)
{
<li>@productData.propertyName</li>
}
When I try to acces it it says that the object does not contain a definition for propertyName.What am I doing wrong here?
The
objecthasn’tpropertyNameproperty.If described scenario is very necessary and if you use .net 4.0 or above you can use
dynamiclike this:Usage: